PostGIS 3.7.0dev-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_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 if hasattr(gdalc, 'GDT_Float16'):
43 fmttypes[gdalc.GDT_Float16] = 'e'
44 return fmttypes.get(pt, 'x')
45
46if len(sys.argv) < 5 or len(sys.argv) > 6:
47 print("Usage: pixval.py <raster> <band> <x> <y>")
48 print("\traster - GDAL supported dataset")
49 print("\tband - 1-based number of band")
50 print("\toverview - optional 1-based number of overview")
51 print("\tx - Pixel column - 1..N where N is raster X dimension")
52 print("\ty - Pixel row - 1..N where N is raster Y dimension")
53 sys.exit(0)
54
55infile = sys.argv[1]
56nband = int(sys.argv[2])
57x = int(sys.argv[3])
58y = int(sys.argv[4])
59if len(sys.argv) > 5:
60 noverview = int(sys.argv[5])
61else:
62 noverview = None
63
64print("File : %s" % infile)
65print("Band : %d" % nband)
66if noverview is not None:
67 print("Overview: %d" % noverview)
68print("Pixel: %d x %d" % (x, y))
69
70ds = gdal.Open(infile, gdalc.GA_ReadOnly);
71if ds is None:
72 sys.exit('ERROR: Cannot open input file: ' + str(infile))
73
74band = ds.GetRasterBand(nband)
75if band is None:
76 sys.exit('Cannot access band %d', nband)
77
78if noverview is None:
79 src_band = band
80else:
81 if noverview > 0 and noverview <= band.GetOverviewCount():
82 src_band = band.GetOverview(noverview - 1)
83 else:
84 print("ERROR: Invalid overview index")
85 print("Band %d consists of %d overivews" % (nband, band.GetOverviewCount()))
86 sys.exit(1)
87
88if x <= 0 or x > src_band.XSize or y <= 0 or y > src_band.YSize:
89 print("ERROR: Invalid pixel coordinates")
90 print("Band or overview dimensions are %d x %d" % (src_band.XSize, src_band.YSize))
91 sys.exit(1)
92
93# Pixel index is 0-based
94pixel = src_band.ReadRaster(x - 1, y - 1, 1, 1, 1, 1)
95
96fmt = pt2fmt(src_band.DataType)
97pixval = struct.unpack(fmt, pixel)
98
99print("Pixel value -> %s" % str(pixval[0]))
#define str(s)
pt2fmt(pt)
Definition pixval.py:31