[GIS] Problem to clip a raster with a shapefile

qgisrastershapefile

I need to clip a raster layer with a vector layer. When I try this, the following error appears:

**ERROR 1: Attempt to create 0x0 dataset is illegal,sizes must be larger than zero.** 

What am I doing wrong?

Best Answer

The problem was in the source files. If you open one of those files (i.e. map-3), you'll see that they have 4 bands. I think the four band is an alpha channel (for transparency) added by some photoediting software (photoshop, gimp). To drop the 4th band on the merged image, use the raster conversion tool and edit the command line by hand:

gdal_translate -b 1 -b 2 -b 3 sourcefilename.tif bands.tif

Now, you have a 3 band raster. Then, you'll see that the clipper tool won't work. Why? I don't know (I think source file are corrupted). So, use another strategy: rasterize the polygon in the rav.shp file (I'll call ravrast.tif the raster version), and then use the raster calculator to extract the values of the 3 bands:

output file: band1.tif; expression: bands@1 * ravrast@1 output file: band2.tif; expression: bands@2 * ravrast@1 output file: band3.tif; expression: bands@3 * ravrast@1

Now, you have to merge these three files. Use the Raster/Miscellaneus/Union menu voice.

The final command of this tool should appear as follows:

gdal_merge.py -separate -of GTiff -o united.tif band1.tif band2.tif band3.tif

Now, united.tif will contain the clipped 3 bands image you were searching, but there is one last step, in order to remove the black color for notada cells. From the command line, type:

rgb2pct.py -n 256 united.tif final.tif

(I'm on linux, maybe on Windows it can be rgb2pct.bat)

Load the final.tif file on QGIS, ask for pixel values in the black area of the image, then go to the layer properties and set nodata or transparent pixel values to this number (in my case (the file that I attached).

The final file is a pseudo-colored tif file. You can edit it on QGIS or on gimp and save it an another format.

Bye,

Diego

Related Question