<div dir="ltr">Hello Again, <br><br>I have been googling around and reading how to save my numpy arrays to rasters. So far I have not found a satisfying answer. <br>The closest thing to what I'd would like to do is found here: <b><a href="http://n2.nabble.com/basic-raster-math-td2033308.html#a2033316">http://n2.nabble.com/basic-raster-math-td2033308.html#a2033316</a></b><br>
<br>When I try do to the following:<br><br>from osgeo import gdal<br>from osgeo.gdalconst import *<br>dataset = gdal.Open('raster1.asc', GA_ReadOnly)<br>dataset1 = gdal.Open('raster2.asc', GA_ReadOnly)<br>
print dataset, dataset1<br>from osgeo import gdal_array<br>from osgeo import osr<br>a = gdal_array.DatasetReadAsArray(dataset)<br>b = gdal_array.DatasetReadAsArray(dataset1)<br>print a<br>print b<br>def WriteRaster (dst_filename, raster):<br>
<br> format = "GTiff"<br> driver = gdal.GetDriverByName( format )<br> dst_ds = driver.Create( dst_filename, 71, 73,\<br> 1,gdal.GDT_Float32,options=["COMPRESS=PACKBITS","TFW=YES"] )<br>
dst_ds.SetGeoTransform( [-19.5, 1.0, 0.0, 37.5, 0.0, -1.0] )<br> srs = osr.SpatialReference()<br> srs.ImportFromEPSG(4326) #WGS84 lat long.<br> dst_ds.SetProjection( srs.ExportToWkt() )<br> dst_ds.GetRasterBand(1).WriteArray( raster )<br>
dst_ds = None<br><br><br>c= a+b<br>print c<br>WriteRaster ('raster3.gtiff', c)<br><br>A file with following content is created:<br><br>1.0000000000<br>0.0000000000<br>0.0000000000<br>-1.0000000000<br>-19.0000000000<br>
37.0000000000<br clear="all"><br>The rasters originally used are ascii type, and this are their contents:<br>raster1.asc: raster2.asc:<br>ncols 6<br>nrows 4<br>xllcorner 3366820.0000019<br>yllcorner 5814589.0300007<br>
cellsize 10<br>NODATA_value -9999<br>1 1 1 1 1 1<br>1 1 1 1 1 1<br>1 1 1 1 1 1<br>1 1 1 1 -9999 -9999<br><br>So I would expect a file containing just: <br> 2 2 2 2 2 2<br> 2 2 2 2 2 2<br>
2 2 2 2 2 2<br>2 2 2 2 -19998 -19998<br><br><br>I would appreciate it if some one can supply a code snippet how to successfuly save numpy arrays into ascii raster. <br><br>Thanks again, <br>
Oz Nahum<br>Eberhard Karls Universität Tübingen<br>Grad-student<br>Applied Environmental Geosciences<br>Institut für Geowissenschaften<br>-- <br><br>---<br>when one person suffers from a delusion it is called insanity. When many people suffer from a delusion it is called religion."<br>
Robert Pirsig, Zen and the Art of Motorcycle Maintenance<br><br>
</div>