24 from osgeo
import gdal
26 import osgeo.gdalconst
as gdalc
34 gdalc.GDT_UInt16:
'H',
36 gdalc.GDT_UInt32:
'I',
37 gdalc.GDT_Float32:
'f',
38 gdalc.GDT_Float64:
'f'
40 return fmttypes.get(pt,
'x')
42 if len(sys.argv) < 5
or len(sys.argv) > 6:
43 print "Usage: pixval.py <raster> <band> <x> <y>"
44 print "\traster - GDAL supported dataset"
45 print "\tband - 1-based number of band"
46 print "\toverview - optional 1-based number of overview"
47 print "\tx - Pixel column - 1..N where N is raster X dimension"
48 print "\ty - Pixel row - 1..N where N is raster Y dimension"
52 nband = int(sys.argv[2])
56 noverview = int(sys.argv[5])
60 print "File : %s" % infile
61 print "Band : %d" % nband
62 if noverview
is not None:
63 print "Overview: %d" % noverview
64 print "Pixel: %d x %d" % (x, y)
66 ds = gdal.Open(infile, gdalc.GA_ReadOnly);
68 sys.exit(
'ERROR: Cannot open input file: ' + str(infile))
70 band = ds.GetRasterBand(nband)
72 sys.exit(
'Cannot access band %d', nband)
77 if noverview > 0
and noverview <= band.GetOverviewCount():
78 src_band = band.GetOverview(noverview - 1)
80 print "ERROR: Invalid overview index"
81 print "Band %d consists of %d overivews" % (nband, band.GetOverviewCount())
84 if x <= 0
or x > src_band.XSize
or y <= 0
or y > src_band.YSize:
85 print "ERROR: Invalid pixel coordinates"
86 print "Band or overview dimensions are %d x %d" % (src_band.XSize, src_band.YSize)
90 pixel = src_band.ReadRaster(x - 1, y - 1, 1, 1, 1, 1)
93 pixval = struct.unpack(fmt, pixel)
95 print "Pixel value -> %s" % str(pixval[0])