diff --git a/scrub_layer_class.py b/scrub_layer_class.py index b47c365..06fa23c 100644 --- a/scrub_layer_class.py +++ b/scrub_layer_class.py @@ -8,7 +8,7 @@ Project Developer: Alireza Adli alireza.adli@concordia.ca from qgis.core import QgsApplication, QgsField, QgsProject, \ QgsProcessingFeedback, QgsVectorLayer, QgsVectorDataProvider, \ QgsExpressionContext, QgsExpressionContextUtils, edit, QgsFeatureRequest, \ - QgsExpression + QgsExpression, QgsVectorFileWriter from qgis.PyQt.QtCore import QVariant from qgis.analysis import QgsNativeAlgorithms from basic_functions import * @@ -27,6 +27,25 @@ class ScrubLayer: self.layer = self.load_layer() self.data_count = self.layer.featureCount() + def duplicate_layer(self, output_path, duplicated_layer_name): + options = QgsVectorFileWriter.SaveVectorOptions() + options.driverName = 'ESRI Shapefile' + + duplication = QgsVectorFileWriter.writeAsVectorFormat( + self.layer, + output_path, + options + ) + + if duplication == QgsVectorFileWriter.NoError: + print(f"Shapefile successfully duplicated") + duplicated_layer = ScrubLayer( + self.qgis_path, output_path, duplicated_layer_name) + duplicated_layer.create_spatial_index() + return duplicated_layer + else: + print(f"Error duplicating shapefile: {duplication}") + def get_cell(self, fid, field_name): return self.layer.getFeature(fid)[field_name]