[gdal-dev] numpy array to raster

questions anon questions.anon at gmail.com
Tue Jan 31 21:20:05 EST 2012


thanks Frank, following your instructions with:
src_ds=gdal_array.OpenArray(myarray)
dst_ds =
gdal.GetDriverByName('GTiff').Create('E:/test/rasterise/mynewraster.tif',ncols,
nrows, 1 ,gdal.GDT_Byte)
dst_ds.SetGeoTransform(geotransform)
dst_ds.GetRasterBand(1).WriteArray(myarray)

I do not receive any error messages but the tiff produced are all just
zeros. Is there a step I am missing?
thanks

On Wed, Feb 1, 2012 at 12:07 PM, Frank Warmerdam <warmerdam at pobox.com>wrote:

> On Tue, Jan 31, 2012 at 4:38 PM, questions anon
> <questions.anon at gmail.com> wrote:
> > I need to output my numpy array as a raster so that someone else can
> access
> > the data in ArcGIS. So basically the steps I need are:
> > read numpy array into gdal
> > convert to raster
> > use latitude and longitude and array size to set projection
> >
> > I am really struggling with gdal because I can't seem to find enough
> > documentation about each step to understand what it is doing.
> > Here are some of the steps I think I need:
> >
> > myarray=myarray
> > #the extent and shape of my array
> > xmin,ymin,xmax,ymax=[139.8,-39.2,150.0,-33.6]
> > ncols,nrows=[193,106]
> > xres=(xmax-xmin)/float(ncols)
> > yres=(ymax-ymin)/float(nrows)
> > geotransform=(xmin,xres,0,ymax,0, -yres)
> >
> > from osgeo import gdal
> > from osgeo import gdal_array
> >
> > src_ds=gdal_array.OpenArray(myarray)
> >
> > dst_ds =
> >
> gdal.GetDriverByName('GTiff').Create('E:/test/rasterise/mynewraster.tif',ncols,
> > nrows, 1 ,gdal.GDT_Byte)
> > dst_rb = dst_ds.GetRasterBand(0)
> > dst_ds.SetGeoTransform(geotransform)
> > output = gdal.RasterizeLayer(dst_ds)
>
> Dear "Questions Anon",
>
> There are a variety of Python related information at:
>
>  http://trac.osgeo.org/gdal/wiki/GdalOgrInPython
>
> One serious issue with the above is that "gdal.RasterizeLayer()" is used to
> turn vector data into raster data - for instance to rasterize polygon
> features
> into an existing raster file.  I think you want to write your array into a
> raster file.  I think you can just call "dst_ds.WriteArray(myarray)".
>
> Alternatively if that method does not exist on the dataset, you
> can write the one band like:
>
>  dst_ds.GetRasterBand(1).WriteArray(myarray)
>
> Some of the samples referenced from the GdalOgrInPython should be
> helpful though I understand it can be hard to know where to look.
>
> Best regards,
> --
>
> ---------------------------------------+--------------------------------------
> I set the clouds in motion - turn up   | Frank Warmerdam,
> warmerdam at pobox.com
> light and sound - activate the windows | http://pobox.com/~warmerdam
> and watch the world go round - Rush    | Geospatial Software Developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20120201/c9e01243/attachment.html


More information about the gdal-dev mailing list