Duplicate layer

This commit is contained in:
Alireza Adli 2024-08-16 10:43:17 -04:00
parent a9ad3ad0ac
commit e849ca568c

View File

@ -8,7 +8,7 @@ Project Developer: Alireza Adli alireza.adli@concordia.ca
from qgis.core import QgsApplication, QgsField, QgsProject, \ from qgis.core import QgsApplication, QgsField, QgsProject, \
QgsProcessingFeedback, QgsVectorLayer, QgsVectorDataProvider, \ QgsProcessingFeedback, QgsVectorLayer, QgsVectorDataProvider, \
QgsExpressionContext, QgsExpressionContextUtils, edit, QgsFeatureRequest, \ QgsExpressionContext, QgsExpressionContextUtils, edit, QgsFeatureRequest, \
QgsExpression QgsExpression, QgsVectorFileWriter
from qgis.PyQt.QtCore import QVariant from qgis.PyQt.QtCore import QVariant
from qgis.analysis import QgsNativeAlgorithms from qgis.analysis import QgsNativeAlgorithms
from basic_functions import * from basic_functions import *
@ -27,6 +27,25 @@ class ScrubLayer:
self.layer = self.load_layer() self.layer = self.load_layer()
self.data_count = self.layer.featureCount() 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): def get_cell(self, fid, field_name):
return self.layer.getFeature(fid)[field_name] return self.layer.getFeature(fid)[field_name]