Skip to content

Commit

Permalink
Changed the multiple grid intersections to sqlite processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
rpachaly committed Jan 13, 2025
1 parent 2796685 commit 383dc09
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 56 deletions.
10 changes: 3 additions & 7 deletions flo2d/flo2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4112,13 +4112,9 @@ def show_levee_elev_tool(self):
n_levee_directions_total += n_levee_directions
n_fail_features_total += n_fail_features

# This for loop corrects the elevation
for no in sorted(dlg_levee_elev.methods):
if no == 1:
# processing for a spatial selection range is enabled on this type
dlg_levee_elev.methods[no](rangeReq=ranger)
else:
dlg_levee_elev.methods[no]()
# This for loop corrects the elevation
for no in sorted(dlg_levee_elev.methods):
dlg_levee_elev.methods[no]()

inctime = time.time()
print("%s seconds to process levee features" % round(inctime - starttime, 2))
Expand Down
159 changes: 112 additions & 47 deletions flo2d/flo2d_tools/elevation_correctors.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,57 +195,122 @@ def elevation_from_points(self, search_buffer):
continue
self.gutils.con.commit()

def elevation_from_lines(self, regionReq=None):
cur = self.gutils.con.cursor()
# use moving window
qryIndex = "CREATE INDEX if not exists leveeDataUser_Line_FID ON levee_data (user_line_fid);"
cur.execute(qryIndex)
self.gutils.con.commit()
def elevation_from_lines(self):
"""
This function corrects the levcrest on levees.
"""

grid = self.lyrs.data["grid"]["qlyr"]
levee_data = self.lyrs.data["levee_data"]["qlyr"]
levee_spatial_index = QgsSpatialIndex(levee_data.getFeatures())
user_levee_lines = self.lyrs.data["user_levee_lines"]["qlyr"]

levee_data.startEditing()
for user_levee in user_levee_lines.getFeatures():
user_levee_fid = user_levee["fid"]
elev = user_levee["elev"]
cor = user_levee["correction"]

if elev == NULL and cor == NULL:
continue
elif elev != NULL and cor != NULL:
self.gutils.execute(f"""
UPDATE levee_data AS ld
SET levcrest = (
SELECT ull.correction + ull.elev
FROM user_levee_lines AS ull
JOIN grid AS g
ON ST_Intersects(CastAutomagic(g.geom), CastAutomagic(ull.geom))
WHERE ull.fid = {user_levee_fid} AND g.fid = ld.grid_fid
)
WHERE EXISTS (
SELECT 1
FROM user_levee_lines AS ull
JOIN grid AS g
ON ST_Intersects(CastAutomagic(g.geom), CastAutomagic(ull.geom))
WHERE ull.fid = {user_levee_fid} AND g.fid = ld.grid_fid
);
""")
elif elev != NULL and cor == NULL:
self.gutils.execute(f"""
UPDATE levee_data AS ld
SET levcrest = (
SELECT ull.elev
FROM user_levee_lines AS ull
JOIN grid AS g
ON ST_Intersects(CastAutomagic(g.geom), CastAutomagic(ull.geom))
WHERE ull.fid = {user_levee_fid} AND g.fid = ld.grid_fid
)
WHERE EXISTS (
SELECT 1
FROM user_levee_lines AS ull
JOIN grid AS g
ON ST_Intersects(CastAutomagic(g.geom), CastAutomagic(ull.geom))
WHERE ull.fid = {user_levee_fid} AND g.fid = ld.grid_fid
);
""")
elif elev == NULL and cor != NULL:
self.gutils.execute(f"""
UPDATE levee_data AS ld
SET levcrest = levcrest + (
SELECT ull.correction
FROM user_levee_lines AS ull
JOIN grid AS g
ON ST_Intersects(CastAutomagic(g.geom), CastAutomagic(ull.geom))
WHERE ull.fid = {user_levee_fid} AND g.fid = ld.grid_fid
)
WHERE EXISTS (
SELECT 1
FROM user_levee_lines AS ull
JOIN grid AS g
ON ST_Intersects(CastAutomagic(g.geom), CastAutomagic(ull.geom))
WHERE ull.fid = {user_levee_fid} AND g.fid = ld.grid_fid
);
""")
else:
continue

