3

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..

  • Have you tried it with the parameters that are required? – Michael Stimson Mar 8 '18 at 4:19
  • Have a look at this though gis.stackexchange.com/questions/40798/… – joseph_k Mar 8 '18 at 4:24
  • I tried setting the parameters, but an error occurred in qgis that I could not find the "qgis: splitvectorlayer" function. – GOME Mar 8 '18 at 5: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 8 '18 at 6: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 8 '18 at 7:41
1

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

  • 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 8 '18 at 5: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 8 '18 at 5:24
0

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.