From 383dc090f6d6f7a9c6317d8a65c999bb986fc822 Mon Sep 17 00:00:00 2001 From: rpachaly <39889306+rpachaly@users.noreply.github.com> Date: Mon, 13 Jan 2025 19:04:16 -0300 Subject: [PATCH] Changed the multiple grid intersections to sqlite processing. --- flo2d/flo2d.py | 10 +- flo2d/flo2d_tools/elevation_correctors.py | 159 +++++++++++++++------- flo2d/gui/dlg_levee_elev.py | 4 +- 3 files changed, 117 insertions(+), 56 deletions(-) diff --git a/flo2d/flo2d.py b/flo2d/flo2d.py index 883b1469..1405ced5 100644 --- a/flo2d/flo2d.py +++ b/flo2d/flo2d.py @@ -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)) diff --git a/flo2d/flo2d_tools/elevation_correctors.py b/flo2d/flo2d_tools/elevation_correctors.py index a5bd2577..b89ae390 100644 --- a/flo2d/flo2d_tools/elevation_correctors.py +++ b/flo2d/flo2d_tools/elevation_correctors.py @@ -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): diff --git a/flo2d/gui/dlg_levee_elev.py b/flo2d/gui/dlg_levee_elev.py index bdf85f5f..ce5fb8f2 100644 --- a/flo2d/gui/dlg_levee_elev.py +++ b/flo2d/gui/dlg_levee_elev.py @@ -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: