[gdal-dev] Re: Incorrect blank raster example in the FAQ?

Markus Reinhold markus.reinhold at uni-jena.de
Mon Oct 5 11:39:43 EDT 2009

Hej Simon,

> Instead of:
> raster_transform = [src_extent[0], px, 0.0, src_extent[3], 0.0, -px]
> I used:
> x_scale = (src_extent[1] - src_extent[0]) / tiff_width
> y_scale = (src_extent[3] - src_extent[2]) / tiff_height
> raster_transform = [src_extent[0], x_scale, 0.0, src_extent[3], 0.0, -y_scale]
> I'm not using the px variable at all.

There's a note on the given FAQ page <http://tinyurl.com/y9qmxsq> 
stating that a user should adjust the px variable to meet her desired 
raster resolution and dimensions. That's exactly what you've done with 
your calculations above. You've set the desired height and width of your 
result (somewhere) beforehand and calculated the corresponding raster 
resolution according to the spatial extent of the input shapefile. That 
is a fair solution, even though you may end up with a raster dataset not 
having square pixels. Which is fine, but not very common.
Calculating the size of the dataset using the desired pixel size is IMHO 
a much more frequently used concept. It could look like the following 

# setting pixel resolution
px = 0.5

# calculating raster size from extent and desired pixel size
# using math.ceil to make it a little bit bigger than actually necessary
# equations are just transformed versions of your ones
tiff_width = int(math.ceil(abs(src_extent[0] - src_extent[1]) / px))
tiff_height = int(math.ceil(abs(src_extent[3] - src_extent[2]) / px))

# creating new raster layer
raster = gdal.GetDriverByName('GTiff')
dst_ds = raster.Create(tiff, tiff_width, tiff_height, 1, gdal.GDT_Byte)

# creating raster geo transform based on upper left corner and desired pixel
# now using a consistent pixel resolution
raster_transform = [src_extent[0], px, 0.0, src_extent[3], 0.0, - px]

I hope this will help to answer your question.


Dipl.-Kartograph Markus Reinhold

Chair of Geoinformatics, Geohydrology and Modeling
Friedrich-Schiller-University Jena
Löbdergraben 32
D - 07743 Jena

Phone.: (+49)(0)3641 / 9 488 65
E-Mail: markus.reinhold at uni-jena.de

More information about the gdal-dev mailing list