[gdal-dev] saving numpy array as ascii raster

Oz Nahum oz.nahum at student.uni-tuebingen.de
Thu Mar 5 14:34:02 EST 2009


Hello Again,

I have been googling around and reading how to save my numpy arrays to
rasters. So far I have not found a satisfying answer.
The closest thing to what I'd would like to do is found here: *
http://n2.nabble.com/basic-raster-math-td2033308.html#a2033316*

When I try do to the following:

from osgeo import gdal
from osgeo.gdalconst import *
dataset = gdal.Open('raster1.asc', GA_ReadOnly)
dataset1 = gdal.Open('raster2.asc', GA_ReadOnly)
print dataset, dataset1
from osgeo import gdal_array
from osgeo import osr
a = gdal_array.DatasetReadAsArray(dataset)
b = gdal_array.DatasetReadAsArray(dataset1)
print a
print b
def WriteRaster (dst_filename, raster):

        format = "GTiff"
        driver = gdal.GetDriverByName( format )
        dst_ds = driver.Create( dst_filename, 71, 73,\
               1,gdal.GDT_Float32,options=["COMPRESS=PACKBITS","TFW=YES"] )
        dst_ds.SetGeoTransform( [-19.5, 1.0, 0.0, 37.5, 0.0, -1.0] )
        srs = osr.SpatialReference()
        srs.ImportFromEPSG(4326) #WGS84 lat long.
        dst_ds.SetProjection( srs.ExportToWkt() )
        dst_ds.GetRasterBand(1).WriteArray( raster )
        dst_ds = None


c= a+b
print c
WriteRaster ('raster3.gtiff', c)

A file with following content is created:

1.0000000000
0.0000000000
0.0000000000
-1.0000000000
-19.0000000000
37.0000000000

The rasters originally used are ascii type, and this are their contents:
raster1.asc: raster2.asc:
ncols         6
nrows         4
xllcorner     3366820.0000019
yllcorner     5814589.0300007
cellsize      10
NODATA_value -9999
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 -9999 -9999

So I would expect a file containing just:
  2      2      2      2      2      2
  2      2      2      2      2      2
 2      2      2      2      2      2
2      2      2      2 -19998 -19998


I would appreciate it if some one can supply a code snippet how to
successfuly save numpy arrays into ascii raster.

Thanks again,
Oz Nahum
Eberhard Karls Universität Tübingen
Grad-student
Applied Environmental Geosciences
Institut für Geowissenschaften
-- 

---
when one person suffers from a delusion it is called insanity. When many
people suffer from a delusion it is called religion."
Robert Pirsig, Zen and the Art of Motorcycle Maintenance
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20090305/4aaaf45e/attachment.html


More information about the gdal-dev mailing list