diff options
author | QuentinAndre <quentin.andre@imt-atlantique.net> | 2021-07-06 20:19:37 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-09-01 12:36:45 +0200 |
commit | 0dbba5048ae60e4ba746ef100605e8edea72d6ce (patch) | |
tree | 19f3736e64fc507344a1269881988a589b864fcb | |
parent | 79cff7a266fc1dd4abf4ca749a60fda30dee49d1 (diff) | |
download | Ishtar-0dbba5048ae60e4ba746ef100605e8edea72d6ce.tar.bz2 Ishtar-0dbba5048ae60e4ba746ef100605e8edea72d6ce.zip |
to geoJSON structure
-rw-r--r-- | archaeological_finds/tests.py | 42 | ||||
-rw-r--r-- | ishtar_common/models_common.py | 39 |
2 files changed, 54 insertions, 27 deletions
diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index 4e237673a..e06d5faa0 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -2523,7 +2523,13 @@ class GeomaticTest(FindInit, TestCase): base_find.multi_polygon = None base_find.save() base_find = models.BaseFind.objects.get(pk=base_find.pk) - dic = {"centroid": (2.0, 43.0)} + dic = { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [2.0, 43.0] + } + } res = base_find.get_geo_items(get_polygons=True) self.assertEqual(dic, res) res = base_find.get_geo_items(get_polygons=False) @@ -2542,14 +2548,32 @@ class GeomaticTest(FindInit, TestCase): 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) + dict_poly = { + "type": "Feature", + "geometry": { + "type": "MultiPolygon", + "coordinates": [ + [ + [[1.0, 1.0], [5.0, 1.0], [5.0, 5.0], [1.0, 5.0], [1.0, 1.0]], + [[2.0, 2.0], [2.0, 3.0], [3.0, 3.0], [3.0, 2.0], [2.0, 2.0]], + ], + [ + [[6.0, 3.0], [9.0, 2.0], [9.0, 4.0], [6.0, 3.0]] + ] + ] + }, + } + res_poly = base_find.get_geo_items(get_polygons=True) + self.assertEqual(dict_poly, res_poly) + dict_centroid = { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [3.86111, 3.02778] + } + } + res_centroid = base_find.get_geo_items(get_polygons=False) + self.assertEqual(dict_centroid, res_centroid) class AutocompleteTest(AutocompleteTestBase, TestCase): fixtures = FIND_FIXTURES diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index 1462d538e..3d8b4f8f6 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -22,7 +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.gis.geos import Point from django.contrib.postgres.fields import JSONField from django.contrib.postgres.search import SearchVectorField, SearchVector from django.contrib.sites.models import Site @@ -2848,26 +2848,29 @@ class GeoItem(models.Model): return self.multi_polygon, self.multi_polygon_source_item def get_geo_items(self, get_polygons=False, rounded=True): - dict = {} + dict = { + "type": "Feature", + "geometry": {}, + } if self.multi_polygon: - i = 0 if get_polygons: + list_coords = [] 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)) + list_coords.append([]) + for linear_ring in range(len(polygon)): + list_coords[-1].append([]) + for coords in polygon[linear_ring]: + point_2d = Point(coords[0], coords[1], srid=self.multi_polygon.srid) + list_coords[-1][linear_ring].append(self.convert_coordinates(point_2d, rounded)) + dict["geometry"]["type"] = "MultiPolygon" + dict["geometry"]["coordinates"] = list_coords else: - dict["centroid"] = self.convert_coordinates(self.multi_polygon.centroid, rounded) + dict["geometry"]["type"] = "Point" + dict["geometry"]["coordinates"] = self.convert_coordinates(self.multi_polygon.centroid, rounded) else: - dict["centroid"] = self.display_coordinates + dict["geometry"]["type"] = "Point" + x,y = self.display_coordinates + dict["geometry"]["coordinates"] = [x,y] return dict def convert_coordinates(self, point_2d, rounded): @@ -2885,8 +2888,8 @@ class GeoItem(models.Model): 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 + return [round(x, 5), round(y, 5)] + return [x, y] def most_precise_geo(self): if self.point_source == "M": |