for regionReqSwatch in (
gridRegionGenerator(self.gutils, grid, regionPadding=0, showProgress=True)
if regionReq is None
else [regionReq]
):

# Added an intermediate loop that iterates over each grid to get all features inside the grid element
for grid_feature in grid.getFeatures(regionReqSwatch):
grid_geometry = grid_feature.geometry()
bbox = grid_geometry.boundingBox()
levees_intersected = levee_spatial_index.intersects(bbox)
request = QgsFeatureRequest().setFilterFids(levees_intersected)

for feat in levee_data.getFeatures(request):
levee_data_fid = feat["fid"]

user_levee_lines_fid = feat["user_line_fid"]
user_levee_lines_data = self.gutils.execute(f"SELECT elev, correction FROM user_levee_lines WHERE fid "
f"= '{user_levee_lines_fid}'").fetchall()[0]
elev = user_levee_lines_data[0]
cor = user_levee_lines_data[1]

levee_data.updateFeature(feat)

if elev == NULL and cor == NULL:
continue
elif elev != NULL and cor != NULL:
val = elev + cor
self.gutils.execute(f"UPDATE levee_data SET levcrest = {val} WHERE fid = {levee_data_fid};")
elif elev != NULL and cor == NULL:
val = elev
self.gutils.execute(f"UPDATE levee_data SET levcrest = {val} WHERE fid = {levee_data_fid};")
elif elev == NULL and cor != NULL:
val = cor
self.gutils.execute(f"UPDATE levee_data SET levcrest = levcrest + {val} WHERE fid = {levee_data_fid};")
else:
continue
levee_data.commitChanges()
# cur = self.gutils.con.cursor()
# # use moving window
# qryIndex = "CREATE INDEX if not exists leveeDataUser_Line_FID ON levee_data (user_line_fid);"
# cur.execute(qryIndex)
# self.gutils.con.commit()
#
# grid = self.lyrs.data["grid"]["qlyr"]
# levee_data = self.lyrs.data["levee_data"]["qlyr"]
# levee_spatial_index = QgsSpatialIndex(levee_data.getFeatures())
#
# levee_data.startEditing()
#
# # Added an intermediate loop that iterates over each grid to get all features inside the grid element
# for grid_feature in grid.getFeatures():
# grid_geometry = grid_feature.geometry()
# bbox = grid_geometry.boundingBox()
# levees_intersected = levee_spatial_index.intersects(bbox)
# request = QgsFeatureRequest().setFilterFids(levees_intersected)
#
# for feat in levee_data.getFeatures(request):
# levee_data_fid = feat["fid"]
#
# user_levee_lines_fid = feat["user_line_fid"]
# user_levee_lines_data = self.gutils.execute(f"SELECT elev, correction FROM user_levee_lines WHERE fid "
# f"= '{user_levee_lines_fid}'").fetchall()[0]
# elev = user_levee_lines_data[0]
# cor = user_levee_lines_data[1]
#
# levee_data.updateFeature(feat)
#
# if elev == NULL and cor == NULL:
# continue
# elif elev != NULL and cor != NULL:
# val = elev + cor
# self.gutils.execute(f"UPDATE levee_data SET levcrest = {val} WHERE fid = {levee_data_fid};")
# elif elev != NULL and cor == NULL:
# val = elev
# self.gutils.execute(f"UPDATE levee_data SET levcrest = {val} WHERE fid = {levee_data_fid};")
# elif elev == NULL and cor != NULL:
# val = cor
# self.gutils.execute(f"UPDATE levee_data SET levcrest = levcrest + {val} WHERE fid = {levee_data_fid};")
# else:
# continue
# levee_data.commitChanges()

@timer
def elevation_from_polygons(self):
Expand Down
4 changes: 2 additions & 2 deletions flo2d/gui/dlg_levee_elev.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ def elev_from_points(self):
finally:
self.corrector.clear_filter()

def elev_from_lines(self, rangeReq=None):
self.corrector.elevation_from_lines(regionReq=rangeReq)
def elev_from_lines(self):
self.corrector.elevation_from_lines()

def elev_from_polys(self):
try:
Expand Down

0 comments on commit 383dc09

Please sign in to comment.