mirror of
https://gitlab.com/pamhyr/pamhyr2.git
synced 2026-10-30 03:20:41 +01:00
Model, Geometry: Save and load geometry into SQLite.
This commit is contained in:
1 parent
a4acbdce16
commit
1921889309
7 files changed
+143
-21
No files matched your search
@@ -21,7 +21,8 @@ class PointXYZ(Point, SQLSubModel):
|
||||
def _sql_create(cls, execute):
|
||||
execute("""
|
||||
CREATE TABLE geometry_pointXYZ(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
ind INTEGER NOT NULL,
|
||||
name TEXT,
|
||||
x INTEGER NOT NULL,
|
||||
y INTEGER NOT NULL,
|
||||
@@ -41,9 +42,54 @@ class PointXYZ(Point, SQLSubModel):
|
||||
|
||||
@classmethod
|
||||
def _sql_load(cls, execute, data = None):
|
||||
return None
|
||||
points = []
|
||||
|
||||
status = data["status"]
|
||||
profile = data["profile"]
|
||||
|
||||
table = execute(
|
||||
"SELECT ind, name, x, y, z " +
|
||||
"FROM geometry_pointXYZ " +
|
||||
f"WHERE profile = {profile}"
|
||||
)
|
||||
|
||||
# Create points list
|
||||
for _ in table:
|
||||
points.append(None)
|
||||
|
||||
# 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]
|
||||
|
||||
new = cls(
|
||||
name = name,
|
||||
x = x, y = y, z = z,
|
||||
status = status
|
||||
)
|
||||
|
||||
points[ind] = new
|
||||
|
||||
return points
|
||||
|
||||
def _sql_save(self, execute, data = None):
|
||||
profile = data["profile"]
|
||||
ind = data["ind"]
|
||||
|
||||
sql = (
|
||||
"INSERT OR REPLACE INTO " +
|
||||
"geometry_pointXYZ(ind, name, x, y, z, profile) "+
|
||||
"VALUES (" +
|
||||
f"{ind}, '{self._sql_format(self._name)}', " +
|
||||
f"{self.x}, {self.y}, {self.z}, " +
|
||||
f"{profile}" +
|
||||
")"
|
||||
)
|
||||
execute(sql)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
||||
@@ -6,15 +6,23 @@ from Model.Geometry.Point import Point
|
||||
from Model.Except import NotImplementedMethodeError
|
||||
|
||||
class Profile(object):
|
||||
def __init__(self, num: int = 0,
|
||||
_id_cnt = 0
|
||||
|
||||
def __init__(self, id:int = -1, num:int = 0,
|
||||
kp:float = 0.0, name:str = "",
|
||||
code1: int = 0, code2: int = 0,
|
||||
code1:int = 0, code2:int = 0,
|
||||
_type:str = "", reach = None,
|
||||
status = None):
|
||||
super(Profile, self).__init__()
|
||||
|
||||
self._status = status
|
||||
|
||||
if id == -1:
|
||||
self.id = Profile._id_cnt
|
||||
Profile._id_cnt += 1
|
||||
else:
|
||||
self.id = id
|
||||
|
||||
self._num = int(num)
|
||||
self._code1 = int(code1)
|
||||
self._code2 = int(code2)
|
||||
|
||||
@@ -17,6 +17,7 @@ class ProfileXYZ(Profile, SQLSubModel):
|
||||
]
|
||||
|
||||
def __init__(self,
|
||||
id: int = -1,
|
||||
name: str = "",
|
||||
kp: float = 0.,
|
||||
reach = None,
|
||||
@@ -37,6 +38,7 @@ class ProfileXYZ(Profile, SQLSubModel):
|
||||
Nothing.
|
||||
"""
|
||||
super(ProfileXYZ, self).__init__(
|
||||
id = id,
|
||||
num = num,
|
||||
name = name,
|
||||
kp = kp,
|
||||
@@ -61,20 +63,72 @@ class ProfileXYZ(Profile, SQLSubModel):
|
||||
)
|
||||
""")
|
||||
|
||||
cls._create_submodel(execute)
|
||||
return True
|
||||
return cls._create_submodel(execute)
|
||||
|
||||
@classmethod
|
||||
def _sql_update(cls, execute, version):
|
||||
cls._update_submodel(execute, version)
|
||||
return True
|
||||
return cls._update_submodel(execute, version)
|
||||
|
||||
@classmethod
|
||||
def _sql_load(cls, execute, data = None):
|
||||
return None
|
||||
profiles = []
|
||||
status = data["status"]
|
||||
reach = data["reach"]
|
||||
|
||||
table = execute(
|
||||
"SELECT id, name, kp, num, code1, code2 " +
|
||||
"FROM geometry_profileXYZ " +
|
||||
f"WHERE reach = {reach}"
|
||||
)
|
||||
|
||||
for row in table:
|
||||
id = row[0]
|
||||
name = row[1]
|
||||
kp = row[2]
|
||||
num = row[3]
|
||||
code1 = row[4]
|
||||
code2 = row[5]
|
||||
|
||||
new = cls(
|
||||
id=id, num = num,
|
||||
name = name, kp = kp,
|
||||
code1 = code1, code2 = code2,
|
||||
reach = data["parent"],
|
||||
status = status
|
||||
)
|
||||
|
||||
data["profile"] = id
|
||||
new._points = PointXYZ._sql_load(execute, data)
|
||||
|
||||
profiles.append(new)
|
||||
|
||||
return profiles
|
||||
|
||||
def _sql_save(self, execute, data = None):
|
||||
return True
|
||||
ok = True
|
||||
|
||||
sql = (
|
||||
"INSERT OR REPLACE INTO " +
|
||||
"geometry_profileXYZ(id, name, reach, kp, num, code1, code2) "+
|
||||
"VALUES (" +
|
||||
f"{self.id}, '{self._sql_format(self._name)}', " +
|
||||
f"{self.reach.id}, {self.kp}, {self.num}, " +
|
||||
f"{self.code1}, {self.code1}" +
|
||||
")"
|
||||
)
|
||||
execute(sql)
|
||||
|
||||
if data is None:
|
||||
data = {}
|
||||
data["profile"] = self.id
|
||||
|
||||
ind = 0
|
||||
for point in self._points:
|
||||
data["ind"] = ind
|
||||
ok &= point._sql_save(execute, data)
|
||||
ind += 1
|
||||
|
||||
return ok
|
||||
|
||||
@classmethod
|
||||
def from_data(cls, header, data):
|
||||
|
||||
@@ -23,6 +23,7 @@ class Reach(SQLSubModel):
|
||||
]
|
||||
|
||||
def __init__(self, status=None, parent=None):
|
||||
self.id = parent.id
|
||||
self._status = status
|
||||
self._parent = parent
|
||||
self._profiles: List[Profile] = []
|
||||
@@ -46,17 +47,15 @@ class Reach(SQLSubModel):
|
||||
|
||||
new._profiles = ProfileXYZ._sql_load(
|
||||
execute,
|
||||
data = {
|
||||
"status": data["status"],
|
||||
"reach": new,
|
||||
}
|
||||
data = data
|
||||
)
|
||||
|
||||
return new
|
||||
|
||||
def _sql_save(self, execute, data = None):
|
||||
cls._save_submodel(execute, data)
|
||||
return True
|
||||
objs = self._profiles
|
||||
|
||||
return self._save_submodel(execute, objs, data)
|
||||
|
||||
def profile(self, i):
|
||||
"""Returns profile at index i
|
||||
|
||||
Reference in new issue
Block a user