diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-03-31 17:38:31 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:21:00 +0100 |
commit | 810cf25c85cd44fe1610db4792693983a81c8818 (patch) | |
tree | 8c890597afae74c44266ccbec14882a772a201d4 /ishtar_common/models_common.py | |
parent | 5dfc4cfee03d1bb8cc85f7148d9e3ce3344a4e30 (diff) | |
download | Ishtar-810cf25c85cd44fe1610db4792693983a81c8818.tar.bz2 Ishtar-810cf25c85cd44fe1610db4792693983a81c8818.zip |
Geodata: gpkg, shp import
Diffstat (limited to 'ishtar_common/models_common.py')
-rw-r--r-- | ishtar_common/models_common.py | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index 104e46100..12f8b0507 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -23,6 +23,7 @@ from django.conf import settings from django.contrib.auth.models import User, Group from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.fields import GenericForeignKey +from django.db import migrations as db_migrations from django.contrib.gis.db import models from django.contrib.gis.geos import Point from django.contrib.gis.gdal.error import GDALException @@ -2078,13 +2079,15 @@ GEOJSON_POINT_TPL = { } -class GeoVectorData(models.Model): +class GeoVectorData(Imported): name = models.TextField(_("Name"), default=_("Default")) source_content_type = models.ForeignKey( ContentType, related_name="content_type_geovectordata", on_delete=models.CASCADE ) source_id = models.PositiveIntegerField() source = GenericForeignKey("source_content_type", "source_id") + import_key = models.TextField(_("Import key"), blank=True, null=True, + help_text=_("Use this for update imports")) origin = models.ForeignKey( GeoOriginType, blank=True, @@ -2141,6 +2144,7 @@ class GeoVectorData(models.Model): class Meta: verbose_name = _("Geographic - Vector data") verbose_name_plural = _("Geographic - Vector data") + unique_together = ("source_content_type", "source_id", "import_key") def __str__(self): name = self.name @@ -2197,11 +2201,20 @@ class GeoVectorData(models.Model): if dim == 2: return [None, None] return [None, None, None] - point = geom.transform(srid, clone=True) + + point = geom + if not srid or srid != geom.srid: + point = geom.transform(srid, clone=True) + x, y = point.x, point.y + # Coordinates are reversed - should be fixed on Django 3.2 + if srid in (4326, 4979): + x, y = y, x + else: + x, y = point.x, point.y if dim == 2: - coordinates = [point.x, point.y] + coordinates = [x, y] else: - coordinates = [point.x, point.y, point.z] + coordinates = [x, y, point.z] if not rounded: return coordinates return [round(coord, rounded) for coord in coordinates] @@ -2362,6 +2375,24 @@ class GeoVectorData(models.Model): return self._geojson_serialize("multi_polygon") return "{}" + @classmethod + def migrate_srid(cls, new_srid): + fields = ( + "point_2d", "point_3d", "multi_points", "multi_line", "multi_polygon", + ) + with connection.cursor() as cursor: + for name in fields: + cursor.execute( + "UPDATE ishtar_common_geovectordata SET %s=ST_SetSRID(%s, %s);", + [name, name, new_srid] + ) + items = cls.objects.all() + for item in items: + for name in fields: + getattr(item, name).transform(new_srid) + item._no_geo_check = True + item.save() + post_save.connect(post_save_geodata, sender=GeoVectorData) |