I took one png file from the data_dir of GeoServer. Install GeoServer and you will have the same image to play with in directory:
\geoserver-2.7.1\data_dir\coverages\mosaic_sample\
Convert png into tiff and assign projection.
gdal_translate -a_srs epsg:4326 global_mosaic_6.png tiff.tiff
Input file size is 50, 50
0...10...20...30...40...50...60...70...80...90...100 - done.
Then use gdalwarp:
gdalwarp -cutline burn.shp -crop_to_cutline tiff.tiff cut.tif
Creating output file that is 16P x 15L.
Processing input file tiff.tiff.
Warning : the source raster dataset has a SRS, but the cutline features
not. We assume that the cutline coordinates are expressed in the destination SRS. If not, cutline results may be incorrect.
0...10...20...30...40...50...60...70...80...90...100 - done.
This is the result:
Burn.shp contains one polygon:
POLYGON (( 10.375610236185853 39.384332051098106, 10.344426149120759 39.730475417520644, 10.87455562922735 39.92069834861771, 11.152094004106685 39.546489303836594, 10.92133175982499 39.16292503293594, 10.375610236185853 39.384332051098106 ))
It appeared later from the comments that the cutline shapefile "polygon border200millas_polygon.shp" contained three polygons and gdalwarp seems to accept only one geometry as a cutline. However, it can take a multipolygon which can be make from polygons for example with the QGIS function "dissolve".
This thread is a little old. I thought it would be good to share my method for solving the issue presented. I have GEOS 3.8.0 installed so the ST_Union is pretty snappy. I haven't tried testing it on large datasets.
I use a function to determine the bounding tiles, merge those into one raster, and then apply a mask with a buffer returning the masked raster in the raster's original format. The code is specific to my current application and needs to be made more general, e.g. pulling rasters from a different table (not just ned_13), taking the raster table's SRID, and selecting the correct UTM zone's SRID based on the geometry's location.
CREATE OR REPLACE FUNCTION
ST_GeoMask ( geom GEOMETRY, buf DOUBLE PRECISION DEFAULT 60,
srid INTEGER DEFAULT 6345 )
RETURNS RASTER AS
$$
DECLARE
mask GEOMETRY := ST_Transform (
ST_Buffer (
ST_Transform( geom, srid ),
buf -- use rounded edge buffer the default
),
4269 -- mask in NAD83 coordinates EPSG::4269
);
rasterout RASTER;
BEGIN
SELECT
ST_Clip ( ST_Union ( rast ), mask, TRUE ) INTO rasterout
FROM public.ned_13
WHERE ST_Intersects( rast, geom );
RETURN rasterout;
END;
$$
LANGUAGE plpgsql RETURNS NULL ON NULL INPUT;
Best Answer
For the clipping of two layers, it is mandatory that both are stored to disk in the same CRS.
What you can do is:
Save As ...
choose a new filename and WGS84 as CRSYou can obviously do it the other way round too.