[gdal-dev] Re: Creating a blank raster from scratch

Jorge jorge.arevalo at gmail.com
Tue Feb 3 06:39:45 EST 2009

Hello again,

I' ve found the problem, I think. Is the version of numpy used by
gdalnumeric. The code from FAQ works with numpy version 1.0.1, but fails
with numpy version 1.2.1. The fail is because the narray attribute
"typecode()" doesn't exist. From the numpy book, available at
http://numpy.scipy.org/numpybook.pdf, you can get (page 55):

"If you are converting code from Numeric, then you
will need to make the following (search and replace)
conversions: .typecode() --> .dtype.char;
.iscontiguous() --> .flags.contiguous;
.byteswapped() --> .byteswap(); .toscalar()
--> .item(); and .itemsize() --> .itemsize. The
numpy.oldnumeric.alter code1 module can automate this for you."

Ok. I've opened gdalnumeric.py, and the last change was from 2006/03/21.
The older version from numpy available at sourceforge download's page is
from 2007-11-08. Then, gdal is using a really old version of numpy.
Maybe I can use the numpy 1.0.1 from my mac, but I think that it would
be easier to replace the snippet's code lines that uses numpy from the
correct ones. What's the best way to do this

        # Create blank raster with fully opaque alpha band
        zeros = numpy.zeros( (tiff_height, tiff_width), numpy.uint8 )
        dst_ds.GetRasterBand(1).WriteArray( zeros )
        dst_ds.GetRasterBand(2).WriteArray( zeros )
        dst_ds.GetRasterBand(3).WriteArray( zeros )
        opaque = numpy.zeros((tiff_height,tiff_width), numpy.uint8 )*255
        dst_ds.GetRasterBand(4).WriteArray( opaque )

by using gdal? Because, if I'm right, this snippet from the FAQ is


Jorge escribió:
> Hello everybody
> I'm creating a blank raster from scratch, to rasterize it later. Ok,
> I've read the FAQ:
> http://trac.osgeo.org/gdal/wiki/FAQRaster#HowcanIcreateablankrasterbasedonavectorfilesextentsforusewithgdal_rasterize
> I've executed this code with no problems in a Mac, with Leopard. I've
> used the frameworks at http://www.kyngchaos.com/software:frameworks. The
> version of GDAL is 1.6.0, and the numpy version is 1.0.1 Now, I try to
> execute the code in another machine, a PC with Suse Linux Enterpise 11.0
> for 64bits processors. I've installed gdal and numpy from repositories.
> The version of GDAL is 1.6.0, and numpy version is 1.2.1  So, the gdal
> versions are the same. But if I open, for example, the file ogr.py in
> both systems, are different files. Maybe the Framework I'm using is a
> port of GDAL 1.6.0 to Mac with any changes?
> And another issue: the code fails in the PC with Suse. The first error is:
> src_lyr = src_ds.GetLayerByIndex( index = 0 )
> AttributeError: DataSource instance has no attribute 'GetLayerByIndex'
> I could solve the problem by changing the call to "GetLayerByIndex(
> index = 0 )" for "GetLayer( 0 )". Why is this happening, if the version
> of GDAL is 1.6.0 on both systems? Again, I think that maybe the
> Framework I'm using is a port of GDAL 1.6.0 to Mac with any changes.
> Then, made this change, I get another error, that I couldn't solve yet
> (create_raster.py is the name of my file):
>  File "./create_raster.py", line 112, in ?
>     dst_ds.GetRasterBand(1).WriteArray( zeros )
>   File "/usr/lib64/python2.4/site-packages/gdal.py", line 876, in WriteArray
>     return gdalnumeric.BandWriteArray( self, array, xoff, yoff )
>   File "/usr/lib64/python2.4/site-packages/gdalnumeric.py", line 193, in
> BandWriteArray
>     datatype = NumericTypeCodeToGDALTypeCode( array.typecode() )
> AttributeError: 'numpy.ndarray' object has no attribute 'typecode'
> So, basically, the method "BandWriteArray" of gdalnumeric.py is
> expecting an object array, with a method named "typecode()", but the
> array created with numpy.zeros, don't have this attribute. Am I right?
> Then, the fact is that in version 1.0.1 of numpy, the array created with
> numpy.zeros has a method named "typecode()", but in version 1.2.1, this
> attribute doesn't exist. Do I have to "downgrade" the numpy version from
> 1.2.1 to 1.0.1 to make the code works in suse? Is there another
> solution? Sorry for this kind of questions, but I'm very newbie with Python
> Thanks in advance!
> Regards
> Jorge

"Afirmo con total convicción que la religión cristiana organizada como Iglesia ha sido y es aún la principal enemiga del progreso moral en el mundo"

"La religión se basa, pienso, principal y primariamente en el miedo. El miedo es el padre de la crueldad, y por tanto no es sorprendente que crueldad y religión han ido tomadas de la mano. La ciencia puede ayudarnos a superar este cobarde temor en que ha vivido la humanidad por tantas generaciones." 

(Bertrand Russell)

More information about the gdal-dev mailing list