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