Commit 6dc96b86 authored by Pierre-Antoine Rouby's avatar Pierre-Antoine Rouby
Browse files

Merge branch 'shape' into v0.0.8

Showing with 96 additions and 2 deletions
+96 -2
...@@ -11,3 +11,4 @@ pyinstaller>=5.11.0 ...@@ -11,3 +11,4 @@ pyinstaller>=5.11.0
shapely>=2.0.1 shapely>=2.0.1
lxml>=4.9.3 lxml>=4.9.3
platformdirs>=4.2.0 platformdirs>=4.2.0
pyshp>=2.3.1
...@@ -11,3 +11,4 @@ pyinstaller>=5.11.0 ...@@ -11,3 +11,4 @@ pyinstaller>=5.11.0
shapely>=2.0.1 shapely>=2.0.1
lxml>=4.9.3 lxml>=4.9.3
platformdirs>=4.2.0 platformdirs>=4.2.0
pyshp>=2.3.1
...@@ -192,6 +192,22 @@ class PointXYZ(Point, SQLSubModel): ...@@ -192,6 +192,22 @@ class PointXYZ(Point, SQLSubModel):
def dist(self, p2): def dist(self, p2):
return PointXYZ.distance(self, p2) return PointXYZ.distance(self, p2)
def dist_2d(self, p2):
return PointXYZ.distance_2d(self, p2)
@staticmethod
def distance_2d(p1, p2):
"""Euclidean distance between p1 and p2.
Args:
p1: A XYZ Point
p2: A XYZ Point
Returns:
Euclidean 2D distance between the two points
"""
return dist((p1.x, p1.y), (p2.x, p2.y))
@staticmethod @staticmethod
def distance(p1, p2): def distance(p1, p2):
"""Euclidean distance between p1 and p2. """Euclidean distance between p1 and p2.
...@@ -201,6 +217,6 @@ class PointXYZ(Point, SQLSubModel): ...@@ -201,6 +217,6 @@ class PointXYZ(Point, SQLSubModel):
p2: A XYZ Point p2: A XYZ Point
Returns: Returns:
Euclidean distance between the two points Euclidean 3D distance between the two points
""" """
return dist((p1.x, p1.y, p1.z), (p2.x, p2.y, p2.z)) return dist((p1.x, p1.y, p1.z), (p2.x, p2.y, p2.z))
...@@ -16,7 +16,9 @@ ...@@ -16,7 +16,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
import logging import logging
import shapefile
import numpy as np import numpy as np
from time import time from time import time
...@@ -533,8 +535,58 @@ class Reach(SQLSubModel): ...@@ -533,8 +535,58 @@ class Reach(SQLSubModel):
# Import/Export # Import/Export
@timer
def import_geometry(self, file_path_name: str): 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(
reach=self, status=self._status
)
prof.import_points(points)
profiles.append(prof)
self.profiles = profiles + self.profiles
self._update_profile_numbers()
self._recompute_kp()
return profiles
@timer
def import_geometry_st(self, file_path_name: str):
"""Import a geometry from file (.ST or .st) """Import a geometry from file (.ST or .st)
Args: Args:
...@@ -762,3 +814,24 @@ class Reach(SQLSubModel): ...@@ -762,3 +814,24 @@ class Reach(SQLSubModel):
except Exception as e: except Exception as e:
logger_exception(e) logger_exception(e)
return 0 return 0
def _recompute_kp(self, offset=0.0):
self._recompute_kp_no_gl(offset=offset)
def _recompute_kp_no_gl(self, offset=0.0):
profiles = iter(self.profiles)
previous = next(profiles)
previous.kp = offset
for profile in profiles:
prev_points = previous.points
curr_points = profile.points
dist = (
prev_points[0].dist_2d(curr_points[0]) +
prev_points[-1].dist_2d(curr_points[-1])
) / 2.0
profile.kp = previous.kp + dist
previous = profile
...@@ -36,6 +36,8 @@ class GeometryTranslate(MainTranslate): ...@@ -36,6 +36,8 @@ class GeometryTranslate(MainTranslate):
"Geometry", "File mage geometry (*.ST *.st)") "Geometry", "File mage geometry (*.ST *.st)")
self._dict["file_m"] = _translate( self._dict["file_m"] = _translate(
"Geometry", "File mage meshed geometry (*.M *.m)") "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["file_all"] = _translate("Geometry", "All file (*)")
self._dict["cross_section"] = _translate("Geometry", "cross-section") self._dict["cross_section"] = _translate("Geometry", "cross-section")
......
...@@ -241,6 +241,7 @@ class GeometryWindow(PamhyrWindow): ...@@ -241,6 +241,7 @@ class GeometryWindow(PamhyrWindow):
file_types = [ file_types = [
self._trad["file_st"], self._trad["file_st"],
self._trad["file_m"], self._trad["file_m"],
self._trad["file_shp"],
self._trad["file_all"], self._trad["file_all"],
] ]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment