24 from __future__
import print_function
25 from osgeo
import gdal
27 import 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 return fmttypes.get(pt,
'x')
44 if len(sys.argv) < 5
or len(sys.argv) > 6:
45 print(
"Usage: pixval.py <raster> <band> <x> <y>")
46 print(
"\traster - GDAL supported dataset")
47 print(
"\tband - 1-based number of band")
48 print(
"\toverview - optional 1-based number of overview")
49 print(
"\tx - Pixel column - 1..N where N is raster X dimension")
50 print(
"\ty - Pixel row - 1..N where N is raster Y dimension")
54 nband = int(sys.argv[2])
58 noverview = int(sys.argv[5])
62 print(
"File : %s" % infile)
63 print(
"Band : %d" % nband)
64 if noverview
is not None:
65 print(
"Overview: %d" % noverview)
66 print(
"Pixel: %d x %d" % (x, y))
68 ds = gdal.Open(infile, gdalc.GA_ReadOnly);
70 sys.exit(
'ERROR: Cannot open input file: ' +
str(infile))
72 band = ds.GetRasterBand(nband)
74 sys.exit(
'Cannot access band %d', nband)
79 if noverview > 0
and noverview <= band.GetOverviewCount():
80 src_band = band.GetOverview(noverview - 1)
82 print(
"ERROR: Invalid overview index")
83 print(
"Band %d consists of %d overivews" % (nband, band.GetOverviewCount()))
86 if x <= 0
or x > src_band.XSize
or y <= 0
or y > src_band.YSize:
87 print(
"ERROR: Invalid pixel coordinates")
88 print(
"Band or overview dimensions are %d x %d" % (src_band.XSize, src_band.YSize))
92 pixel = src_band.ReadRaster(x - 1, y - 1, 1, 1, 1, 1)
95 pixval = struct.unpack(fmt, pixel)
97 print(
"Pixel value -> %s" %
str(pixval[0]))