From b0e94b114b5c298cc007f6a02cbccabc1ea3f2ef Mon Sep 17 00:00:00 2001 From: Alireza Adli Date: Mon, 26 Feb 2024 10:52:39 -0500 Subject: [PATCH] Add a service that clip based on several overlays --- .../clip_with_several_overlays.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 services_scripts/clip_with_several_overlays.py diff --git a/services_scripts/clip_with_several_overlays.py b/services_scripts/clip_with_several_overlays.py new file mode 100644 index 0000000..442fea7 --- /dev/null +++ b/services_scripts/clip_with_several_overlays.py @@ -0,0 +1,35 @@ +from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.analysis import QgsNativeAlgorithms +import processing +from services_scripts.basic_functions import create_folders + +clipped_layers_folder = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/clip_all' +num_clipped_layers = 21 +create_folders(clipped_layers_folder, num_clipped_layers) + +# Set the path to QGIS installation +QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.34.1/apps/qgis", True) + +# Initialize QGIS application +qgs = QgsApplication([], False) +qgs.initQgis() + +# Add native algorithms provider +QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) + +input_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/uniteevaluationfonciere/uniteevaluationfonciere.shp' + +for each in range(num_clipped_layers): + clipping_layer = f'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/divided_all/layer_{each}/layer_{each}.shp' + clipped_layer = f'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/clip_all/layer_{each}/layer_{each}.shp' + + params = {'INPUT': input_layer, 'OVERLAY': clipping_layer, 'FILTER_EXPRESSION': '', 'FILTER_EXTENT': None, 'OUTPUT': clipped_layer} + processing.run("native:clip", params) + + # Creating spatian index for each new layer + create_spatial_indext_params = {'INPUT': clipped_layer, 'OUTPUT': 'Output'} + processing.run("native:createspatialindex", create_spatial_indext_params) + +print("Clipping is completed.") + +qgs.exitQgis() \ No newline at end of file