Move clip_by_multiple from the workflow to the scrub layer class
This commit is contained in:
parent
35d58c67f0
commit
96a09e1487
|
@ -9,8 +9,10 @@ import processing
|
||||||
|
|
||||||
class ScrubLayer:
|
class ScrubLayer:
|
||||||
def __init__(self, qgis_path, layer_path, layer_name):
|
def __init__(self, qgis_path, layer_path, layer_name):
|
||||||
|
|
||||||
|
self.qgis_path = qgis_path
|
||||||
# Set the path to QGIS installation
|
# Set the path to QGIS installation
|
||||||
QgsApplication.setPrefixPath(qgis_path, True)
|
QgsApplication.setPrefixPath(self.qgis_path, True)
|
||||||
|
|
||||||
# Initialize QGIS application
|
# Initialize QGIS application
|
||||||
qgs = QgsApplication([], False)
|
qgs = QgsApplication([], False)
|
||||||
|
@ -75,13 +77,18 @@ class ScrubLayer:
|
||||||
processing.run("native:clip", clip_layer_params)
|
processing.run("native:clip", clip_layer_params)
|
||||||
print(f'Clipping of {self.layer_name} is completed.')
|
print(f'Clipping of {self.layer_name} is completed.')
|
||||||
|
|
||||||
def multipart_to_singleparts(self, singleparts_layer_path):
|
def clip_by_multiple(self, number_of_partitions, overlay_layer_path, overlay_layers_dir, clipped_layers_dir):
|
||||||
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
|
# overlay_layer = ScrubLayer(self.qgis_path, overlay_layer_path, 'Original Overlay Layer')
|
||||||
params = {'INPUT': self.layer,
|
# overlay_layer.split_layer(number_of_partitions, overlay_layers_dir)
|
||||||
'OUTPUT': singleparts_layer_path}
|
# create_folders(clipped_layers_dir, number_of_partitions)
|
||||||
processing.run("native:multiparttosingleparts", params)
|
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
|
number_of_layers -= 1
|
||||||
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
|
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
|
||||||
create_folders(splitted_layers_dir, number_of_layers)
|
create_folders(splitted_layers_dir, number_of_layers)
|
||||||
|
@ -96,21 +103,28 @@ class ScrubLayer:
|
||||||
|
|
||||||
processing.run("native:extractbyexpression", params)
|
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()
|
new_layer.create_spatial_index()
|
||||||
|
|
||||||
remaining_features = number_of_layers
|
# Adding a folder for the remaining features
|
||||||
os.makedirs(splitted_layers_dir + f'/layer_{remaining_features}')
|
|
||||||
|
os.makedirs(splitted_layers_dir + f'/layer_{number_of_layers}')
|
||||||
output_layer_path = splitted_layers_dir + \
|
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,
|
params = {'INPUT': self.layer,
|
||||||
'EXPRESSION': f'$id >= {number_of_layers * intervals}\r\n',
|
'EXPRESSION': f'$id >= {number_of_layers * intervals}\r\n',
|
||||||
'OUTPUT': output_layer_path}
|
'OUTPUT': output_layer_path}
|
||||||
|
|
||||||
processing.run("native:extractbyexpression", params)
|
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()
|
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):
|
def delete_duplicates(self, deleted_duplicates_layer):
|
||||||
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
|
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
|
||||||
params = {'INPUT': self.layer_path,
|
params = {'INPUT': self.layer_path,
|
||||||
|
|
|
@ -12,11 +12,17 @@ class ScrubMTL:
|
||||||
self.initialize_layer(property_assessment, 'Property Assessment')
|
self.initialize_layer(property_assessment, 'Property Assessment')
|
||||||
self.montreal_boundary = montreal_boundary
|
self.montreal_boundary = montreal_boundary
|
||||||
self.output_paths_dir = output_paths_dir
|
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 = {
|
self.output_paths = {
|
||||||
'Fixed NRCan': '',
|
'Fixed NRCan': '',
|
||||||
'Fixed GeoIndex': '',
|
'Fixed GeoIndex': '',
|
||||||
'Clipped Fixed GeoIndex': '',
|
'Clipped Fixed GeoIndex': '',
|
||||||
'Splitted NRCan': '',
|
'Splitted NRCans': '',
|
||||||
'Pairwise Clipped Property Assessment Partitions': '',
|
'Pairwise Clipped Property Assessment Partitions': '',
|
||||||
'Pairwise Clipped Merged Property Assessment': '',
|
'Pairwise Clipped Merged Property Assessment': '',
|
||||||
'Property Assessment and NRCan': '',
|
'Property Assessment and NRCan': '',
|
||||||
|
@ -41,7 +47,10 @@ class ScrubMTL:
|
||||||
new_folder = path.lower().replace(' ', '_')
|
new_folder = path.lower().replace(' ', '_')
|
||||||
output_path = self.output_paths_dir + '/' + new_folder
|
output_path = self.output_paths_dir + '/' + new_folder
|
||||||
os.mkdir(output_path)
|
os.mkdir(output_path)
|
||||||
|
if path[-1] != 's':
|
||||||
self.output_paths[path] = output_path + f'/{new_folder}.shp'
|
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):
|
def initialize_layer(self, layer_path, layer_name):
|
||||||
return ScrubLayer(self.qgis_path, layer_path, layer_name)
|
return ScrubLayer(self.qgis_path, layer_path, layer_name)
|
||||||
|
@ -54,7 +63,7 @@ class ScrubMTL:
|
||||||
self.initialize_layer(self.output_paths['Fixed NRCan'],
|
self.initialize_layer(self.output_paths['Fixed NRCan'],
|
||||||
'Fixed NRCan')
|
'Fixed NRCan')
|
||||||
nrcan_fixed.create_spatial_index()
|
nrcan_fixed.create_spatial_index()
|
||||||
print(f'Data Count of the NRCan layer ({nrcan_fixed.layer_name})'
|
print(f'Data Count of the NRCan layer ({nrcan_fixed.layer_name}) '
|
||||||
f'after fixing geometries: {nrcan_fixed.layer.featureCount()}')
|
f'after fixing geometries: {nrcan_fixed.layer.featureCount()}')
|
||||||
|
|
||||||
def process_geo_index(self):
|
def process_geo_index(self):
|
||||||
|
@ -65,7 +74,7 @@ class ScrubMTL:
|
||||||
self.initialize_layer(self.output_paths['Fixed GeoIndex'],
|
self.initialize_layer(self.output_paths['Fixed GeoIndex'],
|
||||||
'Fixed GeoIndex')
|
'Fixed GeoIndex')
|
||||||
geo_index_fixed.create_spatial_index()
|
geo_index_fixed.create_spatial_index()
|
||||||
print(f'Data Count of the GeoIndex layer ({geo_index_fixed.layer_name})'
|
print(f'Data Count of the GeoIndex layer ({geo_index_fixed.layer_name}) '
|
||||||
f'after fixing geometries: {geo_index_fixed.layer.featureCount()}')
|
f'after fixing geometries: {geo_index_fixed.layer.featureCount()}')
|
||||||
geo_index_fixed.clip_layer(self.montreal_boundary,
|
geo_index_fixed.clip_layer(self.montreal_boundary,
|
||||||
self.output_paths['Clipped Fixed GeoIndex'])
|
self.output_paths['Clipped Fixed GeoIndex'])
|
||||||
|
@ -79,39 +88,23 @@ class ScrubMTL:
|
||||||
f'{geo_index_clipped.layer.featureCount()}')
|
f'{geo_index_clipped.layer.featureCount()}')
|
||||||
|
|
||||||
def process_property_assesment(self):
|
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}')
|
f'{self.property_assessment.data_count}')
|
||||||
self.property_assessment.create_spatial_index()
|
self.property_assessment.create_spatial_index()
|
||||||
|
|
||||||
def clip_with_several_overlays(self, input_layer_path, input_layer_name,
|
def the_cleaning_workflow(self):
|
||||||
overlaying_layer_path, overlaying_layer_name,
|
"""It carries out all the steps to clean the dataset."""
|
||||||
number_of_overlays, splitted_layers_dir,
|
# self.generate_output_paths()
|
||||||
clipped_layers_dir):
|
# self.process_nrcan()
|
||||||
"""This method clips using desired number of overlay layers
|
# self.process_geo_index()
|
||||||
by splitting a big overlaying layer. It uses the ScrubLayer class
|
# self.process_property_assesment()
|
||||||
clip_layer method."""
|
self.clip_with_several_overlays(self.input_paths['Property Assessment'],
|
||||||
overlaying_layer = \
|
'Property Assessment',
|
||||||
self.initialize_layer(overlaying_layer_path, overlaying_layer_name)
|
self.output_paths['Fixed NRCan'],
|
||||||
input_layer = \
|
'Fixed NRCan',
|
||||||
self.initialize_layer(input_layer_path, input_layer_name)
|
10, self.output_paths['Splitted NRCans'],
|
||||||
overlaying_layer.\
|
self.output_paths
|
||||||
split_layer(number_of_overlays, splitted_layers_dir, self.qgis_path)
|
['Pairwise Clipped Property Assessment Partitions'])
|
||||||
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 refine_heights(self):
|
def refine_heights(self):
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in New Issue
Block a user