Using gdalwarp to effectively trim an image

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using gdalwarp to effectively trim an image

drshinnick
This post was updated on .
Hello everyone!

Regarding gdalwarp included in 1.9

I'm hoping someone can shed some light on the use of gdalwarp for me.  I'm trying to use it to effectively trim an mosaicked image of its sawtooth edges by masking the area outside a line as defined by a shapefile.  I've been successful when using a single polygon such as with this command here:

gdalwarp -cutline .\shp -cl 1642 -crop_to_cutline .\1642\1642.bmp .\cutlinetest.tif

Where 1642.shp is located in the .\shp directory.  1642.shp is a single polygon record extracted from a larger multi-record shape file know as 'allROIS.shp' also located at .\shp and contains a field named 'item'.

The problem is that I have a whole mess of these things that i need to trim.  So I was trying to use the -csql switch to reference the particular record within 'allROIs.shp' but I'm not having much luck.  The command i'm issuing is:

gdalwarp -cutline .\shp -csql "select * from allROIs where item = 1642" -crop_to_cutline .\1642.bmp .\cutlinetest.tif

The error I'm receiving is:

Cannot compute bounding box of cutline.

The individual polygons within allROIs.shp overlap but are separate records.  Not sure if that's significant.

Any insight is appreciated.  Thanks.

***Update***

I have also tried using the -cwhere switch in gdalwarp without luck.  I have however been able to use ogr2ogr to extract the individual polygons using ITS -where switch specifying

-where "Item = 1642"

I had hoped that the syntax would be the same in gdalwarp.  But using

gdalwarp -cutling .\shp -cl allROIS -cwhere "Item = 1642" -crop_to_cutline .\1642\1642.bmp .\cutlinetest.tif

yields the same "Can not Compute Bounding Box" error.

My option at the point seems to be to extract the individual polys programatically and use them to trim.  

***Update***



-Dan

P.S.  If this is the wrong forum for this post please advise of that as well.  Thanks again.
Reply | Threaded
Open this post in threaded view
|

Re: Using gdalwarp to effectively trim an image

drshinnick
The problem seems to have been related to the presence of a .prj file with the .shp file.

using the command

gdalwarp -cutline .\shp -cl allROIs -cwhere "Item = 1642" .\1642\1642.bmp .\1642.tif

seems to fail to compute cutline bounding box when there is a .prj file present for the .shp and the target image has no georeference embeded as with a geotiff.  Here in this case I was trying to trim a .bmp so that was that I think was the reason.

However remove the .prj from the .\shp directory and it works!  Alternatively, I can specify the -s_srs and -t_srs as the same and this also works although it does throw a warning that the input vector layer has an srs while the raster does not.  As long as the two are in the same projection.  No problem I think.


-Dan