[GIS] gdal_rasterize error: “attempt to create 0x0 dataset is illegal..”

gdalrasterization

I'm trying to use gdal_rasterize to convert a polygon shapefile into a raster, where the value comes from the 'COVTYPE' attribute. I try calling gdal rasterize like so (I'm calling from within a python script so I have to call os.system):

os.system("gdal_rasterize -a COVTYPE -l %s -a_nodata 999 -a_srs EPSG:102001 -t 30 30 %s %s" % (bname, inshp, outrast))

Where bname is the basename of the shapefile (aka the layer name), inshp is 'inpath/bname.shp' and outrast is 'outpath/bname.tif'.

And get the error : "ERROR 1: Attempt to create 0x0 dataset is illegal, sizes must be larger than zero. Cannot create outpath/bname.tif"

It was my understanding that with gdal>1.8 (I'm running gdal 1.9.2), gdal_rasterize could create a new raster file as long as -tr (target resolution) or -ts (target size) is set (see http://www.gdal.org/gdal_rasterize.html). I'm setting the target resolution, so I don't see why it thinks I'm trying to create a 0x0 raster. Additionally, documentation says that if the target extent isn't set, it'll take the extent of the vector layer, which is want I want.

I've tried calling gdal_rasterize outside of python and get the same error. I also tried rearranging some of the keywords and taking away the a_srs keyword thinking it may be causing problems (a_srs overrides the default projection for the output file), but still to no avail.

Does anyone have an inkling as to what's going on?

Best Answer

So after using gdal_translate to reproject the shapefile first, then using that as the input for gdal_rasterize, everything worked. I'm assuming that gdal didn't recognize the shapefile's projection (it wasn't anything I'd seen either) and that just caused the whole thing to fail. It makes sense, but I wish the error raised would've pointed to that, and I could have saved a lot of time. So, the final solution is:

os.system("ogr2ogr -t_srs EPSG:102001 %s %s" % (rpshp, inshp)) # output then input
os.system("gdal_rasterize -a COVTYPE -l %s -a_nodata 999 -tr 30 30 %s %s" % (rbname, rpout, outrast)) # layername (basename of reprojected shp), input then output