0

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

  • Haven't played around with your script, but you declare coords as the empty list, and append 4 floats to it. Later on you reference list elements as iterable (lines starting with point1=..., point2=...). This should raise an exeption. – Detlev Dec 10 '17 at 7:14
  • Hi, as you can see in the last line i commented out the function to add the temp layer i created to qgis as this was not needed for my script. This works and the layer is correctly added with the 2 points and correct dimensions but im only intrested in getting the new extent out to another tool in the toolbox. So there seems to be no python problem with the code. – Sonic Dec 10 '17 at 19:21
0

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

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.