diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-02-19 17:36:25 +0100 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-04-24 19:38:56 +0200 | 
| commit | 986511b2572acb369086ed613adc32ff1d1863a9 (patch) | |
| tree | 1ac2de99841b7a28fee128a68700f59458594f14 /ishtar_common/utils.py | |
| parent | a6b1ad5c73d2a51ab2bf6ba1a3451d7298993722 (diff) | |
| download | Ishtar-986511b2572acb369086ed613adc32ff1d1863a9.tar.bz2 Ishtar-986511b2572acb369086ed613adc32ff1d1863a9.zip | |
Geo: manage container and warehouse - manage geo save dependencies
Diffstat (limited to 'ishtar_common/utils.py')
| -rw-r--r-- | ishtar_common/utils.py | 104 | 
1 files changed, 84 insertions, 20 deletions
| diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 408309c4e..2aa8db298 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -487,18 +487,28 @@ def post_save_geo(sender, **kwargs):      """      Convert raw x, y, z point to real geo field      """ -    if not kwargs.get('instance'): +    if not kwargs.get('instance') or getattr(kwargs['instance'], +                                             '_post_saved_geo', False):          return      from ishtar_common.models import get_current_profile  # not clean but utils      # is loaded before models      instance = kwargs.get('instance') +    current_source = unicode(instance.__class__._meta.verbose_name) +    if instance.point_source_item and \ +            instance.point_source_item != current_source:  # refetch +        instance.point, instance.point_2d = None, None +        instance.x, instance.y = None, None +        instance.point_source = None +      point = instance.point      point_2d = instance.point_2d      profile = get_current_profile()      modified = False +    print (instance) -    if (point or point_2d) and instance.x is None:  # db source +    if (point or point_2d) and instance.x is None and not \ +            instance.point_source:  # db source          if point:              current_point = point              instance.z = point.z @@ -509,11 +519,13 @@ def post_save_geo(sender, **kwargs):          srs = get_srid_obj_from_point(current_point)          instance.spatial_reference_system = srs          instance.point_source = 'P' +        instance.point_source_item = current_source          if not point_2d:              instance.point_2d = convert_coordinates_to_point(                  instance.point.x, instance.point.y,                  srid=current_point.srid) -    elif not instance.point_source and instance.x and instance.y and \ +        modified = True +    elif 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 @@ -523,43 +535,95 @@ def post_save_geo(sender, **kwargs):                  srid=instance.spatial_reference_system.srid)          except forms.ValidationError:              return  # irrelevant data in DB +        distance = 1  # arbitrary +        if point_2d and instance.point_2d: +            distance = point_2d.transform( +                4326, clone=True).distance( +                instance.point_2d.transform(4326, clone=True)) +          if instance.z:              point = convert_coordinates_to_point(                  instance.x, instance.y, instance.z,                  srid=instance.spatial_reference_system.srid) -        if point_2d != instance.point_2d or point != instance.point: +        # no change if distance inf to 1 mm +        if distance >= 0.0001 and (point_2d != instance.point_2d +                                   or point != instance.point):              instance.point = point              instance.point_2d = point_2d              instance.point_source = 'P' +            instance.point_source_item = current_source              modified = True -    elif not point_2d and profile.use_town_for_geo:  # try to get from parent -        point_2d = instance.get_town_centroid() -        if point_2d != instance.point_2d: +    else: +        instance.point_source = None +        # get coordinates from parents +        precise_points = instance.get_precise_points() +        if precise_points: +            point_2d, point, source_item = precise_points              instance.point_2d = point_2d -            instance.point_source = 'T'  # town +            instance.point = point +            instance.point_source = 'P' +            instance.point_source_item = source_item              instance.x = point_2d.x              instance.y = point_2d.y +            if point: +                instance.z = point.z              srs = get_srid_obj_from_point(point_2d)              instance.spatial_reference_system = srs              modified = True -    if not hasattr(instance, 'multi_polygon'): -        return - -    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 +        elif profile.use_town_for_geo:  # try to get from parent +            centroid = instance.get_town_centroid() +            if centroid and centroid[0]: +                instance.point_2d, instance.point_source_item = centroid +                instance.point = None +                instance.point_source = 'T' +                instance.x = instance.point_2d.x +                instance.y = instance.point_2d.y +                srs = get_srid_obj_from_point(instance.point_2d) +                instance.spatial_reference_system = srs +                modified = True +            else: +                instance.point_2d, instance.point_source_item = None, None +                instance.point = None +                instance.point_source = None +                modified = True + +    if hasattr(instance, 'multi_polygon'): +        if instance.multi_polygon_source_item and \ +                instance.multi_polygon_source_item != current_source:  # refetch +            instance.multi_polygon = None +            instance.multi_polygon_source = None + +        if instance.multi_polygon and not instance.multi_polygon_source: +            # should be a db source +            instance.multi_polygon_source = 'P' +            instance.multi_polygon_source_item = current_source              modified = True +        elif instance.multi_polygon_source != 'P': +            precise_poly = instance.get_precise_polygons() +            if precise_poly: +                poly, source_item = precise_poly +                instance.multi_polygon = poly +                instance.multi_polygon_source = 'P' +                instance.multi_polygon_source_item = source_item +                modified = True +            elif profile.use_town_for_geo: +                poly = instance.get_town_polygons() +                if poly: +                    poly, poly_source = poly +                    if poly != instance.multi_polygon: +                        instance.multi_polygon_source_item = poly_source +                        instance.multi_polygon_source = 'T'  # town +                        try: +                            instance.multi_polygon = poly +                            modified = True +                        except TypeError: +                            print(instance, instance.pk)      if modified:          instance.skip_history_when_saving = True +        instance._post_saved_geo = True          instance.save()      return | 
