From e528ba5278d34b94ff279dc6d4ccb8a97e20099b Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr> Date: Tue, 23 Apr 2024 11:50:35 +0200 Subject: [PATCH] Geometry: Add shapefile import file format. --- requirements.txt | 1 + src/Model/Geometry/Reach.py | 54 +++++++++++++++++++++++++++++++++- src/View/Geometry/Translate.py | 2 ++ src/View/Geometry/Window.py | 1 + 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 1f0a3194..a4393842 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ pyinstaller>=5.11.0 shapely>=2.0.1 lxml>=4.9.3 platformdirs>=4.2.0 +pyshp>=2.3.1 diff --git a/src/Model/Geometry/Reach.py b/src/Model/Geometry/Reach.py index 448bc622..1b7748fe 100644 --- a/src/Model/Geometry/Reach.py +++ b/src/Model/Geometry/Reach.py @@ -16,7 +16,9 @@ # -*- coding: utf-8 -*- +import os import logging +import shapefile import numpy as np from time import time @@ -533,8 +535,58 @@ class Reach(SQLSubModel): # Import/Export - @timer def import_geometry(self, file_path_name: str): + if file_path_name.endswith(".st"): + return self.import_geometry_st(file_path_name) + elif file_path_name.endswith(".shp"): + return self.import_geometry_shp(file_path_name) + else: + return [] + + @timer + def import_geometry_shp(self, file_path_name: str): + sf = shapefile.Reader( + os.path.abspath(file_path_name) + ) + + if sf.shapeType != shapefile.POLYLINEZ: + raise FileFormatError( + file_path_name, + "Shapefile is not a POLYLINEZ" + ) + + profiles = [] + + for i in range(len(sf)): + s_profile = sf.shape(i) + z = s_profile.z + + ind = 0 + points = [] + for point in s_profile.points: + points.append([ + point[0], + point[1], + z[ind] + ]) + + ind += 1 + + prof = ProfileXYZ( + kp=round(points[0][0]), + reach=self, status=self._status + ) + prof.import_points(points) + + profiles.append(prof) + + self.profiles = profiles + self.profiles + self._update_profile_numbers() + + return profiles + + @timer + def import_geometry_st(self, file_path_name: str): """Import a geometry from file (.ST or .st) Args: diff --git a/src/View/Geometry/Translate.py b/src/View/Geometry/Translate.py index 7b607d83..23034fd5 100644 --- a/src/View/Geometry/Translate.py +++ b/src/View/Geometry/Translate.py @@ -36,6 +36,8 @@ class GeometryTranslate(MainTranslate): "Geometry", "File mage geometry (*.ST *.st)") self._dict["file_m"] = _translate( "Geometry", "File mage meshed geometry (*.M *.m)") + self._dict["file_shp"] = _translate( + "Geometry", "Shapefile (*.SHP *.shp)") self._dict["file_all"] = _translate("Geometry", "All file (*)") self._dict["cross_section"] = _translate("Geometry", "cross-section") diff --git a/src/View/Geometry/Window.py b/src/View/Geometry/Window.py index d8bcc06b..3631ff10 100644 --- a/src/View/Geometry/Window.py +++ b/src/View/Geometry/Window.py @@ -241,6 +241,7 @@ class GeometryWindow(PamhyrWindow): file_types = [ self._trad["file_st"], self._trad["file_m"], + self._trad["file_shp"], self._trad["file_all"], ] -- GitLab