Monday, 9 March 2015

gdal - Applying gdalwarp cutline to PostGIS data


I can not figure out how I directly can read a polygon into gdawarp's -cutline argument.


My current approach is:


ogr2ogr -f "ESRI Shapefile" mydata.shp PG:"user=myuser dbname=gisdb" -sql "select * from polys where id='12345'"
and then I run


gdalwarp -cutline mydata.shp in.tif out.tif


How could I skip the step with the shapefile?



Answer



If you're using GDAL 1.8.0 (which I recommend because it adds a number of useful features), you can use:


gdalwarp -cutline "PG:dbname=gisdb" -csql 'select * from polytest where id=1' -crop_to_cutline -of GTiff -srcnodata -9999 -dstnodata -9999 src.tif dest.tif


Note the "-crop_to_cutline" parameter.


With gdalwarp, I have found it always pays to use -srcnodata and -dstnodata explicitly, otherwise it'll fill the nodata areas with 0/black which quite often is a valid value.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...