Add deleting a field as a new service

This commit is contained in:
Alireza Adli 2024-02-25 22:47:20 -05:00
parent 0f81781a42
commit 8d523bfc11
2 changed files with 42 additions and 0 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ standalone_charm.py
# Other Files
setting_up_standalone_pyqgis.docx
symbology-style.db
.idea/.name

View File

@ -0,0 +1,41 @@
from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, edit
from qgis.analysis import QgsNativeAlgorithms
def load_layer(path, layer_name):
the_layer = QgsVectorLayer(path, layer_name, "ogr")
if not the_layer.isValid():
print(f'{layer_name} failed to load!')
else:
QgsProject.instance().addMapLayer(the_layer)
return the_layer, layer_name
# Set the path to QGIS installation
QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.34.1/apps/qgis", True)
# Initialize QGIS application
qgs = QgsApplication([], False)
qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/merged_all_delete_columns/merge_divisions.shp'
the_layer = load_layer(layer_path, 'Merged Pairwise Clipped')[0]
column_name = "layer" # and 'path'
# Start editing
with edit(the_layer):
# Get the index of the column to delete
idx = the_layer.fields().indexFromName(column_name)
# Delete the field
the_layer.deleteAttribute(idx)
# Update layer fields
the_layer.updateFields()
qgs.exitQgis()