From a05222260de530d44980931badc2db41fe4b6cb6 Mon Sep 17 00:00:00 2001 From: QuentinAndre Date: Tue, 6 Jul 2021 17:57:21 +0200 Subject: get_geo_items + test --- ishtar_common/models_common.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'ishtar_common/models_common.py') diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index db7e77ce0..bb1fd4e19 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -22,6 +22,7 @@ from django.conf import settings from django.contrib.auth.models import User, Group from django.contrib.contenttypes.models import ContentType from django.contrib.gis.db import models +from django.contrib.gis.geos import GEOSGeometry from django.contrib.postgres.fields import JSONField from django.contrib.postgres.search import SearchVectorField, SearchVector from django.contrib.sites.models import Site @@ -2846,6 +2847,44 @@ class GeoItem(models.Model): if self.multi_polygon_source == "P" and self.multi_polygon: return self.multi_polygon, self.multi_polygon_source_item + def get_geo_items(self, get_polygons=False, rounded=True): + dict = {} + if get_polygons and self.multi_polygon: + i = 0 + for polygon in self.multi_polygon: + i += 1 + for linearRing in range(len(polygon)): + if linearRing == 0: + key = "external" + else: + key = "internal" + key += "Polygon" + str(i) + dict[key] = [] + for coords in polygon[linearRing]: + point_2d = GEOSGeometry("POINT({} {})".format(coords[0], coords[1]), srid=self.multi_polygon.srid) + dict[key].append(self.convert_coordinates(point_2d, rounded)) + else: + dict["centroid"] = self.display_coordinates + return dict + + def convert_coordinates(self, point_2d, rounded): + profile = get_current_profile() + if ( + not profile.display_srs + or not profile.display_srs.srid + or ( + profile.display_srs == self.spatial_reference_system + and point_2d.x + and point_2d.y) + ): + x, y = point_2d.x, point_2d.y + else: + point = point_2d.transform(profile.display_srs.srid, clone=True) + x, y = point.x, point.y + if rounded: + return round(x, 5), round(y, 5) + return x, y + def most_precise_geo(self): if self.point_source == "M": return "multi_polygon" -- cgit v1.2.3