TransWikia.com

QGIS - Script for Process Modeler, Output = Extent

Geographic Information Systems Asked by Sonic on October 6, 2020

just played arround a little bit with the process modeler and scripts to automate a simple workflow.

I have created a simple script that should get the extent of the current layer, and then increase the extent a bit by a input number in both x and y directions equally.

My problem is that i cant pass this new extent to the gdal rasterize tool in the process modeler, there seems to be no valid output of my script that i can connect with the corresponding input field of the gdal tool.
The built in tool to get the feature extent works fine.

##point_layer=vector point
##cell_size=number 0.2
##new_extent=output extent

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

inlayer = processing.getObject(point_layer)

ext = inlayer.extent()

coords = []

coords.append(['xmin', ext.xMinimum()])
coords.append(['xmax', ext.xMaximum()])
coords.append(['ymin', ext.yMinimum()])
coords.append(['ymax', ext.yMaximum()])

print coords

for coord in coords:
    if 'min' in coord[0]:
        coord[1] = coord[1] - (cell_size / 2)
    if 'max' in coord[0]:
        print coord[0]
        coord[1] = coord[1] + (cell_size / 2)
    print coord[1]

print coords

#extentcoords = "%f,%f,%f,%f" %(coords[0][1], coords[1][1], coords[2][1], coords[3][1]) # this is a string that stores the coordinates

# create a memory layer with two points
layer =  QgsVectorLayer('Point', 'points' , "memory")
pr = layer.dataProvider() 
# add the first point
pt = QgsFeature()
point1 = QgsPoint(coords[0][1],coords[2][1])
pt.setGeometry(QgsGeometry.fromPoint(point1))
pr.addFeatures([pt])
# update extent of the layer
layer.updateExtents()
# add the second point
pt = QgsFeature()
point2 = QgsPoint(coords[1][1],coords[3][1])
pt.setGeometry(QgsGeometry.fromPoint(point2))
pr.addFeatures([pt])
# update extent
layer.updateExtents()
# add the layer to the canvas

#QgsMapLayerRegistry.instance().addMapLayers([layer])
new_extent  = layer.extent()

I know this script is needlessly complicated with the python lists and so on 😉

One Answer

You could try using:

processing.runalg('qgis:polygonfromlayerextent', input_layer, by_feature, output)

see QGIS documentation for more information. This will give you a polygon you can then feed into gdal

Answered by firefly-orange on October 6, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP