Move clip_by_multiple from the workflow to the scrub layer class

This commit is contained in:
Alireza Adli 2024-04-03 16:32:06 -04:00
parent 35d58c67f0
commit 96a09e1487
2 changed files with 53 additions and 46 deletions

View File

@ -9,8 +9,10 @@ import processing
class ScrubLayer:
def __init__(self, qgis_path, layer_path, layer_name):
self.qgis_path = qgis_path
# Set the path to QGIS installation
QgsApplication.setPrefixPath(qgis_path, True)
QgsApplication.setPrefixPath(self.qgis_path, True)
# Initialize QGIS application
qgs = QgsApplication([], False)
@ -75,13 +77,18 @@ class ScrubLayer:
processing.run("native:clip", clip_layer_params)
print(f'Clipping of {self.layer_name} is completed.')
def multipart_to_singleparts(self, singleparts_layer_path):
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
params = {'INPUT': self.layer,
'OUTPUT': singleparts_layer_path}
processing.run("native:multiparttosingleparts", params)
def clip_by_multiple(self, number_of_partitions, overlay_layer_path, overlay_layers_dir, clipped_layers_dir):
# overlay_layer = ScrubLayer(self.qgis_path, overlay_layer_path, 'Original Overlay Layer')
# overlay_layer.split_layer(number_of_partitions, overlay_layers_dir)
# create_folders(clipped_layers_dir, number_of_partitions)
for layer in range(number_of_partitions):
overlay = overlay_layers_dir + f'/layer_{layer}/layer_{layer}.shp'
clipped = clipped_layers_dir + f'/layer_{layer}/layer_{layer}.shp'
self.clip_layer(overlay, clipped)
clipped_layer = ScrubLayer(self.qgis_path, clipped, 'Temp Layer')
clipped_layer.create_spatial_index()
def split_layer(self, number_of_layers, splitted_layers_dir, app_path):
def split_layer(self, number_of_layers, splitted_layers_dir):
number_of_layers -= 1
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
create_folders(splitted_layers_dir, number_of_layers)
@ -96,21 +103,28 @@ class ScrubLayer:
processing.run("native:extractbyexpression", params)
new_layer = ScrubLayer(app_path, output_layer_path, 'Temp Layer')
new_layer = ScrubLayer(self.qgis_path, output_layer_path, 'Temp Layer')
new_layer.create_spatial_index()
remaining_features = number_of_layers
os.makedirs(splitted_layers_dir + f'/layer_{remaining_features}')
# Adding a folder for the remaining features
os.makedirs(splitted_layers_dir + f'/layer_{number_of_layers}')
output_layer_path = splitted_layers_dir + \
f'/layer_{remaining_features}/layer_{remaining_features}.shp'
f'/layer_{number_of_layers}/layer_{number_of_layers}.shp'
params = {'INPUT': self.layer,
'EXPRESSION': f'$id >= {number_of_layers * intervals}\r\n',
'OUTPUT': output_layer_path}
processing.run("native:extractbyexpression", params)
new_layer = ScrubLayer(app_path, output_layer_path, 'Temp Layer')
new_layer = ScrubLayer(self.qgis_path, output_layer_path, 'Temp Layer')
new_layer.create_spatial_index()
def multipart_to_singleparts(self, singleparts_layer_path):
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
params = {'INPUT': self.layer,
'OUTPUT': singleparts_layer_path}
processing.run("native:multiparttosingleparts", params)
def delete_duplicates(self, deleted_duplicates_layer):
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
params = {'INPUT': self.layer_path,

View File

@ -12,11 +12,17 @@ class ScrubMTL:
self.initialize_layer(property_assessment, 'Property Assessment')
self.montreal_boundary = montreal_boundary
self.output_paths_dir = output_paths_dir
self.input_paths = {
'NRCan': nrcan,
'GeoIndex': geo_index,
'Property Assessment': property_assessment,
'Montreal Boundary': montreal_boundary
}
self.output_paths = {
'Fixed NRCan': '',
'Fixed GeoIndex': '',
'Clipped Fixed GeoIndex': '',
'Splitted NRCan': '',
'Splitted NRCans': '',
'Pairwise Clipped Property Assessment Partitions': '',
'Pairwise Clipped Merged Property Assessment': '',
'Property Assessment and NRCan': '',
@ -41,7 +47,10 @@ class ScrubMTL:
new_folder = path.lower().replace(' ', '_')
output_path = self.output_paths_dir + '/' + new_folder
os.mkdir(output_path)
if path[-1] != 's':
self.output_paths[path] = output_path + f'/{new_folder}.shp'
else:
self.output_paths[path] = output_path
def initialize_layer(self, layer_path, layer_name):
return ScrubLayer(self.qgis_path, layer_path, layer_name)
@ -79,39 +88,23 @@ class ScrubMTL:
f'{geo_index_clipped.layer.featureCount()}')
def process_property_assesment(self):
print(f'Data Count of the GeoIndex layer: '
print(f'Data Count of the Property Assessment layer: '
f'{self.property_assessment.data_count}')
self.property_assessment.create_spatial_index()
def clip_with_several_overlays(self, input_layer_path, input_layer_name,
overlaying_layer_path, overlaying_layer_name,
number_of_overlays, splitted_layers_dir,
clipped_layers_dir):
"""This method clips using desired number of overlay layers
by splitting a big overlaying layer. It uses the ScrubLayer class
clip_layer method."""
overlaying_layer = \
self.initialize_layer(overlaying_layer_path, overlaying_layer_name)
input_layer = \
self.initialize_layer(input_layer_path, input_layer_name)
overlaying_layer.\
split_layer(number_of_overlays, splitted_layers_dir, self.qgis_path)
create_folders(clipped_layers_dir, number_of_overlays)
for overlay_layer_num in range(number_of_overlays):
splitted_overlay_layer_path = \
splitted_layers_dir + \
f'/layer_{overlay_layer_num}/layer_{overlay_layer_num}.shp'
clipped_layer_partition_path = \
clipped_layers_dir + \
f'/layer_{overlay_layer_num}/layer_{overlay_layer_num}.shp'
input_layer.clip_layer(splitted_overlay_layer_path,
clipped_layer_partition_path)
clipped_layer = \
self.initialize_layer(clipped_layer_partition_path,
f'clipped_layer_{overlay_layer_num}')
clipped_layer.create_spatial_index()
print('Pairwise Clipping is completed.')
def the_cleaning_workflow(self):
"""It carries out all the steps to clean the dataset."""
# self.generate_output_paths()
# self.process_nrcan()
# self.process_geo_index()
# self.process_property_assesment()
self.clip_with_several_overlays(self.input_paths['Property Assessment'],
'Property Assessment',
self.output_paths['Fixed NRCan'],
'Fixed NRCan',
10, self.output_paths['Splitted NRCans'],
self.output_paths
['Pairwise Clipped Property Assessment Partitions'])
def refine_heights(self):
pass