mtl_gis_oo/handle_mtl_ds_workflow.py
2024-08-05 18:00:06 -04:00

145 lines
4.7 KiB
Python

"""
handle_mtl_ds_workflow module
The workflow of cleaning and updating the Montreal Buildings dataset.
Project Developer: Alireza Adli alireza.adli@concordia.ca
"""
from scrub_layer_class import *
from input_paths_and_layers import *
# Making folders for the output data layers
create_output_folders(output_paths, output_paths_dir)
# Initialize the input data layers
nrcan = ScrubLayer(qgis_path, input_paths['NRCan'], 'NRCan')
geo_index = ScrubLayer(qgis_path, input_paths['GeoIndex'], 'GeoIndex')
property_assessment = \
ScrubLayer(
qgis_path, input_paths['Property Assessment'], 'Property Assessment')
montreal_boundary = \
ScrubLayer(qgis_path, input_paths['Montreal Boundary'], 'Montreal Boundary')
# Processing the NRCan layer includes fixing its geometries
print('Processing the NRCan layer')
print(nrcan)
nrcan.create_spatial_index()
nrcan.fix_geometries(output_paths['Fixed NRCan'])
# Defining a new layer for the fixed NRCan
nrcan_fixed = \
ScrubLayer(qgis_path, output_paths['Fixed NRCan'], 'Fixed NRCan')
nrcan_fixed.create_spatial_index()
print(nrcan_fixed)
# Processing the GeoIndex layer includes fixing its geometries and
# clipping it based on the Montreal boundary data layer
print('Processing the GeoIndex layer')
print(geo_index)
geo_index.create_spatial_index()
geo_index.fix_geometries(output_paths['Fixed GeoIndex'])
# Defining a new layer for the fixed GeoIndex
geo_index_fixed = ScrubLayer(qgis_path, output_paths['Fixed GeoIndex'],
'Fixed GeoIndex')
geo_index_fixed.create_spatial_index()
print(geo_index_fixed)
geo_index_fixed.clip_layer(montreal_boundary.layer_path,
output_paths['Clipped Fixed GeoIndex'])
geo_index_clipped = \
ScrubLayer(qgis_path,
output_paths['Clipped Fixed GeoIndex'], 'Clipped Fixed GeoIndex')
geo_index_clipped.create_spatial_index()
print(geo_index_clipped)
# Processing the Property Assessment layer includes a pairwise clip, and
# two spatial join with NRCan and GeoIndex layers, respectively
print(property_assessment)
property_assessment.create_spatial_index()
# For the pairwise clip, number of overlaying layers can be chosen
# (meaning number of splits for NRCan layer). This improves the performance
# where may increase duplicates. This has been done because using the NRCan
# layer as a whole causes crashing the clipping process.
# First we split the overlaying layers into our desired number
nrcan_fixed.split_layer(120, output_paths['Splitted NRCans'])
# Clipping have to be done in
clipping_property_assessment = """
from input_paths_and_layers import *
property_assessment.clip_by_multiple(
120, output_paths['Splitted NRCans'],
output_paths['Pairwise Clipped Property Assessment Partitions'])"""
exec(clipping_property_assessment)
property_assessment.merge_layers(
output_paths['Pairwise Clipped Property Assessment Partitions'],
output_paths['Pairwise Clipped Merged Property Assessment'])
clipped_property_assessment = ScrubLayer(
qgis_path,
output_paths['Pairwise Clipped Merged Property Assessment'],
'Clipped Property Assessment')
print(clipped_property_assessment)
clipped_property_assessment.create_spatial_index()
clipped_property_assessment.spatial_join(
nrcan_fixed.layer_path,
output_paths['Property Assessment and NRCan'])
property_assessment_nrcan = ScrubLayer(
qgis_path,
output_paths['Property Assessment and NRCan'],
'Property Assessment and NRCan')
print(property_assessment_nrcan)
property_assessment_nrcan.create_spatial_index()
property_assessment_nrcan.spatial_join(
geo_index_clipped,
output_paths['Property Assessment and NRCan and GeoIndex'])
property_assessment_nrcan_geo = ScrubLayer(
qgis_path,
output_paths['Property Assessment and NRCan and GeoIndex'],
'Property Assessment and NRCan and GeoIndex')
print(property_assessment_nrcan_geo)
property_assessment_nrcan_geo.create_spatial_index()
property_assessment_nrcan_geo.delete_duplicates(
output_paths['Deleted Duplicates Layer'])
deleted_dups_layer = ScrubLayer(
qgis_path,
output_paths['Deleted Duplicates Layer'],
'Deleted Duplicates Layer')
print(deleted_dups_layer)
property_assessment_nrcan_geo.create_spatial_index()
property_assessment_nrcan_geo.multipart_to_singleparts(
output_paths['Single Parts Layer'])
single_parts_layer = ScrubLayer(
qgis_path,
output_paths['Single Parts Layer'],
'Single Parts Layer')
print(single_parts_layer)
single_parts_layer.create_spatial_index()
# Add an area field
single_parts_layer.add_field('Area')
single_parts_layer.assign_area('Area')
dismissive_area = 15
single_parts_layer.conditional_delete_record('Area', '<', dismissive_area)
print(f'After removing buildings with less than {dismissive_area} squaremeter area:')
print(single_parts_layer)