diff options
author | Étienne Loks <etienne.loks@peacefrogs.net> | 2019-02-06 14:25:30 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-04-24 19:38:56 +0200 |
commit | 172eb15954d2403831b073c785c38cac84505927 (patch) | |
tree | 7d2e9da91f30d86a408ec1cc6d984997993a4bfd /ishtar_common | |
parent | 06747b5bef984838030667cf3b17e466cf5d27f7 (diff) | |
download | Ishtar-172eb15954d2403831b073c785c38cac84505927.tar.bz2 Ishtar-172eb15954d2403831b073c785c38cac84505927.zip |
Geo: manage autogen of polygons
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/tests.py | 4 | ||||
-rw-r--r-- | ishtar_common/utils.py | 84 |
2 files changed, 70 insertions, 18 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 8ecf98255..53ad67faa 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -45,7 +45,7 @@ from django.test.runner import DiscoverRunner from ishtar_common import models from ishtar_common import views from ishtar_common.apps import admin_site -from ishtar_common.utils import post_save_point, update_data, move_dict_data, \ +from ishtar_common.utils import post_save_geo, update_data, move_dict_data, \ rename_and_simplify_media_name, try_fix_file @@ -1589,7 +1589,7 @@ class GeomaticTest(TestCase): x=2, y=3, z=4, spatial_reference_system=srs) self.assertIsNone(obj.point_2d) - post_save_point(None, instance=obj) + post_save_geo(None, instance=obj) self.assertIsNotNone(obj.point_2d) self.assertIsNotNone(obj.point) diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 20957c43e..0f33fd30d 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -469,10 +469,11 @@ def convert_coordinates_to_point(x, y, z=None, srid=4326): return geom -def post_save_point(sender, **kwargs): +def post_save_geo(sender, **kwargs): """ Convert raw x, y, z point to real geo field """ + from ishtar_common.models import SpatialReferenceSystem if not kwargs.get('instance'): return instance = kwargs.get('instance') @@ -481,21 +482,72 @@ def post_save_point(sender, **kwargs): from ishtar_common.models import get_current_profile # not clean but utils # must be loaded before models profile = get_current_profile() - if instance.x and instance.y and \ - instance.spatial_reference_system and \ - instance.spatial_reference_system.auth_name == 'EPSG' and \ - instance.spatial_reference_system.srid != 0: - point_2d = convert_coordinates_to_point( - instance.x, instance.y, srid=instance.spatial_reference_system.srid) - if instance.z: - point = convert_coordinates_to_point( - instance.x, instance.y, instance.z, - srid=instance.spatial_reference_system.srid) - elif profile.use_town_for_geo: - point_2d = instance.get_town_centroid() - if point_2d != instance.point_2d or point != instance.point: - instance.point = point - instance.point_2d = point_2d + modified = False + + if (point or point_2d) and instance.x is None: # db source + if point: + current_point = point + instance.z = point.z + else: + current_point = point_2d + instance.x = current_point.x + instance.y = current_point.y + try: + srs = SpatialReferenceSystem.objects.get( + srid=int(current_point.srid)) + except SpatialReferenceSystem.DoesNotExist: + srs = SpatialReferenceSystem.objects.create( + srid=int(current_point.srid), + auth_name='EPSG', + label=u"EPSG-{}".format(current_point.srid), + txt_idx=u"epsg-{}".format(current_point.srid), + ) + instance.spatial_reference_system = srs + instance.point_source = 'P' + if not point_2d: + instance.point_2d = convert_coordinates_to_point( + instance.point.x, instance.point.y, + srid=current_point.srid) + elif not point_2d: + source = None + if instance.x and instance.y and \ + instance.spatial_reference_system and \ + instance.spatial_reference_system.auth_name == 'EPSG' and \ + instance.spatial_reference_system.srid != 0: # form input + try: + point_2d = convert_coordinates_to_point( + instance.x, instance.y, + srid=instance.spatial_reference_system.srid) + except forms.ValidationError: + return # irrelevant data in DB + if point_2d: + source = 'P' # precise + if instance.z: + point = convert_coordinates_to_point( + instance.x, instance.y, instance.z, + srid=instance.spatial_reference_system.srid) + elif profile.use_town_for_geo: # + point_2d = instance.get_town_centroid() + source = 'T' # town + + if point_2d != instance.point_2d or point != instance.point: + instance.point = point + instance.point_2d = point_2d + instance.point_source = source + modified = True + + if instance.multi_polygon and not instance.multi_polygon_source: + # should be a db source + instance.multi_polygon_source = 'P' + modified = True + elif profile.use_town_for_geo and instance.multi_polygon_source != 'P': + poly = instance.get_town_polygons() + if poly and poly != instance.multi_polygon: + instance.multi_polygon_source = 'T' # town + instance.multi_polygon = poly + modified = True + + if modified: instance.skip_history_when_saving = True instance.save() return |