Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Levees two sides #1539

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions flo2d/flo2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4053,22 +4053,7 @@ def show_levee_elev_tool(self):
n_fail_features_total = 0

starttime = time.time()
# Create a virtual field to add the process data
# This is used to avoid duplicate of processed data when running the moving window
levees = self.lyrs.data["levee_data"]["qlyr"]
field_name = 'processed_data'

# Check if the virtual field was already created, if so, delete it
existing_field_index = levees.fields().indexFromName(field_name)

if existing_field_index != -1: # Field exists, delete it
levees.dataProvider().deleteAttributes([existing_field_index])
levees.updateFields()

# Add a virtual field
field = QgsField(field_name, QVariant.Int)
levees.dataProvider().addAttributes([field])
levees.updateFields()

# This for loop creates the attributes in the levee_dat
for (
Expand Down Expand Up @@ -4099,25 +4084,6 @@ def show_levee_elev_tool(self):
print("in clear loop")
dletes = "Cell - Direction\n---------------\n"

# fix the ones that the correction was not correctly applied
badCorrectionQry = """
UPDATE levee_data
SET levcrest = levcrest + (
SELECT correction
FROM user_levee_lines
WHERE user_levee_lines.fid = levee_data.user_line_fid
)
WHERE processed_data IS NULL;
"""
self.gutils.con.execute(badCorrectionQry)
self.gutils.con.commit()

# Delete the processed field on the user_levee_lines
existing_field_index = levees.fields().indexFromName(field_name)
if existing_field_index != -1: # Field exists, delete it
levees.dataProvider().deleteAttributes([existing_field_index])
levees.updateFields()

# delete duplicate elements with the same direction and elevation too
qryIndex = "CREATE INDEX if not exists levee_dataFIDGRIDFIDLDIRLEVCEST ON levee_data (fid, grid_fid, ldir, levcrest);"
self.gutils.con.execute(qryIndex)
Expand Down
57 changes: 27 additions & 30 deletions flo2d/flo2d_tools/elevation_correctors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections import defaultdict

from PyQt5.QtWidgets import QMessageBox
from qgis._core import QgsMessageLog
from qgis._core import QgsMessageLog, QgsSpatialIndex
from qgis.analysis import QgsZonalStatistics
from qgis.core import (
NULL,
Expand Down Expand Up @@ -204,8 +204,7 @@ def elevation_from_lines(self, regionReq=None):

grid = self.lyrs.data["grid"]["qlyr"]
levee_data = self.lyrs.data["levee_data"]["qlyr"]
# qry = "UPDATE levee_data SET levcrest = ? WHERE user_line_fid = ?;"
# qryElevNullPlusCorrect = "UPDATE levee_data SET levcrest = levcrest + ? WHERE user_line_fid = ?;"
levee_spatial_index = QgsSpatialIndex(levee_data.getFeatures())

levee_data.startEditing()

Expand All @@ -215,40 +214,38 @@ def elevation_from_lines(self, regionReq=None):
else [regionReq]
):

for feat in levee_data.getFeatures(regionReqSwatch):
levee_data_fid = feat["fid"]
# 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)

# self.gutils.uc.log_info(str(processed_time))
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]
for feat in levee_data.getFeatures(request):
levee_data_fid = feat["fid"]

if feat["processed_data"] == 1:
feat["processed_data"] = 2
else:
feat["processed_data"] = 1
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)
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:
if feat["processed_data"] == 2:
if elev == NULL and cor == NULL:
continue
else:
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()
else:
continue
levee_data.commitChanges()

@timer
def elevation_from_polygons(self):
Expand Down
Loading