#qgistutorial, #python_script_qgis, #qgis, #gis
In this tutorial, we demonstrate how to efficiently split a shapefile into multiple individual shapefiles based on an attribute, using Python scripting in QGIS. This method is perfect for tasks such as exporting individual country boundaries from a global shapefile. By leveraging the QGIS Python API, you can automate repetitive tasks, save time, and streamline your GIS workflow.
This step-by-step guide will walk you through:
Setting up the script in QGIS Python console
Exporting individual features to separate shapefiles
Ensuring that the original data remains intact
Whether you’re a GIS professional, student, or enthusiast, this tutorial will help you enhance your Python scripting skills and apply automation to your geospatial projects.
Key Highlights:
Automate repetitive GIS tasks
Learn QGIS Python API for efficient data management
Export data in the widely used ESRI Shapefile format
Keywords and Search Terms:
QGIS Python scripting, split shapefile by attribute, QGIS Python tutorial, shapefile export automation, GIS Python automation, QGIS shapefile split, Python for GIS, automate QGIS tasks, export shapefile by feature, QGIS for beginners
If you enjoyed this tutorial or found it useful, please give it a thumbs up and consider subscribing to the channel for more GIS and Python tutorials. Feel free to leave any questions or suggestions in the comments section below. Happy mapping!
-----------------------------------------
Code Used in the Tutorial:
----------------------------------------
from qgis.core import (
QgsVectorLayer,
QgsVectorFileWriter,
QgsFeature,
QgsCoordinateTransformContext
)
Get the active layer in QGIS
layer = iface.activeLayer()
output_folder = "C:/Users/user/Desktop/New folder/countries/" # Update to your desired folder
Loop through each feature in the layer
for feature in layer.getFeatures():
country_name = feature["name"] # Replace with the correct field name for country names
output_path = f"{output_folder}{country_name.replace(' ', '_')}.shp" # Avoid spaces in filenames
Convert WkbType to geometry type string
geometry_type = layer.geometryType()
geometry_string = {
0: 'Point',
1: 'LineString',
2: 'Polygon'
}.get(geometry_type, 'Unknown') # Adjust based on geometry type
Create an independent in-memory layer for the selected feature
country_layer = QgsVectorLayer(
f"{geometry_string}?crs={layer.crs().authid()}",
layer.name(),
"memory"
)
Add the same fields as the original layer
country_layer_data_provider = country_layer.dataProvider()
country_layer_data_provider.addAttributes(layer.fields())
country_layer.updateFields()
Add the current feature to the in-memory layer
new_feature = QgsFeature(feature)
country_layer_data_provider.addFeatures([new_feature])
Export the in-memory layer to a shapefile
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "ESRI Shapefile"
QgsVectorFileWriter.writeAsVectorFormatV3(
country_layer,
output_path,
QgsCoordinateTransformContext(),
options
)
print("Export completed!")