Skip to content

Commit

Permalink
Fix regression in study area
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Sep 30, 2024
1 parent 219f2f2 commit b8d435e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 117 deletions.
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# .pre-commit-config.yaml
repos:
- repo: https://github.com/psf/black
rev: 24.4.0 # Replace with the latest version of black
hooks:
- id: black
name: black
language_version: python3
# Restrict black to only the `geest` directory
additional_dependencies: []
args: [geest]

29 changes: 8 additions & 21 deletions geest/core/study_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
self.gpkg_path: str = os.path.join(
self.working_dir, "study_area", "study_area.gpkg"
)

self.counter: int = 0
# Remove the GeoPackage if it already exists to start with a clean state
if os.path.exists(self.gpkg_path):
try:
Expand Down Expand Up @@ -226,9 +226,6 @@ def process_singlepart_geometry(
bbox: QgsRectangle = self.grid_aligned_bbox(geom.boundingBox())

# Create a feature for the aligned bounding box
study_area_feature: QgsFeature = QgsFeature()
study_area_feature.setGeometry(QgsGeometry.fromRect(bbox))
study_area_feature.setAttributes([area_name])
# Always save the study area bounding boxes regardless of mode
self.save_to_geopackage(
layer_name="study_area_bboxes",
Expand All @@ -244,14 +241,10 @@ def process_singlepart_geometry(
geom.transform(transform)

# Create a feature for the original part
study_area_polygon: QgsFeature = QgsFeature()
study_area_polygon.setGeometry(geom)
study_area_polygon.setAttributes([area_name])
# Always save the study area bounding boxes regardless of mode
self.save_to_geopackage(
layer_name="study_area_polygons", geom=geom, area_name=normalized_name
)

# Process the geometry based on the selected mode
if self.mode == "vector":
QgsMessageLog.logMessage(
Expand Down Expand Up @@ -329,15 +322,16 @@ def grid_aligned_bbox(self, bbox: QgsRectangle) -> QgsRectangle:
* 100
)

y_min -= 100 # Offset by 100m to ensure the grid covers the entire geometry
y_max += 100 # Offset by 100m to ensure the grid covers the entire geometry
x_min -= 100 # Offset by 100m to ensure the grid covers the entire geometry
x_max += 100 # Offset by 100m to ensure the grid covers the entire geometry

# Return the aligned bbox in the output CRS
return QgsRectangle(x_min, y_min, x_max, y_max)

def save_to_geopackage(
self,
features: List[QgsFeature],
layer_name: str,
fields: List[QgsField],
geometry_type: QgsWkbTypes,
self, layer_name: str, geom: QgsGeometry, area_name: str
) -> None:
"""
Save features to GeoPackage. Create or append the layer as necessary.
Expand Down Expand Up @@ -386,9 +380,7 @@ def append_to_layer(
level=Qgis.Critical,
)

def create_layer_if_not_exists(
self, layer_name: str, fields: List[QgsField], geometry_type: QgsWkbTypes
) -> None:
def create_layer_if_not_exists(self, layer_name: str) -> None:
"""
Create a new layer in the GeoPackage if it doesn't already exist.
Expand Down Expand Up @@ -433,11 +425,6 @@ def create_layer_if_not_exists(
QgsVectorFileWriter.CreateOrOverwriteLayer
)

# Convert list of QgsField objects to QgsFields object
qgs_fields = QgsFields()
for field in fields:
qgs_fields.append(field)

# Create a new GeoPackage layer
QgsVectorFileWriter.create(
fileName=self.gpkg_path,
Expand Down
96 changes: 0 additions & 96 deletions geest/gui/tree_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,99 +375,3 @@ def process_leaves(self):
"""
self.start_workflows()
self.queue_manager.start_processing()

# old implementation to be removed soone
# follows below.

### model = self.treeView.model() # Get the model from the tree_view

# Disable the prepare button and show the throbber during processing
self.prepare_button.setEnabled(False)
self.prepare_throbber.setVisible(True)

# Iterate over all items in the tree and find nodes with the 'layer' role
### layer_nodes = []
### row_count = model.rowCount()

### for row in range(row_count):
### parent_index = model.index(row, 0) # Start from the root level
### self.collect_layer_nodes(model, parent_index, layer_nodes)

# Process each 'layer' node
### self.process_each_layer(layer_nodes, 0)

def collect_layer_nodes(self, model, parent_index, layer_nodes):
"""
Recursively collects all 'layer' nodes from the tree starting from the given parent index.
Nodes with the 'layer' role are added to the layer_nodes list.
"""
# Get the item from the model
item = parent_index.internalPointer()

# If the item is a 'layer', add it to the list of nodes to process
if item and getattr(item, "role", None) == "layer":
layer_nodes.append(parent_index)

# Process all child items recursively
for row in range(model.rowCount(parent_index)):
child_index = model.index(row, 0, parent_index)
self.collect_layer_nodes(model, child_index, layer_nodes)

def process_each_layer(self, layer_nodes, index):
"""
Processes each 'layer' node by showing an animated icon, waiting for 2 seconds,
and then removing the animation.
"""
# Base case: if all nodes are processed, enable the button and hide the throbber
if index >= len(layer_nodes):
self.prepare_button.setEnabled(True)
self.prepare_throbber.setVisible(False)
return

# Get the current 'layer' node index
node_index = layer_nodes[index]
model = self.treeView.model()

# Create a QModelIndex for the second column of the current row
second_column_index = model.index(node_index.row(), 1, node_index.parent())

# Set an animated icon (using a QLabel and QMovie to simulate animation)
movie = QMovie(
resources_path("resources", "throbber.gif")
) # Use a valid path to an animated gif
row_height = self.treeView.rowHeight(
node_index
) # Get the height of the current row
movie.setScaledSize(
movie.currentPixmap()
.size()
.scaled(row_height, row_height, Qt.KeepAspectRatio)
)

label = QLabel()
label.setMovie(movie)
movie.start()

# Set the animated icon in the second column of the node
self.treeView.setIndexWidget(second_column_index, label)

# Wait for 2 seconds to simulate processing
QTimer.singleShot(
2000,
lambda: self.finish_processing(
second_column_index, layer_nodes, index, movie
),
)

def finish_processing(self, second_column_index, layer_nodes, index, movie):
"""
Finishes processing by removing the animated icon and proceeds to the next node.
"""
model = self.treeView.model()

# Stop the animation and remove the animated icon
movie.stop()
self.treeView.setIndexWidget(second_column_index, None)

# Move to the next 'layer' node
self.process_each_layer(layer_nodes, index + 1)

0 comments on commit b8d435e

Please sign in to comment.