4

I am working through some analysis in QGIS and need to iterate through a layer of points and buffer each point and then export to a new layer. I want the new layer names to have the values from a field in my attribute table.

I have created a GIS friendly text field with no spaces: "GISNAME". I am using the fixed distance buffer tool and clicking on the iterate over layer button. Is there a way to make each exported name be Buffer_[value from GISNAME field].shp?

In ArcGIS I would use modelbuilder and set up inline variable substitution to achieve this. I am using QGIS and wanted to know if I can do this here?

3

You probably could do it though the QGIS modeler but the following code should hopefully do what you're looking for (tested on QGIS 2.8.2-Wien).

You can run this from the Python console or add it as a script in the Processing Toolbox.

The only thing is to add a folder called "Results" on your desktop (or change the path in the script) to where you want the shapefiles to be saved. Also, the parameters you really need to change are the Distance (100) and Segments (10) in the final line to whatever it is you need:

import processing, os

from os.path import expanduser
home = expanduser("~")

path_res = home + "/Desktop/Results//"       # Sets directory for output

layer = qgis.utils.iface.activeLayer()
for feat in layer.getFeatures():
    idx = layer.fieldNameIndex('GISNAME')    # Locates field 'GISNAME'
    value = feat.attributes()[idx]           # Fetches field value
    output_name = "Buffer_" + str(value)     # Converts value to string in order to insert it as file name

    # Extract each point using Random Extract tool 
    output_0 = processing.runalg("qgis:randomextract", layer, 0, 1, None)    

    # Buffers the extracted point and saves output as shapefile
    output_1 = processing.runalg("qgis:fixeddistancebuffer", output_0['OUTPUT'], 100, 10, False, path_res + output_name + ".shp")
4
  • 1
    Thank you. I was kind of hoping it would be something I could just type into the buffer output file name but I can see it can not be that simple. – GubbinsG Sep 16 '15 at 13:52
  • @GubbinsG - Hopefully others can advise for a more simpler solution, I could easily have overlooked something :) – Joseph Sep 17 '15 at 10:52
  • 1
    @Joseph, if you use forward slashes "/" instead of backslashes "\" the code will be portable and Windows will still happily accept it. – Matthias Kuhn Sep 17 '15 at 11:22
  • @MatthiasKuhn - Many thanks buddy for the brilliant suggestion! – Joseph Sep 17 '15 at 11:25
1

There is some kind of "hackish" way to achieve sth similar.

There is the "Split vector" function, and thats actually what you like to achieve, no?

So, make your buffers with the toolbox, normally your temporary layer name should be "Buffer". Otherwise just rename it under layer properties/General.

Then rename your "GISNAME" field to "_" with the Table Manager plugin.

When you now use the "Split Vector" function, pick the layer "Buffer" and the field "_" and choose your destination.

The result will be files named: "Buffer__A", "Buffer__B" ... with two times "_".

This is, because the function takes the layer name, the field name and the attribute, and puts underscores between them to generate the result name. If you can live with two "_", thats the way to go.

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.