Skip to content

Commit

Permalink
working layer to block method
Browse files Browse the repository at this point in the history
  • Loading branch information
andywar65 committed Nov 29, 2022
1 parent 22cb777 commit 765a9d6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
53 changes: 53 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,59 @@ def __str__(self):
return "block-" + self.name + "-" + str(self.id)
return self.name + "-" + str(self.id)

def transform_to_block(self):
# layer "0" can't be transformed in block
if self.name == "0":
return
# already a block
if self.is_block:
return
# get drawing's "0" layer
zero = Layer.objects.get(drawing_id=self.drawing.id, name="0")
# prepare and save block insertion
insert = Insertion(
layer=zero,
block=self,
point=self.drawing.geom,
geom=self.geom,
)
insert.save()
# get drawing origin
long_d = self.drawing.geom["coordinates"][0]
lat_d = self.drawing.geom["coordinates"][1]
# prepare block geometries
geometries_b = []
for geom in self.geom["geometries"]:
# from longlat to xy
if geom["type"] == "Polygon":
vert = latlong2xy(geom["coordinates"][0], long_d, lat_d)
else:
vert = latlong2xy(geom["coordinates"], long_d, lat_d)
# back to longlat with coordinates (0,0)
vert = xy2latlong(vert, 0, 0, 0, 1, 1)
if geom["type"] == "Polygon":
geometries_b.append(
{
"type": "Polygon",
"coordinates": [vert],
}
)
else:
geometries_b.append(
{
"type": "LineString",
"coordinates": vert,
}
)
# prepare and save block
self.geom = {
"geometries": geometries_b,
"type": "GeometryCollection",
}
self.color_field = "#FFFFFF"
self.is_block = True
super().save()

@property
def popupContent(self):
if self.is_block:
Expand Down
10 changes: 7 additions & 3 deletions views.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ def setup(self, request, *args, **kwargs):
self.layer = get_object_or_404(Layer, id=self.kwargs["pk"])
if request.user != self.layer.drawing.user:
raise PermissionDenied
self.layer.transform_to_block()

def get_redirect_url(self, *args, **kwargs):
return reverse(
Expand All @@ -341,8 +342,9 @@ def setup(self, request, *args, **kwargs):
raise Http404(_("Can't delete layer '0'"))
if request.user != layer.drawing.user:
raise PermissionDenied
name = layer.name
layer.delete()
messages.error(request, _('Layer "%s" deleted') % layer.name)
messages.error(request, _('Layer "%s" deleted') % name)


class InsertionCreateView(PermissionRequiredMixin, CreateView):
Expand Down Expand Up @@ -424,8 +426,9 @@ def setup(self, request, *args, **kwargs):
insert = get_object_or_404(Insertion, id=self.kwargs["pk"])
if request.user != insert.block.drawing.user:
raise PermissionDenied
id = insert.id
insert.delete()
messages.error(request, _('Insertion "%d" deleted') % insert.id)
messages.error(request, _('Insertion "%d" deleted') % id)


class InsertionExplodeInlineView(PermissionRequiredMixin, TemplateView):
Expand All @@ -440,8 +443,9 @@ def setup(self, request, *args, **kwargs):
if request.user != insert.block.drawing.user:
raise PermissionDenied
insert.explode_instance()
id = insert.id
insert.delete()
messages.error(request, _('Insertion "%d" exploded') % insert.id)
messages.error(request, _('Insertion "%d" exploded') % id)


def drawing_download(request, pk):
Expand Down

0 comments on commit 765a9d6

Please sign in to comment.