PostGIS  2.5.7dev-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 osgeo import gdal
25 from osgeo import osr
26 import osgeo.gdalconst as gdalc
27 import struct
28 import sys
29 
30 def pt2fmt(pt):
31  fmttypes = {
32  gdalc.GDT_Byte: 'B',
33  gdalc.GDT_Int16: 'h',
34  gdalc.GDT_UInt16: 'H',
35  gdalc.GDT_Int32: 'i',
36  gdalc.GDT_UInt32: 'I',
37  gdalc.GDT_Float32: 'f',
38  gdalc.GDT_Float64: 'f'
39  }
40  return fmttypes.get(pt, 'x')
41 
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"
49  sys.exit(0)
50 
51 infile = sys.argv[1]
52 nband = int(sys.argv[2])
53 x = int(sys.argv[3])
54 y = int(sys.argv[4])
55 if len(sys.argv) > 5:
56  noverview = int(sys.argv[5])
57 else:
58  noverview = None
59 
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)
65 
66 ds = gdal.Open(infile, gdalc.GA_ReadOnly);
67 if ds is None:
68  sys.exit('ERROR: Cannot open input file: ' + str(infile))
69 
70 band = ds.GetRasterBand(nband)
71 if band is None:
72  sys.exit('Cannot access band %d', nband)
73 
74 if noverview is None:
75  src_band = band
76 else:
77  if noverview > 0 and noverview <= band.GetOverviewCount():
78  src_band = band.GetOverview(noverview - 1)
79  else:
80  print "ERROR: Invalid overview index"
81  print "Band %d consists of %d overivews" % (nband, band.GetOverviewCount())
82  sys.exit(1)
83 
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)
87  sys.exit(1)
88 
89 # Pixel index is 0-based
90 pixel = src_band.ReadRaster(x - 1, y - 1, 1, 1, 1, 1)
91 
92 fmt = pt2fmt(src_band.DataType)
93 pixval = struct.unpack(fmt, pixel)
94 
95 print "Pixel value -> %s" % str(pixval[0])
def pt2fmt(pt)
Definition: pixval.py:30