Pages

Thursday, February 12, 2015

Writing Raster Data with Python gdal

So now I have a SAR image in a an array called "data" as described in the previous post I want to save it, lets say, in an ERDAS img data format? This is basically converting the file format:


 I first get and register the appropriate driver for the output file:

        >>> driver = gdal.GetDriverByName(HFA)
    >>> driver.Register()


I want to use the same size from the input file which is in "dataset" as in the previous post  

    >>> cols = dataset.RasterXSize
    >>> rows = dataset.RasterYSize
    >>> bands = dataset.RasterCount
    >>> datatype = band.DataType

Then Creating the output file:

    >>> outDataset = driver.Create(ERS1PRI_19920430.pix, cols, rows, bands, datatype)


I want my outputfile to have the same georeferencing and projection information as the input file (the "0" shows the execution went fine):

        >>> geoTransform = dataset.GetGeoTransform()
    >>> outDataset.SetGeoTransform(geoTransform )
    0
    >>> proj = dataset.GetProjection()
    >>> outDataset.SetProjection(proj)
    0
    >>>




That has to be assigned before writing the data to the output band, otherwise its not in the file!

Before writing the data, I have to get the band from the newly created file:

     >>> outBand = outDataset.GetRasterBand(1)


Now I can write the input image to the new output image:

    >>> outBand.WriteArray(data, 0, 0)


clean variables and close files
      >>> dataset = None
   >>> band = None
   >>> outBand = None
   >>> outDataset = None


[still to find out: got this to work for PCI format, with ENVI hdr the image but not the geoinformation was transferred and Erdas img did not work -- file too big?]

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.