GDALWarp – Using Spatial Functions Inside -csql for Data Transformation

gdalwarpgeopackage

I'm using the following command to cut a GeoPackage from a GeoTIFF file:

gdalwarp -of GTiff -cutline input_database.gpkg -crop_to_cutline input_raster.tif output_raster.tif

Additionally, I know I can use -csql for making queries inside my GeoPackage database:

gdalwarp -of GTiff -cutline input_database.gpkg -csql "select geom from database" -crop_to_cutline input_raster.tif output_raster.tif

However, if I try to use any spatial function inside this query (like the st_buffer() function):

gdalwarp -of GTiff -cutline input_database.gpkg -csql "select st_buffer(geom,5) from database" -crop_to_cutline input_raster.tif output_raster.tif

I receive the error ERROR 1: Failed to identify source layer from datasource. While opening the GeoPackage with Spatialite, I can make this query work with this solution. One of them is adding the function GeomFromGPB() on my geometries, but I've tested the last command with it and it didn't work as well. What am I missing? How can I use spatial functions on GeoPackages while using gdalwarp to cut the new geometry from a GeoTIFF file?

Best Answer

As stated in the comments, this can be done in two lines. Using first ogr2ogr and then gdalwarp:

ogr2ogr -f GPKG -sql "select st_buffer(geom,5) from database"  bufferedOutput.gpkg input.gpkg
gdalwarp -of GTiff -cutline bufferedOutput.gpkg -crop_to_cutline input_raster.tif output_raster.tif

This is a functional solution. However, I didn't manage to use a query with gdalwarp where I can use spatial functions with the -csql parameter.

Related Question