From ec647b88f4898cbeacab97e4b3bcfdaa71935539 Mon Sep 17 00:00:00 2001 From: Alireza Adli Date: Wed, 28 Feb 2024 15:04:14 -0500 Subject: [PATCH] Clean indentation of services --- services_scripts/adding_area_field.py | 26 +++++++++++++ services_scripts/basic_functions.py | 24 +++++++++++- services_scripts/clip_layer.py | 25 +++++++++--- .../clip_with_several_overlays.py | 30 +++++++++++---- services_scripts/count_records.py | 9 +++-- services_scripts/create_spatial_index.py | 12 ++++-- services_scripts/delete_duplicates.py | 17 ++++++--- services_scripts/delete_field.py | 19 ++++++---- services_scripts/fix_geometries.py | 12 ++++-- services_scripts/intersection.py | 27 +++++++++---- .../join_attributes_by_location.py | 17 +++++++-- services_scripts/load_layer.py | 4 +- services_scripts/merge_layers.py | 12 ++++-- services_scripts/multipart_to_singleparts.py | 12 ++++-- services_scripts/split_layer_by_expression.py | 38 ++++++++++++++----- 15 files changed, 217 insertions(+), 67 deletions(-) diff --git a/services_scripts/adding_area_field.py b/services_scripts/adding_area_field.py index e69de29..3b5eab2 100644 --- a/services_scripts/adding_area_field.py +++ b/services_scripts/adding_area_field.py @@ -0,0 +1,26 @@ +from qgis.core import QgsProject, QgsExpression, QgsExpressionContext, edit, QgsField, QgsExpressionContextUtils + +# Import necessary modules +from qgis.core import QgsProject, QgsVectorLayer, QgsField + +# Define the layer name +layer_name = "your_layer_name" + +# Get the layer by name +layer = QgsProject.instance().mapLayersByName(layer_name)[0] + +# Start editing +layer.startEditing() + +# Add a new field to store the area +field_name = 'Area' +field_index = layer.fields().indexFromName(field_name) +if field_index == -1: + layer.dataProvider().addAttributes([QgsField(field_name, QgsField.QVariant.Double)]) + layer.updateFields() + +# Commit changes +layer.commitChanges() + + + diff --git a/services_scripts/basic_functions.py b/services_scripts/basic_functions.py index 9fb9536..1b6c9bb 100644 --- a/services_scripts/basic_functions.py +++ b/services_scripts/basic_functions.py @@ -1,4 +1,6 @@ -import os, glob +import os +import glob +import time def create_folders(directory, num_folders): @@ -25,8 +27,26 @@ def create_folders(directory, num_folders): # .shp files from each subfolder def find_shp_files(root_folder): shp_files = [] - for foldername, _, _ in sorted(os.walk(root_folder)): # Sort folders alphabetically + # Sort folders alphabetically + for foldername, _, _ in sorted(os.walk(root_folder)): for filename in sorted(glob.glob(os.path.join(foldername, '*.shp'))): new_file_name = filename.replace('\\', r'/') shp_files.append(new_file_name) return shp_files + + +# Creates a progress bar for a for loop +def progress_bar(iterable, length=20): + total = len(iterable) + progress = 0 + start_time = time.time() + for item in iterable: + yield item + progress += 1 + percent = progress / total + filled_length = int(length * percent) + bar = '=' * filled_length + '-' * (length - filled_length) + elapsed_time = time.time() - start_time + print(f'\r[{bar}] {progress}/{total} ({percent:.0%}) - ' + f'Elapsed time: {elapsed_time:.2f}s', end='', flush=True) + print() diff --git a/services_scripts/clip_layer.py b/services_scripts/clip_layer.py index 1a7a44a..80e347d 100644 --- a/services_scripts/clip_layer.py +++ b/services_scripts/clip_layer.py @@ -1,4 +1,4 @@ -from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.core import QgsApplication from qgis.analysis import QgsNativeAlgorithms import processing @@ -12,13 +12,26 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -input_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_fixed_05/py_fixes_05.shp' -clipping_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/montreal_boundary|layername=Montreal_boundary' -clipped_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/clipped_nrcan_10/clipped_nrcan_10.shp' +input_layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/python_fixed_05/py_fixes_05.shp' +clipping_layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'input_data/montreal_boundary|layername=Montreal_boundary' +clipped_layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/clipped_nrcan_10/clipped_nrcan_10.shp' -params = {'INPUT': input_layer, 'OVERLAY': clipping_layer, 'FILTER_EXPRESSION': '', 'FILTER_EXTENT': None, 'OUTPUT': clipped_layer} +params = {'INPUT': input_layer, + 'OVERLAY': clipping_layer, + 'FILTER_EXPRESSION': '', + 'FILTER_EXTENT': None, + 'OUTPUT': clipped_layer} processing.run("native:clip", params) print("Clipping is completed.") -qgs.exitQgis() \ No newline at end of file +qgs.exitQgis() diff --git a/services_scripts/clip_with_several_overlays.py b/services_scripts/clip_with_several_overlays.py index 442fea7..3bb717f 100644 --- a/services_scripts/clip_with_several_overlays.py +++ b/services_scripts/clip_with_several_overlays.py @@ -1,9 +1,12 @@ -from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.core import QgsApplication 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' +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) @@ -17,13 +20,26 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -input_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/uniteevaluationfonciere/uniteevaluationfonciere.shp' +input_layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'input_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' + clipping_layer = \ + f'C:/Users/a_adli/PycharmProjects/' \ + f'hydroquebec_archetype_gispy/data/' \ + f'output_data/divided_all/layer_{each}/layer_{each}.shp' + clipped_layer = \ + f'C:/Users/a_adli/PycharmProjects/' \ + f'hydroquebec_archetype_gispy/data/' \ + f'output_data/clip_all/layer_{each}/layer_{each}.shp' - params = {'INPUT': input_layer, 'OVERLAY': clipping_layer, 'FILTER_EXPRESSION': '', 'FILTER_EXTENT': None, 'OUTPUT': clipped_layer} + 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 @@ -32,4 +48,4 @@ for each in range(num_clipped_layers): print("Clipping is completed.") -qgs.exitQgis() \ No newline at end of file +qgs.exitQgis() diff --git a/services_scripts/count_records.py b/services_scripts/count_records.py index f293992..ed73686 100644 --- a/services_scripts/count_records.py +++ b/services_scripts/count_records.py @@ -1,4 +1,4 @@ -from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.core import QgsApplication from qgis.analysis import QgsNativeAlgorithms from load_layer import load_layer @@ -13,9 +13,12 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/clipped_nrcan_10/clipped_nrcan_10.shp' +layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/clipped_nrcan_10/clipped_nrcan_10.shp' layer, layer_name = load_layer(layer, 'NRCan') print(f'{layer_name} data count: {layer.featureCount()}') -qgs.exitQgis() \ No newline at end of file +qgs.exitQgis() diff --git a/services_scripts/create_spatial_index.py b/services_scripts/create_spatial_index.py index f12b033..675727a 100644 --- a/services_scripts/create_spatial_index.py +++ b/services_scripts/create_spatial_index.py @@ -1,4 +1,4 @@ -from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.core import QgsApplication from qgis.analysis import QgsNativeAlgorithms import processing @@ -11,12 +11,16 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/fixed_geoindex_3/fix_geo.shp' +layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/merged_all_delete_columns/merge_divisions.shp' -params = {'INPUT': layer, 'OUTPUT': 'Output'} +params = {'INPUT': layer, + 'OUTPUT': 'Output'} processing.run("native:createspatialindex", params) print("Creating spatial index is completed.") -qgs.exitQgis() \ No newline at end of file +qgs.exitQgis() diff --git a/services_scripts/delete_duplicates.py b/services_scripts/delete_duplicates.py index 1ee4a27..191c45b 100644 --- a/services_scripts/delete_duplicates.py +++ b/services_scripts/delete_duplicates.py @@ -1,4 +1,4 @@ -from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.core import QgsApplication from qgis.analysis import QgsNativeAlgorithms import processing @@ -12,10 +12,17 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/merged_all_delete_columns/merge_divisions.shp' +input_layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/qgis_spatial_pariwiseunitNrcan_geo|layername=spatial_with_geo' +output_layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/empty/delete_duplicates_02.shp' -params = {'INPUT':'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/history/aEndeavor/qgis_spatial_pariwiseunitNrcan_geo|layername=spatial_with_geo', - 'OUTPUT':'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/history/aEndeavor/empty/delete_duplicates_02.shp'} +params = {'INPUT': input_layer, + 'OUTPUT': output_layer} processing.run("native:deleteduplicategeometries", params) -qgs.exitQgis() \ No newline at end of file +qgs.exitQgis() diff --git a/services_scripts/delete_field.py b/services_scripts/delete_field.py index 3ba3401..cbed6b2 100644 --- a/services_scripts/delete_field.py +++ b/services_scripts/delete_field.py @@ -21,21 +21,24 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/merged_all_delete_columns/merge_divisions.shp' -the_layer = load_layer(layer_path, 'Merged Pairwise Clipped')[0] +layer_path = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/merged_all_delete_columns/merge_divisions.shp' +output_layer = load_layer(layer_path, 'Merged Pairwise Clipped')[0] -column_name = "layer" # and 'path' +column_name = 'path' # and 'layer' # Start editing -with edit(the_layer): +with edit(output_layer): # Get the index of the column to delete - idx = the_layer.fields().indexFromName(column_name) + idx = output_layer.fields().indexFromName(column_name) # Delete the field - the_layer.deleteAttribute(idx) + output_layer.deleteAttribute(idx) # Update layer fields - the_layer.updateFields() + output_layer.updateFields() -qgs.exitQgis() \ No newline at end of file +qgs.exitQgis() diff --git a/services_scripts/fix_geometries.py b/services_scripts/fix_geometries.py index 3018817..cfaceaa 100644 --- a/services_scripts/fix_geometries.py +++ b/services_scripts/fix_geometries.py @@ -1,4 +1,4 @@ -from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.core import QgsApplication from qgis.analysis import QgsNativeAlgorithms import processing @@ -11,8 +11,14 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/Geoindex_81670/mamh_usage_predo_2022_s_poly.shp' -fixed_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/fixed_geoindex_7/py_fixes_07.shp' +layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'input_data/Geoindex_81670/mamh_usage_predo_2022_s_poly.shp' +fixed_layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/fixed_geoindex_7/py_fixes_07.shp' params = {'INPUT': layer, 'METHOD': 0, 'OUTPUT': fixed_layer} fix_layer = processing.run("native:fixgeometries", params)['OUTPUT'] diff --git a/services_scripts/intersection.py b/services_scripts/intersection.py index 4b8cab8..c5a183b 100644 --- a/services_scripts/intersection.py +++ b/services_scripts/intersection.py @@ -1,6 +1,5 @@ -# Join attributes by location in QGIS is the same as Spatial Join in ArcGIS - -from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingFeedback +# This services is not used in the main workflow +from qgis.core import QgsApplication from qgis.analysis import QgsNativeAlgorithms import processing @@ -15,15 +14,27 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -layer_1_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/new_tests/python_fixed_05/py_fixes_05.shp' -layer_2_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/property_assessment/uniteevaluationfonciere.shp' -output_layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/intersection_nrcan_property/intersection_nrcan_property.shp' +layer_1_path = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/clipped_nrcan_1/clipped_nrcan_1.shp' +layer_2_path = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'input_data/property_assessment/uniteevaluationfonciere.shp' +output_layer_path = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/intersection_nrcan_property/intersection_nrcan_property_02.shp' params = {'INPUT': layer_1_path, 'OVERLAY': layer_2_path, - 'INPUT_FIELDS': [], 'OVERLAY_FIELDS': [], 'OVERLAY_FIELDS_PREFIX': '', - 'OUTPUT': output_layer_path, 'GRID_SIZE': None} + 'INPUT_FIELDS': [], + 'OVERLAY_FIELDS': [], + 'OVERLAY_FIELDS_PREFIX': '', + 'OUTPUT': output_layer_path, + 'GRID_SIZE': None} processing.run("native:intersection", params) diff --git a/services_scripts/join_attributes_by_location.py b/services_scripts/join_attributes_by_location.py index 876e577..321c777 100644 --- a/services_scripts/join_attributes_by_location.py +++ b/services_scripts/join_attributes_by_location.py @@ -1,6 +1,6 @@ # Join attributes by location in QGIS is the same as Spatial Join in ArcGIS -from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingFeedback +from qgis.core import QgsApplication, QgsProcessingFeedback from qgis.analysis import QgsNativeAlgorithms import processing @@ -15,9 +15,18 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -layer_1_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_fixed_05/py_fixes_05.shp' -layer_2_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/property_assessment/uniteevaluationfonciere.shp' -output_layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_joined_12/py_joined_12.shp' +layer_1_path = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/clipped_nrcan_1/clipped_nrcan_1.shp' +layer_2_path = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/intersection_nrcan_property/intersection_nrcan_property_02.shp' +output_layer_path = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/join_nrcan_intersected_nrcan/join_nrcan_intersected_nrcan.shp' params = {'INPUT': layer_1_path, diff --git a/services_scripts/load_layer.py b/services_scripts/load_layer.py index 4d5e6b7..3c19d77 100644 --- a/services_scripts/load_layer.py +++ b/services_scripts/load_layer.py @@ -1,7 +1,7 @@ from qgis.core import * # Supply the path to the qgis install location -QgsApplication.setPrefixPath(prefixPath='C:/Program Files/QGIS 3.34.1/apps/qgis', useDefaultPaths=True) +QgsApplication.setPrefixPath('C:/Program Files/QGIS 3.34.1/apps/qgis', True) qgs = QgsApplication([], False) @@ -15,4 +15,4 @@ def load_layer(path, layer_name): print(f'{layer_name} failed to load!') else: QgsProject.instance().addMapLayer(the_layer) - return the_layer, layer_name \ No newline at end of file + return the_layer, layer_name diff --git a/services_scripts/merge_layers.py b/services_scripts/merge_layers.py index cc33617..2959aef 100644 --- a/services_scripts/merge_layers.py +++ b/services_scripts/merge_layers.py @@ -1,10 +1,13 @@ -from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.core import QgsApplication from qgis.analysis import QgsNativeAlgorithms import processing from services_scripts.basic_functions import find_shp_files -root_folder = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/clip_all' +root_folder = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/clip_all' layers_path = find_shp_files(root_folder) # Set the path to QGIS installation @@ -17,7 +20,10 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -merged_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/merged_all/merge_divisions.shp' +merged_layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/merged_all/merge_divisions.shp' params = {'LAYERS': layers_path, 'CRS': None, diff --git a/services_scripts/multipart_to_singleparts.py b/services_scripts/multipart_to_singleparts.py index 4092cfb..5e1879f 100644 --- a/services_scripts/multipart_to_singleparts.py +++ b/services_scripts/multipart_to_singleparts.py @@ -1,4 +1,4 @@ -from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.core import QgsApplication from qgis.analysis import QgsNativeAlgorithms import processing @@ -13,8 +13,14 @@ qgs.initQgis() QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -input_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/output_data/delete_dups_4_partitioned|layername=delete_dups__partitioned' -singled_parts_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/output_data/multi_to_single/multi_to_single2.shp' +input_layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/delete_dups_4_partitioned|layername=delete_dups__partitioned' +singled_parts_layer = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/multi_to_single/multi_to_single2.shp' params = {'INPUT': input_layer, 'OUTPUT': singled_parts_layer} diff --git a/services_scripts/split_layer_by_expression.py b/services_scripts/split_layer_by_expression.py index 2ce15d4..9a834de 100644 --- a/services_scripts/split_layer_by_expression.py +++ b/services_scripts/split_layer_by_expression.py @@ -1,4 +1,4 @@ -from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingFeedback +from qgis.core import QgsApplication, QgsVectorLayer, QgsProject from qgis.analysis import QgsNativeAlgorithms import processing import os @@ -26,11 +26,17 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -output_layers_folder = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/divided_all' -num_layers = 3 +output_layers_folder = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/divided_all' +num_layers = 20 create_folders(output_layers_folder, num_layers) -layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/02_clipped_nrcan_mtl_boundary/clipped_nrcan.shp' +layer_path = \ + 'C:/Users/a_adli/PycharmProjects/' \ + 'hydroquebec_archetype_gispy/data/' \ + 'output_data/02_clipped_nrcan_mtl_boundary/clipped_nrcan.shp' fixed_nrcan, fixed_nrcan_name = load_layer(layer_path, 'Clipped NRCan') layer_length = fixed_nrcan.featureCount() @@ -39,23 +45,37 @@ print(f'{fixed_nrcan_name} data count: {layer_length}') intervals = layer_length // num_layers for each in range(num_layers): - output_layer_path = f'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/divided_all/layer_{each}/layer_{each}.shp' + output_layer_path = \ + f'C:/Users/a_adli/PycharmProjects/' \ + f'hydroquebec_archetype_gispy/data/' \ + f'output_data/divided_all/layer_{each}/layer_{each}.shp' params = {'INPUT': layer_path, - 'EXPRESSION': f'$id >= {each * intervals} AND $id < {(each + 1) * intervals}\r\n', + 'EXPRESSION': f'$id >= {each * intervals} ' + f'AND $id < {(each + 1) * intervals}\r\n', 'OUTPUT': output_layer_path} processing.run("native:extractbyexpression", params) +# Creating spatian index for each new layer + create_spatial_indext_params = {'INPUT': output_layer_path, + 'OUTPUT': 'Output'} + processing.run("native:createspatialindex", create_spatial_indext_params) + remaining_features = num_layers -os.makedirs(f'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/divided_all/layer_{remaining_features}') -output_layer_path = f'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/divided_all/layer_{remaining_features}/layer_{remaining_features}.shp' +os.makedirs(f'C:/Users/a_adli/PycharmProjects/' + f'hydroquebec_archetype_gispy/data/' + f'output_data/divided_all/layer_{remaining_features}') +output_layer_path = \ + f'C:/Users/a_adli/PycharmProjects/' \ + f'hydroquebec_archetype_gispy/data/' \ + f'output_data/divided_all/' \ + f'layer_{remaining_features}/layer_{remaining_features}.shp' params = {'INPUT': layer_path, 'EXPRESSION': f'$id >= {num_layers * intervals}\r\n', 'OUTPUT': output_layer_path} processing.run("native:extractbyexpression", params) - # Exit QGIS application qgs.exitQgis()