-
Pierre-Antoine Rouby authoredd0acface
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# PointXYZ.py -- Pamhyr
# Copyright (C) 2023-2024 INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
from math import dist
import numpy as np
from Model.Tools.PamhyrDB import SQLSubModel
from Model.Geometry.Point import Point
class PointXYZ(Point, SQLSubModel):
_sub_classes = []
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0,
name: str = "", profile=None, status=None):
super(PointXYZ, self).__init__(
name=name, profile=profile, status=status)
self._x = float(x)
self._y = float(y)
self._z = float(z)
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE geometry_pointXYZ(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
ind INTEGER NOT NULL,
name TEXT,
x INTEGER NOT NULL,
y INTEGER NOT NULL,
z INTEGER NOT NULL,
profile INTEGER NOT NULL,
sl INTEGER,
FOREIGN KEY(profile) REFERENCES geometry_profileXYZ(id),
FOREIGN KEY(sl) REFERENCES sedimentary_layer(id)
)
""")
return cls._create_submodel(execute)
@classmethod
def _db_update(cls, execute, version):
cls._update_submodel(execute, version)
major, minor, release = version.strip().split(".")
if major == minor == "0":
if int(release) < 2:
execute(
"""
ALTER TABLE geometry_pointXYZ
ADD COLUMN sl INTEGER
REFERENCES sedimentary_layer(id)
"""
)
return True
@classmethod
def _db_load(cls, execute, data=None):
status = data["status"]
profile = data["profile"]
table = execute(
"SELECT ind, name, x, y, z, sl " +
"FROM geometry_pointXYZ " +
f"WHERE profile = {profile.id}"
)
# Fill points list with new point
for row in table:
ind = row[0]
name = row[1]
x = row[2]
y = row[3]
z = row[4]
sl = row[5]
new = cls(
name=name,
x=x, y=y, z=z,
profile=profile,
status=status
)
if sl == -1 or sl is None:
new._sl = None
else:
new._sl = next(
filter(
lambda s: s.id == sl,
data["sediment_layers_list"].sediment_layers
)
)
yield ind, new
def _db_save(self, execute, data=None):
profile = data["profile"]
ind = data["ind"]
sl = self._sl.id if self._sl is not None else -1
sql = (
"INSERT INTO " +
"geometry_pointXYZ(ind, name, x, y, z, profile, sl) " +
"VALUES (" +
f"{ind}, '{self._db_format(self._name)}', " +
f"{self.x}, {self.y}, {self.z}, " +
f"{profile.id}, {sl}" +
")"
)
execute(sql)
return True
@classmethod
def from_data(cls, header, data):
point = None
try:
if len(header) == 0:
point = cls(
*data
)
else:
valid_header = {'name', 'x', 'y', 'z', 'profile'}
d = {}
for i, v in enumerate(data):
h = header[i].strip().lower().split(' ')[0]
if h in valid_header:
d[h] = v
point = cls(**d)
except Exception as e:
raise ClipboardFormatError(header, data)
return point
def __repr__(self):
return f"({self._x}, {self._y}, {self._z}, {self._name})"
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = float(value)
self._status.modified()
@property
def y(self):
return self._y
@y.setter
def y(self, value):
self._y = float(value)
self._status.modified()
@property
def z(self):
return self._z
@z.setter
def z(self, value):
self._z = float(value)
self._status.modified()
def is_nan(self):
"""
Returns:
True if at least one coordinate is as np.nan
"""
return (np.isnan(self.x) or
np.isnan(self.y) or
np.isnan(self.z))
def dist(self, p2):
return PointXYZ.distance(self, p2)
@staticmethod
def distance(p1, p2):
"""Euclidean distance between p1 and p2.
Args:
p1: A XYZ Point
p2: A XYZ Point
Returns:
Euclidean distance between the two points
"""
return dist((p1.x, p1.y, p1.z), (p2.x, p2.y, p2.z))