24 from __future__
import print_function
25 from osgeo
import gdal
27 import osgeo.gdalconst
as gdalc
35 gdalc.GDT_UInt16:
'H',
37 gdalc.GDT_UInt32:
'I',
38 gdalc.GDT_Float32:
'f',
39 gdalc.GDT_Float64:
'f'
41 return fmttypes.get(pt,
'x')
43 if len(sys.argv) < 5
or len(sys.argv) > 6:
44 print(
"Usage: pixval.py <raster> <band> <x> <y>")
45 print(
"\traster - GDAL supported dataset")
46 print(
"\tband - 1-based number of band")
47 print(
"\toverview - optional 1-based number of overview")
48 print(
"\tx - Pixel column - 1..N where N is raster X dimension")
49 print(
"\ty - Pixel row - 1..N where N is raster Y dimension")
53 nband = int(sys.argv[2])
57 noverview = int(sys.argv[5])
61 print(
"File : %s" % infile)
62 print(
"Band : %d" % nband)
63 if noverview
is not None:
64 print(
"Overview: %d" % noverview)
65 print(
"Pixel: %d x %d" % (x, y))
67 ds = gdal.Open(infile, gdalc.GA_ReadOnly);
69 sys.exit(
'ERROR: Cannot open input file: ' +
str(infile))
71 band = ds.GetRasterBand(nband)
73 sys.exit(
'Cannot access band %d', nband)
78 if noverview > 0
and noverview <= band.GetOverviewCount():
79 src_band = band.GetOverview(noverview - 1)
81 print(
"ERROR: Invalid overview index")
82 print(
"Band %d consists of %d overivews" % (nband, band.GetOverviewCount()))
85 if x <= 0
or x > src_band.XSize
or y <= 0
or y > src_band.YSize:
86 print(
"ERROR: Invalid pixel coordinates")
87 print(
"Band or overview dimensions are %d x %d" % (src_band.XSize, src_band.YSize))
91 pixel = src_band.ReadRaster(x - 1, y - 1, 1, 1, 1, 1)
94 pixval = struct.unpack(fmt, pixel)
96 print(
"Pixel value -> %s" %
str(pixval[0]))