From ecc668f719bfbe93ca19b20d2649590d9ab1c934 Mon Sep 17 00:00:00 2001 From: Alireza Adli Date: Tue, 26 Mar 2024 19:21:15 -0400 Subject: [PATCH] Add split a layer --- scrub_layer_class.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/scrub_layer_class.py b/scrub_layer_class.py index 5309d47..272aadf 100644 --- a/scrub_layer_class.py +++ b/scrub_layer_class.py @@ -81,8 +81,35 @@ class ScrubLayer: 'OUTPUT': singleparts_layer_path} processing.run("native:multiparttosingleparts", params) - def split_layer(self): - pass + def split_layer(self, number_of_layers, splitted_layers_dir, app_path): + number_of_layers -= 1 + QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) + create_folders(splitted_layers_dir, number_of_layers) + intervals = self.data_count // number_of_layers + for part in range(number_of_layers): + output_layer_path = \ + splitted_layers_dir + f'/layer_{part}/layer_{part}.shp' + params = {'INPUT': self.layer, + 'EXPRESSION': f'$id >= {part * intervals} ' + f'AND $id < {(part + 1) * intervals}\r\n', + 'OUTPUT': output_layer_path} + + processing.run("native:extractbyexpression", params) + + new_layer = ScrubLayer(app_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}') + output_layer_path = splitted_layers_dir + \ + f'/layer_{remaining_features}/layer_{remaining_features}.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.create_spatial_index() def delete_duplicates(self, deleted_duplicates_layer): QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())