mirror of
https://gitlab.com/pamhyr/pamhyr2.git
synced 2026-10-30 03:20:41 +01:00
geometry: Clean up some code and continue refactoring of geometry view.
This commit is contained in:
1 parent
fa9d95c7e7
commit
0d08ca3889
7 files changed
+217
-721
No files matched your search
@@ -42,4 +42,4 @@ class PointAC(Point):
|
||||
Returns:
|
||||
Euclidean distance between the two points
|
||||
"""
|
||||
return dist((p1.a(), p1.c()), (p2.a(), p2.c()))
|
||||
return dist((p1.a, p1.c), (p2.a, p2.c))
|
||||
@@ -47,7 +47,7 @@ class PointXYZ(Point):
|
||||
Returns:
|
||||
True if at least one coordinate is as np.nan
|
||||
"""
|
||||
return pd_is_na(self.x()) or pd_is_na(self.y()) or pd_is_na(self.z())
|
||||
return pd_is_na(self.x) or pd_is_na(self.y) or pd_is_na(self.z)
|
||||
|
||||
@staticmethod
|
||||
def distance(p1, p2):
|
||||
@@ -60,4 +60,4 @@ class PointXYZ(Point):
|
||||
Returns:
|
||||
Euclidean 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))
|
||||
@@ -1,13 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
class Profile(object):
|
||||
def __init__(self, kp:float = 0.0, name:str = ""):
|
||||
def __init__(self, kp:float = 0.0, name:str = "",
|
||||
code1: int = 0, code2: int = 0,
|
||||
_type:str = ""):
|
||||
super(Profile, self).__init__()
|
||||
|
||||
self._num = int(num)
|
||||
self._code1 = int(code1)
|
||||
self._code2 = int(code2)
|
||||
self._kp = float(kp)
|
||||
self._name = str(name)
|
||||
|
||||
self._points: List[Point] = []
|
||||
|
||||
self._profile_type = _type
|
||||
|
||||
@property
|
||||
def num(self):
|
||||
"""
|
||||
@@ -20,6 +28,34 @@ class Profile(object):
|
||||
def num(self, value: int):
|
||||
self._num = int(value)
|
||||
|
||||
@property
|
||||
def code1(self):
|
||||
"""
|
||||
Returns:
|
||||
Interpolation code 1.
|
||||
"""
|
||||
return self._code1
|
||||
|
||||
@code1.setter
|
||||
def code1(self, value: int):
|
||||
self._code1 = int(value)
|
||||
|
||||
@property
|
||||
def code2(self):
|
||||
"""
|
||||
Returns:
|
||||
Interpolation code 2.
|
||||
"""
|
||||
return self._code2
|
||||
|
||||
@code2.setter
|
||||
def code2(self, value: int):
|
||||
self._code2 = int(value)
|
||||
|
||||
@property
|
||||
def nb_points(self):
|
||||
return len(self._points)
|
||||
|
||||
@property
|
||||
def kp(self):
|
||||
"""
|
||||
@@ -41,5 +77,17 @@ class Profile(object):
|
||||
return self._name
|
||||
|
||||
@name.setter
|
||||
def name(self, other: str):
|
||||
self._name = other
|
||||
def name(self, value: str):
|
||||
self._name = value.strip()
|
||||
|
||||
@property
|
||||
def profile_type(self):
|
||||
"""
|
||||
Returns:
|
||||
Profile type.
|
||||
"""
|
||||
return self._profile_type
|
||||
|
||||
@type.setter
|
||||
def profile_type(self, value: str):
|
||||
self._profile_type = value
|
||||
@@ -8,7 +8,8 @@ from Model.Geometry.Profile import Profile
|
||||
from Model.Geometry.PointXYZ import PointXYZ
|
||||
|
||||
class ProfileXYZ(Profile):
|
||||
def __init__(self, num: int = 0, code1: int = 0, code2: int = 0,
|
||||
def __init__(self, num: int = 0,
|
||||
code1: int = 0, code2: int = 0,
|
||||
kp: float = 0., name: str = ""):
|
||||
"""ProfileXYZ constructor
|
||||
|
||||
@@ -16,7 +17,6 @@ class ProfileXYZ(Profile):
|
||||
num: The number of this profile
|
||||
code1: The interpolation code 1
|
||||
code2: The interpolation code 2
|
||||
nb_points: Number of points
|
||||
kp: Kilometer point
|
||||
name: The name of profile
|
||||
|
||||
@@ -27,46 +27,14 @@ class ProfileXYZ(Profile):
|
||||
num = num,
|
||||
name = name,
|
||||
kp = kp,
|
||||
code1 = code1, code2 = code2,
|
||||
_type = "XYZ",
|
||||
)
|
||||
|
||||
self._code1 = int(code1)
|
||||
self._code2 = int(code2)
|
||||
self._nb_points = int(nb_points)
|
||||
self._list_points: List[PointXYZ] = []
|
||||
|
||||
def __repr__(self):
|
||||
df = pd.DataFrame(columns=["X", "Y", "Z", "Name"],
|
||||
data=[[p.x, p.y, p.z, p.name] for p in self._list_points])
|
||||
return f"\n{self.header}\n{df}"
|
||||
|
||||
|
||||
@property
|
||||
def code1(self):
|
||||
"""
|
||||
Returns:
|
||||
Interpolation code 1.
|
||||
"""
|
||||
return self._code1
|
||||
|
||||
@code1.setter
|
||||
def code1(self, value: int):
|
||||
self._code1 = int(value)
|
||||
|
||||
@property
|
||||
def code2(self):
|
||||
"""
|
||||
Returns:
|
||||
Interpolation code 2.
|
||||
"""
|
||||
return self._code2
|
||||
|
||||
@code2.setter
|
||||
def code2(self, value: int):
|
||||
self._code2 = int(value)
|
||||
|
||||
@property
|
||||
def nb_points(self):
|
||||
return self._nb_points
|
||||
return f"\nProfileXYZ : {self.name}\n{df}"
|
||||
|
||||
@property
|
||||
def header(self):
|
||||
@@ -76,7 +44,7 @@ class ProfileXYZ(Profile):
|
||||
"""
|
||||
return np.array(
|
||||
[self._num, self._code1, self._code2,
|
||||
self._nb_points, self._kp, self._name]
|
||||
self.nb_points(), self._kp, self._name]
|
||||
)
|
||||
|
||||
def import_points(self, list_points: list):
|
||||
@@ -167,3 +135,44 @@ class ProfileXYZ(Profile):
|
||||
raise TypeError(
|
||||
f"{list_index} is instance of unexpected type '{type(list_index)}'"
|
||||
)
|
||||
|
||||
def filter_isnan(self, lst):
|
||||
"""Returns the input list without 'nan' element
|
||||
|
||||
Args:
|
||||
lst: The list to filter
|
||||
|
||||
Returns:
|
||||
The list without 'nan'
|
||||
"""
|
||||
return [x for x in lst if not np.isnan(x)]
|
||||
|
||||
def x(self):
|
||||
return [point.x for point in self._list_points]
|
||||
|
||||
def y(self):
|
||||
return [point.y for point in self._list_points]
|
||||
|
||||
def z(self):
|
||||
return [point.z for point in self._list_points]
|
||||
|
||||
def names(self):
|
||||
return [point.name for point in self._list_points]
|
||||
|
||||
def x_max(self):
|
||||
return max(self.filter_isnan(self.x))
|
||||
|
||||
def x_min(self):
|
||||
return min(self.filter_isnan(self.x))
|
||||
|
||||
def y_max(self):
|
||||
return max(self.filter_isnan(self.y))
|
||||
|
||||
def y_min(self):
|
||||
return min(self.filter_isnan(self.y))
|
||||
|
||||
def z_max(self):
|
||||
return max(self.filter_isnan(self.z))
|
||||
|
||||
def z_min(self):
|
||||
return min(self.filter_isnan(self.z))
|
||||
+80
-15
@@ -15,26 +15,47 @@ from Model.Except import FileFormatError, exception_message_box
|
||||
class Reach:
|
||||
def __init__(self, edge):
|
||||
self._edge = edge
|
||||
self._list_profiles: List[Profile] = []
|
||||
self._profiles: List[Profile] = []
|
||||
|
||||
# Copy/Paste
|
||||
self.__list_copied_profiles = []
|
||||
|
||||
def __repr__(self):
|
||||
return f"\n===== Début liste des profils ======> {np.array(self._list_profiles)}" \
|
||||
return f"\n===== Début liste des profils ======> {np.array(self._profiles)}" \
|
||||
f"\n<====== Fin liste des profils ====="
|
||||
|
||||
def profile(self, i):
|
||||
"""Returns profile at index i
|
||||
|
||||
Args:
|
||||
i: The index of profile
|
||||
|
||||
Returns:
|
||||
The profile at index i
|
||||
"""
|
||||
if i in self._profiles:
|
||||
return self._profiles[i]
|
||||
|
||||
return None
|
||||
|
||||
@property
|
||||
def profiles(self):
|
||||
return self._profiles.copy()
|
||||
|
||||
@property
|
||||
def number_profiles(self):
|
||||
"""
|
||||
Returns:
|
||||
Number of profiles
|
||||
"""
|
||||
return len(self._list_profiles)
|
||||
return len(self._profiles)
|
||||
|
||||
def get_geometry(self) -> List[Profile]:
|
||||
"""
|
||||
Returns:
|
||||
The profiles list.
|
||||
"""
|
||||
return self._list_profiles
|
||||
return self._profiles
|
||||
|
||||
def get_profile_i(self, i: int) -> Profile:
|
||||
"""
|
||||
@@ -45,11 +66,11 @@ class Reach:
|
||||
The profile at index i.
|
||||
"""
|
||||
try:
|
||||
return self._list_profiles[i]
|
||||
return self._profiles[i]
|
||||
except IndexError:
|
||||
raise IndexError(f"Invalid profile index: {i}")
|
||||
|
||||
def add_XYZ(self):
|
||||
def add_profile(self, index):
|
||||
"""Add a new profile at the end of profiles list
|
||||
|
||||
Returns:
|
||||
@@ -58,7 +79,8 @@ class Reach:
|
||||
nb_profile = self.number_profiles
|
||||
profile = ProfileXYZ()
|
||||
profile.num = nb_profile + 1
|
||||
self._list_profiles.append(profile)
|
||||
self._profiles.insert(profile, index + 1)
|
||||
# self._update_profile_numbers()
|
||||
|
||||
def _update_profile_numbers(self):
|
||||
"""Update profiles index
|
||||
@@ -79,7 +101,7 @@ class Reach:
|
||||
Nothing.
|
||||
"""
|
||||
profile = ProfileXYZ()
|
||||
self._list_profiles.insert(index, profile)
|
||||
self._profiles.insert(index, profile)
|
||||
self._update_profile_numbers()
|
||||
|
||||
def delete(self, list_index: list):
|
||||
@@ -95,26 +117,50 @@ class Reach:
|
||||
if list_index:
|
||||
indices = sorted(list(set(list_index)), reverse=True)
|
||||
for idx in indices:
|
||||
# if idx < len(self._list_profiles) :
|
||||
# if idx < len(self._profiles) :
|
||||
try:
|
||||
self._list_profiles.pop(idx)
|
||||
self._profiles.pop(idx)
|
||||
self._update_profile_numbers()
|
||||
except IndexError:
|
||||
print(f"Invalid profile index: {idx}")
|
||||
except TypeError:
|
||||
if isinstance(list_index, int):
|
||||
self._list_profiles.pop(list_index)
|
||||
self._profiles.pop(list_index)
|
||||
self._update_profile_numbers()
|
||||
else:
|
||||
raise TypeError(f"{list_index} is instance of unexpected type '{type(list_index)}'")
|
||||
|
||||
def _sort(self, is_reversed: bool = False):
|
||||
self._list_profiles = sorted(
|
||||
self._list_profiles,
|
||||
self._profiles = sorted(
|
||||
self._profiles,
|
||||
key=lambda profile: profile.kp(),
|
||||
reverse=is_reversed
|
||||
)
|
||||
|
||||
def z_min(self):
|
||||
"""List of z min for each profile
|
||||
|
||||
Returns:
|
||||
List of z min for each profile
|
||||
"""
|
||||
return [profile.z_min() for profile in self._data.profiles]
|
||||
|
||||
def z_max(self):
|
||||
"""List of z max for each profile
|
||||
|
||||
Returns:
|
||||
List of z max for each profile
|
||||
"""
|
||||
return [profile.z_max() for profile in self._data.profiles]
|
||||
|
||||
def kp(self):
|
||||
"""List of profiles kp
|
||||
|
||||
Returns:
|
||||
List of profiles kp
|
||||
"""
|
||||
return [profile.kp for profile in self._data.profiles]
|
||||
|
||||
def sort_ascending(self):
|
||||
"""Sort profiles by increasing KP
|
||||
|
||||
@@ -144,7 +190,7 @@ class Reach:
|
||||
def paste(self):
|
||||
if self.__list_copied_profiles:
|
||||
for profile in self.__list_copied_profiles:
|
||||
self._list_profiles.append(profile)
|
||||
self._profiles.append(profile)
|
||||
|
||||
def import_geometry(self, file_path_name: str):
|
||||
"""Import a geometry from file (.ST or .st)
|
||||
@@ -162,7 +208,7 @@ class Reach:
|
||||
for ind, profile in enumerate(list_profile):
|
||||
prof = ProfileXYZ(*list_header[ind])
|
||||
prof.import_points(profile)
|
||||
self._list_profiles.append(prof)
|
||||
self._profiles.append(prof)
|
||||
self._update_profile_numbers()
|
||||
except FileNotFoundError as e:
|
||||
print(e)
|
||||
@@ -225,3 +271,22 @@ class Reach:
|
||||
|
||||
print("****** Fichier {} lu et traité en {} secondes *******".format(self.file_st, time() - t0))
|
||||
return list_profile, list_header
|
||||
|
||||
|
||||
# TODO: Move this function to model reach
|
||||
def export_reach(self, filename):
|
||||
with open(f"{filename}", "w") as file_st:
|
||||
for index in range(len(self._profiles)):
|
||||
profile = self._profiles[index]
|
||||
|
||||
for v in profile.header:
|
||||
file_st.write(f"{v:>6}")
|
||||
file_st.write("\n")
|
||||
|
||||
for point in self._data.profile[index_pro].points:
|
||||
for i in [point.x, point.y, point.z, point.name]:
|
||||
file_st.write(f"{i:>13.4f}")
|
||||
file_st.write("\n")
|
||||
|
||||
file_st.write(" 999.9990 999.9990 999.9990")
|
||||
file_st.write("\n")
|
||||
Reference in new issue
Block a user