diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-03-16 11:36:15 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:21:00 +0100 |
commit | 3e0a73401c1307baf8813bb87eb9f94f8e3a44f1 (patch) | |
tree | c04bf0b7c237eed0920799a2330e11dd05661737 /ishtar_common/models_common.py | |
parent | 6803fa0cb2d70eb90591ec318d9733ddea387315 (diff) | |
download | Ishtar-3e0a73401c1307baf8813bb87eb9f94f8e3a44f1.tar.bz2 Ishtar-3e0a73401c1307baf8813bb87eb9f94f8e3a44f1.zip |
Geodata: fix templates with geovectordata
Diffstat (limited to 'ishtar_common/models_common.py')
-rw-r--r-- | ishtar_common/models_common.py | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index 414e63c55..104e46100 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -2062,6 +2062,22 @@ class GeoProviderType(HierarchicalType): ) +GEOJSON_POINT_TPL = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [] + }, + "properties": { + } + } + ] +} + + class GeoVectorData(models.Model): name = models.TextField(_("Name"), default=_("Default")) source_content_type = models.ForeignKey( @@ -2272,7 +2288,7 @@ class GeoVectorData(models.Model): def _geojson_serialize(self, geom_attr): if not hasattr(self, geom_attr): - return "" + return "{}" geojson = serialize( "geojson", self.__class__.objects.filter(pk=self.pk), @@ -2280,6 +2296,9 @@ class GeoVectorData(models.Model): fields=("name",), ) geojson_dct = json.loads(geojson) + return self._geojson_base_serialize(geojson_dct) + + def _geojson_base_serialize(self, geojson_dct): profile = get_current_profile() precision = profile.point_precision @@ -2311,6 +2330,38 @@ class GeoVectorData(models.Model): def multi_polygon_geojson(self): return self._geojson_serialize("multi_polygon") + @property + def geometry_type(self): + if self.x or self.y or self.z or self.point_2d or self.point_3d: + return "POINT" + if self.multi_line: + return "MULTILINE" + if self.multi_points: + return "MULTIPOINTS" + if self.multi_polygon: + return "MULTIPOLYGON" + return "" + + @property + def geojson(self): + if self.x or self.y or self.z: + geo = GEOJSON_POINT_TPL.copy() + geo["features"][0]["geometry"]["coordinates"] = [ + self.cached_x, self.cached_y + ] + return self._geojson_base_serialize(geo) + if self.point_2d: + return self._geojson_serialize("point_2d") + if self.point_3d: + return self._geojson_serialize("point_3d") + if self.multi_line: + return self._geojson_serialize("multi_line") + if self.multi_points: + return self._geojson_serialize("multi_points") + if self.multi_polygon: + return self._geojson_serialize("multi_polygon") + return "{}" + post_save.connect(post_save_geodata, sender=GeoVectorData) |