diff options
| author | Quentin André <quentin.andre@imt-atlantique.net> | 2021-07-06 17:57:21 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-07-08 09:58:48 +0200 | 
| commit | c39d2d8a7e904467c1ed9f719da3c17894ceed83 (patch) | |
| tree | 52b54e58e6d195b840be433305a5a7beefa1e49d | |
| parent | c3f9a8944f8c9892a007f95c24d0bf23001fe840 (diff) | |
| download | Ishtar-c39d2d8a7e904467c1ed9f719da3c17894ceed83.tar.bz2 Ishtar-c39d2d8a7e904467c1ed9f719da3c17894ceed83.zip | |
get_geo_items + test
| -rw-r--r-- | archaeological_finds/tests.py | 33 | ||||
| -rw-r--r-- | ishtar_common/models_common.py | 39 | 
2 files changed, 72 insertions, 0 deletions
| diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index c2979d074..8cac07812 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -2521,6 +2521,39 @@ class GeomaticTest(FindInit, TestCase):          self.assertEqual(base_find.point_2d.ewkt, "SRID=4326;POINT (42 3)")          self.assertEqual(base_find.point_source, "P") +    def test_get_geo_items(self): +        profile = get_current_profile() +        profile.mapping = True +        profile.save() + +        find = self.finds[0] +        base_find = find.base_finds.all()[0] +        srs, __ = SpatialReferenceSystem.objects.get_or_create( +            txt_idx="wgs84", +            defaults={"srid": 4326, "label": "WGS84", "auth_name": "EPSG"}, +        ) + +        base_find = models.BaseFind.objects.get(pk=base_find.pk) + +        limit = ( +            "MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))," +            "((6 3,9 2,9 4,6 3)))" +        ) +        base_find.multi_polygon = "SRID=4326;" + limit +        base_find.point_source = None +        base_find.point_2d = None +        base_find.point = None +        base_find.save() + +        base_find = models.BaseFind.objects.get(pk=base_find.pk) +        dictPoly = {"externalPolygon1": [(1.0,1.0), (5.0,1.0), (5.0,5.0), (1.0,5.0), (1.0,1.0)], +                "internalPolygon1": [(2.0,2.0), (2.0,3.0), (3.0,3.0), (3.0,2.0), (2.0,2.0)], +                "externalPolygon2": [(6.0,3.0), (9.0,2.0), (9.0,4.0), (6.0,3.0)]} +        resPoly = base_find.get_geo_items(get_polygons=True) +        self.assertEqual(dictPoly, resPoly) +        dictCentroid = {"centroid": (3.86111, 3.02778)} +        resCentroid = base_find.get_geo_items(get_polygons=False) +        self.assertEqual(dictCentroid, resCentroid)  class AutocompleteTest(AutocompleteTestBase, TestCase):      fixtures = FIND_FIXTURES diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index ac42e5ee6..f21a74a12 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 @@ -2855,6 +2856,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" | 
