4

I am having trouble with using gdal rasterize under windows in a pyqgis script. The command runs fine under linux without specifying any RAST_EXT parameter. On a windows platform, however, the parameter apparently needs to be explicit (I get an error for the missing parameter value). The problem is that I don't manage to pass this argument to the command. I get every time an error of wrong parameter value.

I have seen the posts on this topic, including:

How to pass an extent parameter to a Processing algorithm in PyQGIS?

How to make QGIS processing algorithms use default parameter?

Calling gdalogr:rasterize algorithm in python console in QGIS?

None of the answers are working for me.

My code looks like this.

#Import the grid
pointsLayer = QgsVectorLayer(nm, "region", "ogr")

#Define resolution in meters
resGrid = 2000

#Create the name of the output raster
outName = re.sub(".shp",".tif",nm)


extent = pointsLayer.extent()
extent = (extent.xMinimum(), extent.xMaximum(), extent.yMinimum(), extent.yMaximum())

#Save to a temporary file
tempout = str(os.path.join(tempfile.gettempdir(),"temp.shp"))
QgsVectorFileWriter.writeAsVectorFormat(pointsLayer, tempout, "utf-8", None, "ESRI Shapefile")

first = processing.runalg("gdalogr:rasterize", {"INPUT":tempout, "FIELD":"serp_id", "DIMENSIONS":1, 
        "WIDTH": resGrid, "HEIGHT":resGrid, "TFW":False, "RTYPE":2, 
        "NO_DATA":0, "RAST_EXT":'%f,%f,%f,%f' %extent, "BIGTIFF": 2, "OUTPUT":None})
first = first['OUTPUT']

I tried to provide the extent in several ways

"RAST_EXT": extent
"RAST_EXT": '%f,%f,%f,%f' %extent
"RAST_EXT": '%f, %f, %f, %f' %extent
"RAST_EXT": '%f %f %f %f' %extent

I also tried to switch the order of the extent values to match 'xMin, yMin, xMax, yMax', with again all of the combinations above.

To no avail.

As the name indicates pointsLayer is a points shapefile (grid of points), it is in UTM projection.

Again, this code with the same shapefile works great under linux without specifying a RAST_EXT parameter.

3

You may try to manage the extent with the following lines:

extent = pointsLayer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

Once you have done this, you may insert the extent parameter in this way:

first = processing.runalg("gdalogr:rasterize", {"INPUT":tempout, "FIELD":"serp_id", "DIMENSIONS":1, 
        "WIDTH": resGrid, "HEIGHT":resGrid, "TFW":False, "RTYPE":2, 
        "NO_DATA":0, "RAST_EXT":"%f,%f,%f,%f" %(xmin, xmax, ymin, ymax), "BIGTIFF": 2, "OUTPUT":None})

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.