[gdal-dev] Creating Geotiff with WriteArray in Python

Matt Fearon singlespeed10 at gmail.com
Mon Mar 24 12:57:48 EDT 2008


Hi Chris and others,

Thank you all for your help, especially Chris for the detailed
explanations. I finally got my script working with WriteArray.

Just a follow up question in regard to metadata and ENVI file format
in python/GDAL. If the dat file I read in has an ENVI .hdr file
associated with it - how could I unpack/access the metadata from the
.hdr file so that I could use it when writing my GeoTiff? I know I
could easily read the .hdr file, but it seems like GDAL already knows
the header information when I read the dat file? For example, I read
my dat file the following way;

file = "IMG-HH-ALPSRP021230580-H1.5GUA"
gd=gdal.Open(file)
array=gd.ReadAsArray()

Next, I would like to access the .hdr information associated with this
dat file? There is a .hdr file named
"IMG-HH-ALPSRP021230580-H1.5GUA.HDR".

thanks,
Matt

On Thu, Mar 20, 2008 at 4:34 PM, Christopher Barker
<Chris.Barker at noaa.gov> wrote:
>
>  Matt Fearon wrote:
>  > Chris, thank you for the great detail of your response.
>
>  You're welcome.
>
>
>  > However,
>  > I still receive trouble with WriteArray and few additional things now.
>  > Below are my errors
>
>
> >     datatype = NumericTypeCodeToGDALTypeCode( array.typecode() )
>  > AttributeError: 'numpy.ndarray' object has no attribute 'typecode'
>
>  Sorry, my fault -- I think what's happening is that you're passing in a
>  numpy array, and it's expecting a Numeric array -- they are very
>  similar, but not identical. It seems you are using GDAL 1.4, in which
>  case it's probably better to just stick with Numeric all around.
>
>  Note to Frank -- we could wrap input arrays with Numeric.asarray()
>  calls, so that the conversion would happen automatically.
>
>
>  > #! /usr/bin/env python
>  >
>  > import pickle, array, sys, os, shutil
>  > import gdal, gdalnumeric
>  > from gdalconst import *
>  > import numpy
>  > from numpy import *
>
>  I like to do:
>
>  import numpy as N
>  or import Numeric as N
>
>  In this case, you're probably better off with just Numeric. And I really
>  recommend against "import *". In fact, in this case, if you do that,
>  you're stomping on the numpy array constructor with your "array" variable.
>
>
>
>  > gd=gdal.Open(file)
>  > array=gd.ReadAsArray()
>
>  I think this is now a Numeric array, check like this:
>
>  type(array) -- if it says:
>
>  <type 'array'>
>  it's Numeric, if it says:
>
>  <type 'numpy.ndarray'>
>  it's numpy.
>
>  I think you're working with Numeric here.
>
>  By the way -- what a mess! Oh well, everyone is moving to numpy now, so
>  in a bit this will no longer be an issue. If you can upgrade to GDAL 1.5
>  now, I would. But for now, let's go Numeric:
>
>
>  > nxarray=numpy.zeros(nx, dtype=float32)
>
>  You don't need that anymore, it's getting created inside the loop anyway.
>
>
>  > for j in range(ny):
>  >     nxarray=array[j,:]
>
>  I now I gave you this code, but there is an issue here, what type is
>  your input data? IIUC, it's not Float32, so you need to do a conversion
>  here:
>      nxarray = array[j,:].astype(N.Float32)
>
>  here's the trick:
>      nxarray.savespace(1)
>  This tells the Numeric array that you don't want it to upcast to a
>  Float64 when you multiply it by a standard Python float (which is 64
>  bit). One advantage to numpy is that it does this by default.
>
>      nxarray = N.where(nxarray > 0, 10.0 * N.log10(pow(nxarray,2)) +
>
>  changed the "numpy" to N -- then if you did "import Numeric as N",
>  you'll get a Numeric array here. You can just do "import Numeric", but
>  then you have to type that long word everywhere...
>
>
>  > cal_factor, nxarray)
>  >     nxarray.shape = (1,-1)
>  >     print nxarray
>  >     dst_ds.GetRasterBand(1).WriteArray(nxarray,0,j)
>
>  that should do it.
>
>
>
>
>  -Chris
>
>
>  --
>  Christopher Barker, Ph.D.
>  Oceanographer
>
>  Emergency Response Division
>  NOAA/NOS/OR&R            (206) 526-6959   voice
>  7600 Sand Point Way NE   (206) 526-6329   fax
>  Seattle, WA  98115       (206) 526-6317   main reception
>
>  Chris.Barker at noaa.gov
>  _______________________________________________
>  gdal-dev mailing list
>  gdal-dev at lists.osgeo.org
>  http://lists.osgeo.org/mailman/listinfo/gdal-dev
>


More information about the gdal-dev mailing list