From 087078d7c1b097eaf3327602b2184fa831fccfef Mon Sep 17 00:00:00 2001 From: Mika Hietanen Date: Mon, 11 Nov 2024 10:19:31 +0200 Subject: [PATCH 1/2] Simplify Lipas geometries Features: - Automatically merge MultiLineStrings into one for both 2D- and 3D-imports. If merging end result in a LineSting, convert it to MultiLineString to preserve consistency. Fallback to original geometry. - Use "hasz"-method for 3D-check --- services/management/commands/lipas_import.py | 15 ++++++++++----- services/management/commands/lipas_import_3d.py | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/services/management/commands/lipas_import.py b/services/management/commands/lipas_import.py index 05d2aaa0f..69362afaf 100644 --- a/services/management/commands/lipas_import.py +++ b/services/management/commands/lipas_import.py @@ -181,14 +181,19 @@ def clean_name(name): self._save_geometries(geometries, units_by_lipas_id) def _save_geometries(self, geometries, units_by_lipas_id): - # Add all geometries we found to the db logger.info("Updating geometries in the database...") for lipas_id, geometry in geometries.items(): unit = units_by_lipas_id[lipas_id] - # FIXME: make sports map UI support simplified - # geometries and bring back simplification - # from commit 6cff46e0399fedbbc8266efa5230cd4ccb8a8485 - unit.geometry = geometry + try: + line_geometry = geometry.merged + if isinstance(line_geometry, LineString): + line_geometry = MultiLineString([line_geometry]) + unit.geometry = line_geometry + except TypeError as e: + logger.warning( + f"Failed to merge geometry for unit {unit.name_fi}: {e}", + ) + unit.geometry = geometry unit.save() def _get_types(self): diff --git a/services/management/commands/lipas_import_3d.py b/services/management/commands/lipas_import_3d.py index e74e3c497..24074e0bd 100644 --- a/services/management/commands/lipas_import_3d.py +++ b/services/management/commands/lipas_import_3d.py @@ -1,5 +1,7 @@ import logging +from django.contrib.gis.geos import LineString, MultiLineString + from services.management.commands import lipas_import logger = logging.getLogger(__name__) @@ -27,7 +29,18 @@ def _save_geometries(self, geometries, units_by_lipas_id): for lipas_id, geometry in geometries.items(): unit = units_by_lipas_id[lipas_id] if self._has_z_coordinate(geometry): - unit.geometry_3d = geometry + try: + line_geometry = geometry.merged + if isinstance(line_geometry, LineString): + line_geometry = MultiLineString([line_geometry]) + unit.geometry_3d = line_geometry + if len(line_geometry) == 0: + unit.geometry_3d = geometry + except TypeError as e: + logger.warning( + f"Failed to merge 3D geometry for unit {unit.name_fi}: {e}", + ) + unit.geometry_3d = geometry unit.save() else: logger.warning( @@ -41,4 +54,4 @@ def _has_z_coordinate(self, geometry): """ Check if a geometry has a Z-coordinate (3D). """ - return "Z" in geometry.ewkt + return geometry.hasz From 595f4805da3251eb4958c6113f1df92f30e838f6 Mon Sep 17 00:00:00 2001 From: Pauliina Ilmanen Date: Thu, 14 Nov 2024 10:59:54 +0200 Subject: [PATCH 2/2] Update setup version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 26b93f54e..cbcbd7a2b 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="smbackend", - version="241022", + version="241114", license="AGPLv3", packages=find_packages(), include_package_data=True,