3

I have a shapefile layer from a vegetation mapping project with 417 polygons that represent 21 different vegetation classes. These are stored in a single column called "type".

I want to break the .shp file down into each different vegetation class, so that I end up with 21 .shp layers, one for each class.

How can I do this efficiently in QGIS?

I have added a screenshot of the shapefile (from MapInfo, but I want to use QGIS for the disaggregation).

Forest types, currently all on one layer

2
  • 1
    If you don't want to use python you can select by each unique 'type' and export selected records only by right clicking on the layer... 21 exports should take about 10 minutes which is less than it would take to write a script. Jun 17, 2015 at 2:07
  • 1
    Or use GDAL ogr2ogr -f "ESRI Shapefile" type_1.shp input.shp -sql "select * from input where type=1"
    – user30184
    Jun 17, 2015 at 6:49

2 Answers 2

11

Use the "Split Vector Layer" processing tool called via Vector > Data Management Tools > Split Vector Layer....

In the dialog that opens, just choose the column with your attribute for the unique "ID".

0
0

I know you said no Python, but this is an easy code you can use in the Python console in QGIS, I will make the comments shown by # where you have to change some names according to your project.

  • Change Your_shapefile for the name of your shapefile
  • Change: ATTRIBUTE = the column name where you want to extract the polygon. Change: VALUE = the value you want to extract
  • Change the path C:\Users\Extracted_values where you want to store the new shapefile, and keep the ending .shp
layers = QgsProject.instance().mapLayersByName("Your_shapefile")  # Change Your_shapefile for the name of your shapefile
if len(layers) > 0:
    layer = layers[0]
    layer.selectByExpression('"ATTRIBUTE" = VALUE') # Change : ATTRIBUTE = 
  the column name where you want to extract the polygon.  Change: VALUE = the value you want to extract 
    fn = r"C:\Users\Extracted_values.shp" # Change the path where you want to store the new shapefile
    writer = QgsVectorFileWriter.writeAsVectorFormat(layer, fn, 'utf-8', driverName='ESRI Shapefile', onlySelected=True)
    del writer
    Selected_layer = iface.addVectorLayer(fn, '', 'ogr')
else:
    print("Layer 'Your_shapefile' not found in the project.") #
2
  • I would probably adjust this piece of code layer.selectByExpression('"ATTRIBUTE" = VALUE'), to make it a for-loop, where each attribute will be selected automatically, instead of manually.
    – Taras
    May 23, 2023 at 8:00
  • Like here: gis.stackexchange.com/a/361673/99589
    – Taras
    May 23, 2023 at 8:04

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