diff options
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) |