Add comments, remove run assignment variables for fixing nrcan geometries and clipping it

This commit is contained in:
Alireza Adli 2024-02-19 10:36:34 -05:00
parent 9c97fae0f1
commit 510466b3bb

View File

@ -3,6 +3,7 @@ from qgis.analysis import QgsNativeAlgorithms
import processing
# This function loads a layer. It's used to count the data records
def load_layer(path, layer_name):
the_layer = QgsVectorLayer(path, layer_name, "ogr")
if not the_layer.isValid():
@ -23,37 +24,42 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
# Reading Automatically Extracted Buildings layer (NRCan) and counting its data records
nrcan_0 = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/nrcan/Autobuilding_QC_VILLE_MONTREAL.shp'
nrcan, nrcan_name = load_layer(nrcan_0, 'NRCan')
print(f'Loading {nrcan_name}')
print(f'{nrcan_name} data count: {nrcan.featureCount()}')
# Fixing geometries of the Automatically Extracted Buildings layer
print(f'Fixing {nrcan_name} geometries')
fixed_nrcan_0 = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/new_tests/python_fixed_05/py_fixes_05.shp'
params_fixing_nrcan = {'INPUT': nrcan_0, 'METHOD': 0, 'OUTPUT': fixed_nrcan_0}
fixed_layer_nrcan = processing.run('native:fixgeometries', params_fixing_nrcan)['OUTPUT']
processing.run('native:fixgeometries', params_fixing_nrcan)
fixed_nrcan, fixed_nrcan_name = load_layer(fixed_layer_nrcan, 'Fixed NRCan')
fixed_nrcan, fixed_nrcan_name = load_layer(fixed_nrcan_0, 'Fixed NRCan')
print(f'{fixed_nrcan_name} data count: {fixed_nrcan.featureCount()}')
# Removing unnecessary parts of the layer (outside of Montreal) using
# Administrative boundaries of the Montreal agglomeration data
clipping_montreal_boundary_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/montreal_boundary|layername=Montreal_boundary'
clipped_nrcan_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/new_tests/clipped_nrcan_1/clipped_nrcan_1.shp'
params_clipping_nrcan = {'INPUT': fixed_nrcan_0, 'OVERLAY': clipping_montreal_boundary_layer, 'FILTER_EXPRESSION': '', 'FILTER_EXTENT': None, 'OUTPUT': clipped_nrcan_layer}
clipped_layer_nrcan = processing.run("native:clip", params_clipping_nrcan)['OUTPUT']
print(f'Clipping of {fixed_nrcan_name} is completed.')
# After each step, data records are counted
clipped_nrcan, clipped_nrcan_name = load_layer(clipped_layer_nrcan, 'Clipped NRCan')
print(f'{clipped_nrcan_name} data count: {clipped_nrcan.featureCount()}')
# Creating spatial index is needed for most of the newly built layers.
# It adds a file with suffix .qix
# Without the index, most of the processes cannot be run (smoothly and efficiently)
params_create_index_nrcan = {'INPUT': clipped_nrcan, 'OUTPUT': 'Output'}
indexed_layer = processing.run("native:createspatialindex", params_create_index_nrcan)
processing.run("native:createspatialindex", params_create_index_nrcan)
print(f'Creating spatial index for {clipped_nrcan_name} is completed.')