4

I have a polygon shapefile that has several attributes.

I would like to know how can I create automatically a vector file for each one.

I already find split vector layer in QGIS (like picture):

enter image description here

but I want to use python.

I found a way through the web site at the following site, but this caused an error in QGIS.

https://docs.qgis.org/2.8/en/docs/user_manual/processing_algs/qgis/vector_general_tools/splitvectorlayer.html

enter image description here

>>> import processing
>>> processing.runalg('qgis:splitvectorlayer')
ERROR: Algorithm not found

So I need another way to split..

underdark
  • 84,148
  • 21
  • 231
  • 413
GOME
  • 41
  • 3
  • Have you tried it with the parameters that are required? – Michael Stimson Mar 08 '18 at 04:19
  • Have a look at this though https://gis.stackexchange.com/questions/40798/how-to-split-shapefile-per-feature-in-python-using-gdal – joseph_k Mar 08 '18 at 04:24
  • I tried setting the parameters, but an error occurred in qgis that I could not find the "qgis: splitvectorlayer" function. – GOME Mar 08 '18 at 05:18
  • Can you provide more information like which version of QGIS is being used and the Operating system (Windows/Linux/Mac)? After import processing try running processing.alglist() to get the list of available algorithms. Maybe there is some issue with the import command. – nash Mar 08 '18 at 06:27
  • Perhaps you should update your QGIS Version, 2.6 is long long ago... Do you see 'Split vector layer-->qgis:splitvectorlayer' when you type >>>processing.alglist() on the console? – Eva Großmann Mar 08 '18 at 07:41
  • No.. right. so I need another method to split like '1 Answer' – GOME Mar 08 '18 at 08:01

2 Answers2

2

I'm not familiar with PyQGIS but this can be easily accomplished using ogr.

The following script shows an example:

from osgeo import ogr

fn = r""  # Vector Layer File path
driver = ogr.GetDriverByName('ESRI Shapefile')  # See OGR Vector Format for other options
dataSource = driver.Open(fn)
layer = dataSource.GetLayer()
sr = layer.GetSpatialRef()  # Spatial Reference

dst = r""  # Output directory
new_feat = ogr.Feature(layer.GetLayerDefn())  # Dummy feature

for id, feat in enumerate(layer):
    new_ds = driver.CreateDataSource(r"{}\feat_{}.shp".format(dst, id))
    new_lyr = new_ds.CreateLayer('feat_{}'.format(id), sr, ogr.wkbPolygon) 
    geom = feat.geometry().Clone()
    new_feat.SetGeometry(geom)
    new_lyr.CreateFeature(new_feat)

    del new_ds, new_lyr

You can, of course, change the name of the driver (in case your layer is not a shapefile) and the geometry of your new files (in case the input vector layer file is not a polygon).

More information about ogr vector formats here: http://www.gdal.org/ogr_formats.html

Marcelo Villa
  • 5,928
  • 2
  • 19
  • 38
  • Thank you very much. However, I need to keep the properties of the shapefile or sort by the table name (ex: "polygonID" or "area_number"). – GOME Mar 08 '18 at 05:16
  • If you use the code, it seems that the ID is being created from the top of the canvas instead of following the Vector Layer file(input). – GOME Mar 08 '18 at 05:24
1

Please check for spaces in your code.

I dont know much of your problem, but this may be useful to you and anyone that came looking for this answer, just like me. This is a compiled of commentaries given by Eva and Nash. The codes i made was with python 2.7 with QGIS 2.18 in Windows 8.1. Try it using the python console inside QGIS.

split vector layer separates a shapefile file in multiple ones, separating them by one atribute only. I dont know if that is what you want.

For running the functions, you are missing the parameters. You should do:

import processing

layer_name = 'my_shapefile_layer'
output_directory = 'C:\Users\Desktop\shapes'
atribute = 'id'    
processing.runalg('qgis:splitvectorlayer',layer_name,atribute,output_directory)

if you dont know what parameters the function has, just call:

processing.alghelp('qgis:splitvectorlayer')

if you like to use another function, look for it using:

processing.alglist()
underfloor
  • 201
  • 1
  • 9