From 765a9d6485a7718de75192752f9dc25199f4eb33 Mon Sep 17 00:00:00 2001 From: andywar65 Date: Tue, 29 Nov 2022 19:07:35 +0100 Subject: [PATCH] working layer to block method --- models.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ views.py | 10 +++++++--- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/models.py b/models.py index 016fe0a..c839436 100644 --- a/models.py +++ b/models.py @@ -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: diff --git a/views.py b/views.py index 401e5c9..df35e0c 100644 --- a/views.py +++ b/views.py @@ -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( @@ -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): @@ -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): @@ -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):