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(
|
fn = lambda sql: self.execute(
|
||||||
sql,
|
sql,
|
||||||
fetch_one = False,
|
fetch_one = False,
|
||||||
commit = True
|
commit = False
|
||||||
)
|
)
|
||||||
|
|
||||||
for cls in self._sub_classes:
|
for cls in self._sub_classes:
|
||||||
requests = cls._sql_create(fn)
|
requests = cls._sql_create(fn)
|
||||||
|
|
||||||
|
self.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
def _create(self):
|
def _create(self):
|
||||||
raise NotImplementedMethodeError(self, self._create)
|
raise NotImplementedMethodeError(self, self._create)
|
||||||
|
|
||||||
|
|
@ -51,13 +54,14 @@ class SQLModel(SQL):
|
||||||
fn = lambda sql: self.execute(
|
fn = lambda sql: self.execute(
|
||||||
sql,
|
sql,
|
||||||
fetch_one = False,
|
fetch_one = False,
|
||||||
commit = True
|
commit = False
|
||||||
)
|
)
|
||||||
|
|
||||||
ok = True
|
ok = True
|
||||||
for cls in self._sub_classes:
|
for cls in self._sub_classes:
|
||||||
ok &= cls._sql_update(fn, version)
|
ok &= cls._sql_update(fn, version)
|
||||||
|
|
||||||
|
self.commit()
|
||||||
return ok
|
return ok
|
||||||
|
|
||||||
def _update(self):
|
def _update(self):
|
||||||
|
|
@ -67,13 +71,14 @@ class SQLModel(SQL):
|
||||||
fn = lambda sql: self.execute(
|
fn = lambda sql: self.execute(
|
||||||
sql,
|
sql,
|
||||||
fetch_one = False,
|
fetch_one = False,
|
||||||
commit = True
|
commit = False
|
||||||
)
|
)
|
||||||
|
|
||||||
ok = True
|
ok = True
|
||||||
for obj in objs:
|
for obj in objs:
|
||||||
ok &= obj._sql_save(fn)
|
ok &= obj._sql_save(fn)
|
||||||
|
|
||||||
|
self.commit()
|
||||||
return ok
|
return ok
|
||||||
|
|
||||||
def _save(self):
|
def _save(self):
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,8 @@ class PointXYZ(Point, SQLSubModel):
|
||||||
def _sql_create(cls, execute):
|
def _sql_create(cls, execute):
|
||||||
execute("""
|
execute("""
|
||||||
CREATE TABLE geometry_pointXYZ(
|
CREATE TABLE geometry_pointXYZ(
|
||||||
id INTEGER NOT NULL PRIMARY KEY,
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
|
ind INTEGER NOT NULL,
|
||||||
name TEXT,
|
name TEXT,
|
||||||
x INTEGER NOT NULL,
|
x INTEGER NOT NULL,
|
||||||
y INTEGER NOT NULL,
|
y INTEGER NOT NULL,
|
||||||
|
|
@ -41,9 +42,54 @@ class PointXYZ(Point, SQLSubModel):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _sql_load(cls, execute, data = None):
|
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):
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,9 @@ from Model.Geometry.Point import Point
|
||||||
from Model.Except import NotImplementedMethodeError
|
from Model.Except import NotImplementedMethodeError
|
||||||
|
|
||||||
class Profile(object):
|
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 = "",
|
kp:float = 0.0, name:str = "",
|
||||||
code1:int = 0, code2:int = 0,
|
code1:int = 0, code2:int = 0,
|
||||||
_type:str = "", reach = None,
|
_type:str = "", reach = None,
|
||||||
|
|
@ -15,6 +17,12 @@ class Profile(object):
|
||||||
|
|
||||||
self._status = status
|
self._status = status
|
||||||
|
|
||||||
|
if id == -1:
|
||||||
|
self.id = Profile._id_cnt
|
||||||
|
Profile._id_cnt += 1
|
||||||
|
else:
|
||||||
|
self.id = id
|
||||||
|
|
||||||
self._num = int(num)
|
self._num = int(num)
|
||||||
self._code1 = int(code1)
|
self._code1 = int(code1)
|
||||||
self._code2 = int(code2)
|
self._code2 = int(code2)
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ class ProfileXYZ(Profile, SQLSubModel):
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
|
id: int = -1,
|
||||||
name: str = "",
|
name: str = "",
|
||||||
kp: float = 0.,
|
kp: float = 0.,
|
||||||
reach = None,
|
reach = None,
|
||||||
|
|
@ -37,6 +38,7 @@ class ProfileXYZ(Profile, SQLSubModel):
|
||||||
Nothing.
|
Nothing.
|
||||||
"""
|
"""
|
||||||
super(ProfileXYZ, self).__init__(
|
super(ProfileXYZ, self).__init__(
|
||||||
|
id = id,
|
||||||
num = num,
|
num = num,
|
||||||
name = name,
|
name = name,
|
||||||
kp = kp,
|
kp = kp,
|
||||||
|
|
@ -61,20 +63,72 @@ class ProfileXYZ(Profile, SQLSubModel):
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
cls._create_submodel(execute)
|
return cls._create_submodel(execute)
|
||||||
return True
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _sql_update(cls, execute, version):
|
def _sql_update(cls, execute, version):
|
||||||
cls._update_submodel(execute, version)
|
return cls._update_submodel(execute, version)
|
||||||
return True
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _sql_load(cls, execute, data = None):
|
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):
|
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
|
@classmethod
|
||||||
def from_data(cls, header, data):
|
def from_data(cls, header, data):
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ class Reach(SQLSubModel):
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, status=None, parent=None):
|
def __init__(self, status=None, parent=None):
|
||||||
|
self.id = parent.id
|
||||||
self._status = status
|
self._status = status
|
||||||
self._parent = parent
|
self._parent = parent
|
||||||
self._profiles: List[Profile] = []
|
self._profiles: List[Profile] = []
|
||||||
|
|
@ -46,17 +47,15 @@ class Reach(SQLSubModel):
|
||||||
|
|
||||||
new._profiles = ProfileXYZ._sql_load(
|
new._profiles = ProfileXYZ._sql_load(
|
||||||
execute,
|
execute,
|
||||||
data = {
|
data = data
|
||||||
"status": data["status"],
|
|
||||||
"reach": new,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return new
|
return new
|
||||||
|
|
||||||
def _sql_save(self, execute, data = None):
|
def _sql_save(self, execute, data = None):
|
||||||
cls._save_submodel(execute, data)
|
objs = self._profiles
|
||||||
return True
|
|
||||||
|
return self._save_submodel(execute, objs, data)
|
||||||
|
|
||||||
def profile(self, i):
|
def profile(self, i):
|
||||||
"""Returns profile at index i
|
"""Returns profile at index i
|
||||||
|
|
|
||||||
|
|
@ -127,6 +127,9 @@ class RiverReach(Edge, SQLSubModel):
|
||||||
def _sql_load(cls, execute, data = None):
|
def _sql_load(cls, execute, data = None):
|
||||||
reachs = []
|
reachs = []
|
||||||
|
|
||||||
|
if data is None:
|
||||||
|
data = {}
|
||||||
|
|
||||||
table = execute("SELECT id, name, enable, node1, node2 FROM river_reach")
|
table = execute("SELECT id, name, enable, node1, node2 FROM river_reach")
|
||||||
for row in table:
|
for row in table:
|
||||||
# Update id counter
|
# Update id counter
|
||||||
|
|
@ -141,6 +144,11 @@ class RiverReach(Edge, SQLSubModel):
|
||||||
|
|
||||||
new = cls(id, name, node1, node2, status = data["status"])
|
new = cls(id, name, node1, node2, status = data["status"])
|
||||||
new.enable(enable = enable)
|
new.enable(enable = enable)
|
||||||
|
|
||||||
|
data["reach"] = id
|
||||||
|
data["parent"] = new
|
||||||
|
new._reach = Reach._sql_load(execute, data)
|
||||||
|
|
||||||
reachs.append(new)
|
reachs.append(new)
|
||||||
|
|
||||||
return reachs
|
return reachs
|
||||||
|
|
@ -157,7 +165,8 @@ class RiverReach(Edge, SQLSubModel):
|
||||||
)
|
)
|
||||||
execute(sql)
|
execute(sql)
|
||||||
|
|
||||||
return True
|
objs = [self._reach]
|
||||||
|
return self._save_submodel(execute, objs, data)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def reach(self):
|
def reach(self):
|
||||||
|
|
|
||||||
|
|
@ -231,8 +231,9 @@ class SQL(object):
|
||||||
value = value.replace("'", "'")
|
value = value.replace("'", "'")
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@timer
|
||||||
def execute(self, cmd, fetch_one = True, commit = False):
|
def execute(self, cmd, fetch_one = True, commit = False):
|
||||||
print(f"[SQL] {cmd}")
|
#print(f"[SQL] {cmd}")
|
||||||
res = self._cur.execute(cmd)
|
res = self._cur.execute(cmd)
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue