From cca682871c8af38a158b72736155f1bc9e4cff69 Mon Sep 17 00:00:00 2001 From: Alireza Adli Date: Mon, 19 Feb 2024 13:42:04 -0500 Subject: [PATCH] Edit the workflow(mtl_buildings_workflow) & Add intersection and spatial join (joinAttributesByLocation) --- mtl_buildings_workflow.py | 12 ++++--- services_scripts/intersection.py | 31 ++++++++++++++++ .../join_attributes_by_location.py | 36 +++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/mtl_buildings_workflow.py b/mtl_buildings_workflow.py index 1d66b3c..9e2ef49 100644 --- a/mtl_buildings_workflow.py +++ b/mtl_buildings_workflow.py @@ -88,13 +88,17 @@ print(f'{clipped_geoindex_name} data count: {clipped_geoindex.featureCount()}') # Creating spatial index fo the GeoIndex layer params_create_index_geoindex = {'INPUT': clipped_geoindex, 'OUTPUT': 'Output'} processing.run("native:createspatialindex", params_create_index_geoindex) - print(f'Creating spatial index for {clipped_geoindex_name} is completed.') # Reading the Property Assessment (uniteevaluationfonciere) dataset -uniteevaluationfonciere_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/uniteevaluationfonciere/uniteevaluationfonciere.shp' +property_assessment_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/uniteevaluationfonciere/uniteevaluationfonciere.shp' -loaded_uniteevaluationfonciere_layer, uniteevaluationfonciere_layer_name = load_layer(uniteevaluationfonciere_layer, 'uniteevaluationfonciere') -print(f'{uniteevaluationfonciere_layer_name} data count: {loaded_uniteevaluationfonciere_layer.featureCount()}') +property_assessment_read, property_assessment_layer_name = load_layer(property_assessment_layer, 'Property Assesment') +print(f'{property_assessment_layer_name} data count: {property_assessment_read.featureCount()}') + +# Creating spatial index fo the GeoIndex layer +params_create_index_property_assessment = {'INPUT': property_assessment_layer, 'OUTPUT': 'Output'} +processing.run("native:createspatialindex", params_create_index_property_assessment) +print(f'Creating spatial index for {property_assessment_layer_name} is completed.') qgs.exitQgis() \ No newline at end of file diff --git a/services_scripts/intersection.py b/services_scripts/intersection.py index e69de29..4b8cab8 100644 --- a/services_scripts/intersection.py +++ b/services_scripts/intersection.py @@ -0,0 +1,31 @@ +# Join attributes by location in QGIS is the same as Spatial Join in ArcGIS + +from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingFeedback +from qgis.analysis import QgsNativeAlgorithms +import processing + +# 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()) + +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' + + +params = {'INPUT': layer_1_path, + 'OVERLAY': layer_2_path, + 'INPUT_FIELDS': [], 'OVERLAY_FIELDS': [], 'OVERLAY_FIELDS_PREFIX': '', + 'OUTPUT': output_layer_path, 'GRID_SIZE': None} + +processing.run("native:intersection", params) + +# Exit QGIS application +qgs.exitQgis() diff --git a/services_scripts/join_attributes_by_location.py b/services_scripts/join_attributes_by_location.py index e69de29..876e577 100644 --- a/services_scripts/join_attributes_by_location.py +++ b/services_scripts/join_attributes_by_location.py @@ -0,0 +1,36 @@ +# Join attributes by location in QGIS is the same as Spatial Join in ArcGIS + +from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingFeedback +from qgis.analysis import QgsNativeAlgorithms +import processing + +# 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()) + +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' + + +params = {'INPUT': layer_1_path, + 'PREDICATE': [0], + 'JOIN': layer_2_path, + 'JOIN_FIELDS': [], + 'METHOD': 0, + 'DISCARD_NONMATCHING': False, + 'PREFIX': '', + 'OUTPUT': output_layer_path} + +feedback = QgsProcessingFeedback() +processing.run('native:joinattributesbylocation', params, feedback=feedback) + +# Exit QGIS application +qgs.exitQgis()