Friday, 27 September 2019

Saving layer as temporary file usig PyQGIS?


I am writing a script to automate some processes which involved producing some layers like this:


Input -> Do sth on Input -> Layer A -> Do sth on Layer A-> Layer B -> Do sth on Layer B -> output


My approach is to save Layer A, B and the output as shapefile



However, layer A B are actually useless after the process is finished. I don't want to save them as shp but as temporary layer like below:


But I want to do it via PyQGIS


enter image description here



Answer



You need to do something like this:


##output=output vector

from qgis.core import *
from PyQt4.QtCore import *


input = 'C:/path_to_the_file/test.shp'
layer = QgsVectorLayer(input, 'input' , 'ogr')

fill = processing.runalg("qgis:fillholes", input, 100000, None)

# Get the output from fill as object for further operations
layerA = processing.getObject(fill['Results'])


fill_2 = processing.runalg("qgis:fillholes", layerA, 100000, None)


# Get the output from fill_2 as object for further operations
layerB = processing.getObject(fill_2['Results'])


fill_3 = processing.runalg("qgis:fillholes", layerB, 100000, output)

In this example, I run the Fill holes algorithm for three times: in the first and the second operation, I save layerA and layerB as temporary layers, while in the last one I save the result to the disk.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...