I have a roads shapefile. In its name
field, I'll create a new shapefile per attribute. I already got each unique attribute under name
. The for-loop
that iterates over the unique attributes and selects the features isn't working.
from qgis.core import QgsProject
def split_shp():
# Parameters
layer_name = 'bicolRoads_primary_secondary'
field_name = 'name'
layer = QgsProject.instance().mapLayersByName(layer_name)[0] # road shp
shp_index = layer.fields().indexOf(field_name) # road shp index
unique_values = layer.uniqueValues(shp_index) # unique attributes under 'name' field
for i in unique_values: # for-loop that iterates over the unique attributes
dog = layer.selectByExpression('field_name=i') # selects features matching i/current attribute
print(dog)
#shp_writer = QgsVectorFileWriter.writeAsVectorFormat(layer, new_shp, 'utf-8', \
#driverName='ESRI Shapefile', onlySelected=True)
split_shp()
The result is this
res = processing.run("native:splitvectorlayer", {'INPUT':'/path/to/data.shp','FIELD':'unique_field','FILE_TYPE':1,'OUTPUT':'/my/folder/'})
,print(res['OUTPUT_LAYERS'])
QgsProcessingException: Error: Algorithm native:splitvectorlayer not found
. The current documentation doesn't also show what's the keyword nor the log when running the Split Vector Layer tool."native:splitvectorlayer"
, I used"qgis:splitvectorlayer"
but it saves per shapefile into a geopackage which is why I opted to make my own script instead of using the tool. Thanks anyways!