mirror of https://gitlab.com/pamhyr/pamhyr2
Model, Geometry: Save and load geometry into SQLite.
parent
a4acbdce16
commit
1921889309
|
|
@ -38,12 +38,15 @@ class SQLModel(SQL):
|
|||
fn = lambda sql: self.execute(
|
||||
sql,
|
||||
fetch_one = False,
|
||||
commit = True
|
||||
commit = False
|
||||
)
|
||||
|
||||
for cls in self._sub_classes:
|
||||
requests = cls._sql_create(fn)
|
||||
|
||||
self.commit()
|
||||
return True
|
||||
|
||||
def _create(self):
|
||||
raise NotImplementedMethodeError(self, self._create)
|
||||
|
||||
|
|
@ -51,13 +54,14 @@ class SQLModel(SQL):
|
|||
fn = lambda sql: self.execute(
|
||||
sql,
|
||||
fetch_one = False,
|
||||
commit = True
|
||||
commit = False
|
||||
)
|
||||
|
||||
ok = True
|
||||
for cls in self._sub_classes:
|
||||
ok &= cls._sql_update(fn, version)
|
||||
|
||||
self.commit()
|
||||
return ok
|
||||
|
||||
def _update(self):
|
||||
|
|
@ -67,13 +71,14 @@ class SQLModel(SQL):
|
|||
fn = lambda sql: self.execute(
|
||||
sql,
|
||||
fetch_one = False,
|
||||
commit = True
|
||||
commit = False
|
||||
)
|
||||
|
||||
ok = True
|
||||
for obj in objs:
|
||||
ok &= obj._sql_save(fn)
|
||||
|
||||
self.commit()
|
||||
return ok
|
||||
|
||||
def _save(self):
|
||||
|
|
|
|||
|
|
@ -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,7 +6,9 @@ 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,
|
||||
_type:str = "", reach = None,
|
||||
|
|
@ -15,6 +17,12 @@ class Profile(object):
|
|||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -127,6 +127,9 @@ class RiverReach(Edge, SQLSubModel):
|
|||
def _sql_load(cls, execute, data = None):
|
||||
reachs = []
|
||||
|
||||
if data is None:
|
||||
data = {}
|
||||
|
||||
table = execute("SELECT id, name, enable, node1, node2 FROM river_reach")
|
||||
for row in table:
|
||||
# Update id counter
|
||||
|
|
@ -141,6 +144,11 @@ class RiverReach(Edge, SQLSubModel):
|
|||
|
||||
new = cls(id, name, node1, node2, status = data["status"])
|
||||
new.enable(enable = enable)
|
||||
|
||||
data["reach"] = id
|
||||
data["parent"] = new
|
||||
new._reach = Reach._sql_load(execute, data)
|
||||
|
||||
reachs.append(new)
|
||||
|
||||
return reachs
|
||||
|
|
@ -157,7 +165,8 @@ class RiverReach(Edge, SQLSubModel):
|
|||
)
|
||||
execute(sql)
|
||||
|
||||
return True
|
||||
objs = [self._reach]
|
||||
return self._save_submodel(execute, objs, data)
|
||||
|
||||
@property
|
||||
def reach(self):
|
||||
|
|
|
|||
|
|
@ -231,8 +231,9 @@ class SQL(object):
|
|||
value = value.replace("'", "'")
|
||||
return value
|
||||
|
||||
@timer
|
||||
def execute(self, cmd, fetch_one = True, commit = False):
|
||||
print(f"[SQL] {cmd}")
|
||||
#print(f"[SQL] {cmd}")
|
||||
res = self._cur.execute(cmd)
|
||||
|
||||
if commit:
|
||||
|
|
|
|||
Loading…
Reference in New Issue