From 07a9e59df0b3cbcdc5bbd84e89b3d496b3658467 Mon Sep 17 00:00:00 2001 From: Tim Sutton Date: Wed, 13 Nov 2024 01:39:25 +0000 Subject: [PATCH] Fix issue with generating large, complex areas like fiji --- geest/core/tasks/study_area.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/geest/core/tasks/study_area.py b/geest/core/tasks/study_area.py index 3c14e78e..0930b4e9 100644 --- a/geest/core/tasks/study_area.py +++ b/geest/core/tasks/study_area.py @@ -633,7 +633,7 @@ def create_and_save_grid(self, geom: QgsGeometry, bbox: QgsRectangle) -> None: total_cells = ((x_max - x_min) // step) * ((y_max - y_min) // step) cell_count = 0 features_per_batch = 10000 - progress_log_interval = 1000 + progress_log_interval = 10000 log_message("Preparing spatial index...") # Spatial index for efficient intersection checks @@ -671,18 +671,18 @@ def create_and_save_grid(self, geom: QgsGeometry, bbox: QgsRectangle) -> None: # Commit in batches if len(feature_batch) >= features_per_batch: provider.addFeatures(feature_batch) - gpkg_layer.updateExtents() + # gpkg_layer.updateExtents() feature_batch.clear() log_message( f"Committed {feature_id} features to {grid_layer_name}." ) - + gpkg_layer.commitChanges() # Reset provider to free memory - del provider - gpkg_layer = QgsVectorLayer( - gpkg_layer_path, grid_layer_name, "ogr" - ) - provider = gpkg_layer.dataProvider() + # del provider + # gpkg_layer = QgsVectorLayer( + # gpkg_layer_path, grid_layer_name, "ogr" + # ) + # provider = gpkg_layer.dataProvider() # Log progress periodically cell_count += 1 @@ -696,10 +696,13 @@ def create_and_save_grid(self, geom: QgsGeometry, bbox: QgsRectangle) -> None: # Commit any remaining features if feature_batch: provider.addFeatures(feature_batch) - gpkg_layer.updateExtents() - log_message( - f"Final commit: {feature_id} features written to {grid_layer_name}." - ) + gpkg_layer.commitChanges() + gpkg_layer.updateExtents() + provider.forceReload() + del gpkg_layer + log_message( + f"Final commit: {feature_id} features written to {grid_layer_name}." + ) except Exception as e: log_message(f"Error during grid creation: {str(e)}", level=Qgis.Critical)