From 76abe748374fed2d6556d0cd82372c4306d8b413 Mon Sep 17 00:00:00 2001 From: Alireza Adli Date: Mon, 19 Feb 2024 14:11:26 -0500 Subject: [PATCH] Add intersection and spatial join (pairwise clip is done) --- mtl_buildings_workflow.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/mtl_buildings_workflow.py b/mtl_buildings_workflow.py index 9e2ef49..f88a6f0 100644 --- a/mtl_buildings_workflow.py +++ b/mtl_buildings_workflow.py @@ -101,4 +101,42 @@ params_create_index_property_assessment = {'INPUT': property_assessment_layer, ' processing.run("native:createspatialindex", params_create_index_property_assessment) print(f'Creating spatial index for {property_assessment_layer_name} is completed.') +intersection_nrcan_property_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/intersection_nrcan_property/intersection_nrcan_property.shp' + +# A. Intersection by inputting NRCan and assigning property assessment as the overlay +# (Three steps replace the pairwise clip where input is the property assessment and the overlay is the clipped nrcan) +# We call these three steps A, B and C respectively +params_intersection_nrcan_property_assessment = \ + {'INPUT': clipped_nrcan, + 'INPUT_FIELDS': [], 'OVERLAY_FIELDS': [], 'OVERLAY_FIELDS_PREFIX': '', + 'OUTPUT': intersection_nrcan_property_layer, 'GRID_SIZE': None} +processing.run("native:intersection", params) + +intersection_nrcan_property_read, intersection_nrcan_property_layer_name = load_layer(property_assessment_layer, 'Property Assesment') +print(f'{intersection_nrcan_property_layer_name} data count: {intersection_nrcan_property_read.featureCount()}') + +# Creating spatial index fo the result of the previous intersection +params_create_index_intersection_nrcan = {'INPUT': intersection_nrcan_property_layer, 'OUTPUT': 'Output'} +processing.run("native:createspatialindex", params_create_index_intersection_nrcan) +print(f'Creating spatial index for {intersection_nrcan_property_layer_name} is completed.') + +# B. Spatial Join (in QGIS: join attributes by location), with the NRCan as input +# and the result of previous intersection as overlay +output_layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/join_nrcan_intersected_nrcan/py_joined_12.shp' + +params_spatial_join_nrcan_intersection = \ + {'INPUT': clipped_nrcan, + 'PREDICATE': [0], + 'JOIN': intersection_nrcan_property_layer, + 'JOIN_FIELDS': [], + 'METHOD': 0, + 'DISCARD_NONMATCHING': False, + 'PREFIX': '', + 'OUTPUT': output_layer_path} + +feedback_spatial_join_nrcan_intersection = QgsProcessingFeedback() +processing.run('native:joinattributesbylocation', params, + feedback=feedback_spatial_join_nrcan_intersection) + + qgs.exitQgis() \ No newline at end of file