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