[GIS] gdalwarp cutline without using a shapefile

gdalgdalwarpshapefilewell-known-text

I'm currently working with gdalwarp to split some tiffs.

gdalwarp -cutline file.shp -dstalpha -of GTiff image.tif split_image.tif

I wanted to know if I can use an other source rather than use a shapefile, for example I'd like to use a WKT entry, something like POLYGON ((0 0, 0 1, 1 1 , 1 0, 0 0))

Best Answer

It should be possible by using the -csql parameter http://www.gdal.org/gdalwarp.html and SQLite dialect http://www.gdal.org/ogr_sql_sqlite.html. Gdalwarp does not have an option for selecting the SQL dialect so you must help it by using some SpatiaLite database as a fake source for the cutline. GDAL selects automatically SQLite dialect when it reads SpatiaLite. The database must exist but it does not matter what it contains.

gdalwarp  -cutline foo.sqlite -csql "select ST_GeomFromText('POLYGON ((
0 0, 0 1, 1 1 , 1 0, 0 0))')" -dstalpha -of GTiff image.tif split_image.tif

BTW. your polygon was invalid because it was not closed. Using .csv file as source for cutline is straight forward as well.