PostGIS  3.7.0dev-r@@SVN_REVISION@@
pixval.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 #
3 #
4 # This is a simple script based on GDAL and used to retrieve value of single raster pixel.
5 # It is used in WKTRaster testing to compare raster samples.
6 #
7 # Copyright (C) 2009 Mateusz Loskot <mateusz@loskot.net>
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software Foundation,
21 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #
23 
24 from __future__ import print_function
25 from osgeo import gdal
26 from osgeo import osr
27 import osgeo.gdalconst as gdalc
28 import struct
29 import sys
30 
31 def pt2fmt(pt):
32  fmttypes = {
33  gdalc.GDT_Byte: 'B',
34  gdalc.GDT_Int8: 'B',
35  gdalc.GDT_Int16: 'h',
36  gdalc.GDT_UInt16: 'H',
37  gdalc.GDT_Int32: 'i',
38  gdalc.GDT_UInt32: 'I',
39  gdalc.GDT_Float32: 'f',
40  gdalc.GDT_Float64: 'f'
41  }
42  return fmttypes.get(pt, 'x')
43 
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")
51  sys.exit(0)
52 
53 infile = sys.argv[1]
54 nband = int(sys.argv[2])
55 x = int(sys.argv[3])
56 y = int(sys.argv[4])
57 if len(sys.argv) > 5:
58  noverview = int(sys.argv[5])
59 else:
60  noverview = None
61 
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))
67 
68 ds = gdal.Open(infile, gdalc.GA_ReadOnly);
69 if ds is None:
70  sys.exit('ERROR: Cannot open input file: ' + str(infile))
71 
72 band = ds.GetRasterBand(nband)
73 if band is None:
74  sys.exit('Cannot access band %d', nband)
75 
76 if noverview is None:
77  src_band = band
78 else:
79  if noverview > 0 and noverview <= band.GetOverviewCount():
80  src_band = band.GetOverview(noverview - 1)
81  else:
82  print("ERROR: Invalid overview index")
83  print("Band %d consists of %d overivews" % (nband, band.GetOverviewCount()))
84  sys.exit(1)
85 
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))
89  sys.exit(1)
90 
91 # Pixel index is 0-based
92 pixel = src_band.ReadRaster(x - 1, y - 1, 1, 1, 1, 1)
93 
94 fmt = pt2fmt(src_band.DataType)
95 pixval = struct.unpack(fmt, pixel)
96 
97 print("Pixel value -> %s" % str(pixval[0]))
#define str(s)
def pt2fmt(pt)
Definition: pixval.py:31