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

asked Dec 9, 2017 at 16:02
2
  • 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. Commented Dec 10, 2017 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. Commented Dec 10, 2017 at 19:21

1 Answer 1

1

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 Dec 15, 2017 at 12:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.