[Gdal-dev] Re: 16-bit PNM support

Andrey Kiselev dron at ak4719.spb.edu
Fri Feb 14 04:39:32 EST 2003


Hi, Jim,

On Thu, Feb 13, 2003 at 08:06:08PM -0500, Jim Julian wrote:
>    Thanks for your response. The output option approach to preserving
> MAXVAL seems the way to go. The "No_Data" option idea is left over
> from a DEM issue. Generally, I convert DEM to images. The NetPBM
> formats are a good solution for that. Unfortunately, the 'No_Data' value
> for most DEM is -9999. This makes the binary representaion signed.
>    Moreover, it also becomes the bottom of the scale and represents a
> similar problem in terms of image scaling.
>    Frank suggested using VRT to make the conversion. He also suggested
> the possibility of using Python. I looked over the documents I have and
> can find no detailed in formation on VRT files their options and uses.
> As a matter of fact, I can find no documentation for input file formats
> and option switches.
>     What do you think? VRT or Python?

I think that automation utility is always better then manual work.
Following small Python script will replace inNoData values (-9999.0 in
our case) from the input dataset by the outNoData (0.0) values.

I think implementation is clear enougth, but you will need
python-numeric package.


-- 
Andrey V. Kiselev
Home phone:  +7 812 5274898  ICQ# 26871517
-------------- next part --------------
#!/usr/bin/env python

import gdal
from gdalconst import *
import Numeric
import sys

inNoData = -9999.
outNoData = 0.

# checking command line
if len(sys.argv) < 2:
    print 'Usage:'
    print '%s <infile> <outfile>' % sys.argv[0]
    sys.exit(0)

infile = sys.argv[1]
outfile = sys.argv[2]

indataset = gdal.Open( infile, GA_ReadOnly )

out_driver = gdal.GetDriverByName('PNM')
outdataset = out_driver.Create(outfile, indataset.RasterXSize, indataset.RasterYSize, indataset.RasterCount, GDT_UInt16)

for iBand in range(1, indataset.RasterCount + 1):
    inband = indataset.GetRasterBand(iBand)
    outband = outdataset.GetRasterBand(iBand)

    for i in range(inband.YSize - 1, -1, -1):
	scanline = inband.ReadAsArray(0, i, inband.XSize, 1, inband.XSize, 1)
	for j in range(inband.XSize):
	    if scanline[0, j] == inNoData:
		scanline[0, j] = outNoData
	outband.WriteArray(scanline, 0, i)

print 'OK'




More information about the Gdal-dev mailing list