From 41e89249388e50e115663ccc0897c4e5438cedf5 Mon Sep 17 00:00:00 2001 From: Alireza Adli Date: Tue, 26 Mar 2024 16:04:43 -0400 Subject: [PATCH] Add new methods --- scrub_layer_class.py | 70 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/scrub_layer_class.py b/scrub_layer_class.py index b18a4bd..1ea81eb 100644 --- a/scrub_layer_class.py +++ b/scrub_layer_class.py @@ -1,6 +1,7 @@ from qgis.core import QgsApplication, QgsField, QgsProject, \ QgsProcessingFeedback, QgsVectorLayer, QgsVectorDataProvider, \ - QgsExpressionContext, QgsExpressionContextUtils + QgsExpressionContext, QgsExpressionContextUtils, edit +from qgis.PyQt.QtCore import QVariant from qgis.analysis import QgsNativeAlgorithms import processing @@ -43,9 +44,25 @@ class ScrubLayer: 'OUTPUT': 'Output' } processing.run("native:createspatialindex", create_spatial_index_params) - print(f'Creating spatial index for {self.layer_name} is completed.') + print(f'Creating Spatial index for {self.layer_name} is completed.') + + def spatial_join(self, joining_layer_path, joined_layer_path): + """In QGIS, it is called 'Join attributes by Location'""" + params = {'INPUT': self.layer, + 'PREDICATE': [0], + 'JOIN': joining_layer_path, + 'JOIN_FIELDS': [], + 'METHOD': 0, + 'DISCARD_NONMATCHING': False, + 'PREFIX': '', + 'OUTPUT': joined_layer_path} + + feedback = QgsProcessingFeedback() + processing.run('native:joinattributesbylocation', params, feedback=feedback) + print(f'Spatial Join with input layer {self.layer_name} is completed.') def clip_layer(self, overlay_layer, clipped_layer): + """This must be tested""" QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) clip_layer_params = { 'INPUT': self.layer_path, @@ -57,6 +74,55 @@ class ScrubLayer: processing.run("native:clip", clip_layer_params) print(f'Clipping of {self.layer_name} is completed.') + def merge_layers(self, layers_path, mered_layer_path): + pass + + def multipart_to_singleparts(self): + pass + + def split_layer(self): + pass + + def delete_duplicates(self, deleted_duplicates_layer): + QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) + params = {'INPUT': self.layer_path, + 'OUTPUT': deleted_duplicates_layer} + processing.run("native:deleteduplicategeometries", params) + + def delete_field(self, field_name): + QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) + with edit(self.layer): + # Get the index of the column to delete + idx = self.layer.fields().indexFromName(field_name) + + # Delete the field + self.layer.deleteAttribute(idx) + + # Update layer fields + self.layer.updateFields() + + def add_field(self, new_field_name): + functionalities = self.layer.dataProvider().capabilities() + + if functionalities & QgsVectorDataProvider.AddAttributes: + new_field = QgsField(new_field_name, QVariant.Double) + self.layer.dataProvider().addAttributes([new_field]) + self.layer.updateFields() + + def assign_area(self, field_name): + self.layer.startEditing() + idx = self.layer.fields().indexFromName(field_name) + + context = QgsExpressionContext() + context.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(self.layer)) + + for feature in self.layer.getFeatures(): + area = feature.geometry().area() + feature[idx] = area + self.layer.updateFeature(feature) + + self.layer.commitChanges() + def __str__(self): return f'The {self.layer_name} has {self.data_count} records.'