Convert features to shape layers

This commit is contained in:
Alireza Adli 2024-10-21 13:03:17 -04:00
parent d70130b570
commit 4b85e48deb

View File

@ -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, QgsVectorFileWriter
QgsExpression, QgsVectorFileWriter, QgsCoordinateReferenceSystem
from qgis.PyQt.QtCore import QVariant
from qgis.analysis import QgsNativeAlgorithms
from basic_functions import *
@ -69,8 +69,27 @@ class ScrubLayer:
QgsProject.instance().addMapLayer(the_layer)
return the_layer
def features_to_layers(self):
pass
def features_to_layers(self, layers_dir, crs):
create_folders(layers_dir, self.data_count)
target_crs = QgsCoordinateReferenceSystem(crs)
for feature in self.layer.getFeatures():
new_layer = QgsVectorLayer(f'Polygon?crs={crs}', "feature_layer", "memory")
new_layer.setCrs(target_crs)
new_provider = new_layer.dataProvider()
new_provider.addFeatures([feature])
feature_id = feature.id()
output_path = f'{layers_dir}layer_{feature_id}/layer_{feature_id}.shp'
QgsVectorFileWriter.writeAsVectorFormat(
new_layer,
output_path,
'utf-8',
new_layer.crs(),
'ESRI Shapefile'
)
print('Shapefiles created for each feature.')
def fix_geometries(self, fixed_layer):
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())