More

QGIS equivalent of inline variable substitution in ArcGIS?


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 beBuffer_[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?


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

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.


Watch the video: Model Bulider ArcGIS 23. استبدال متغير مضمن Inline variable substitution (October 2021).