I am attempting to call gdalwarp (python binding gdal.Warp()) as a script inside QGIS modeler (QGIS 2.18.10, gdal 2.2.0). The goal is to correlate any loaded raster with a UTM zone grid, get the correct EPSG code from that grid, and pipe that code as a dynamic value into gdal.Warp().
To do this, I have written a geoprocessing script because the existing Warp tool does not allow one to dynamically set the EPSG code.
I have looked through the QGIS geoprocessing documentation, but it is a bit thin on examples, which makes troubleshooting difficult.
Here is what I have so far:
##Raster_Layer=raster # Raster to be reprojected
##Joined_Layer=vector # Layer containing the correct EPSG code under the 'EPSG' field
##Output_Raster=output raster
from qgis.core import *
from osgeo import gdal
def get_epsg(lyr):
for field in lyr.fields():
fname = field.name()
if fname == 'EPSG':
epsg_code = field.value()
return epsg_code
layer = processing.getObject(Joined_Layer)
srs = get_epsg(layer)
rlayer = processing.getObject(Raster_Layer)
input_raster = gdal.Open(rlayer)
output_raster = Output_Raster
srs_string = 'EPSG:{}'.format(str(srs))
gdal.Warp(output_raster, input_raster, dstSRS=srs_string)
I am sure I'm missing some QGIS-specific wrappers for the api calls, but have no clue where to look for a thorough documentation on using gdal in QGIS geoprocessing scripts (if such a thing exists).
processing.getObject()and the##Output_Raster = output rasterare particular to QGIS geoprocessing and allow you to write scripts that will offer users a gui tool that accepts inputs or lets them direct output, inside of QGIS. – auslander Oct 23 '17 at 15:51