PostGIS 3.6.2dev-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 return fmttypes.get(pt, 'x')
43
44if 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
53infile = sys.argv[1]
54nband = int(sys.argv[2])
55x = int(sys.argv[3])
56y = int(sys.argv[4])
57if len(sys.argv) > 5:
58 noverview = int(sys.argv[5])
59else:
60 noverview = None
61
62print("File : %s" % infile)
63print("Band : %d" % nband)
64if noverview is not None:
65 print("Overview: %d" % noverview)
66print("Pixel: %d x %d" % (x, y))
67
68ds = gdal.Open(infile, gdalc.GA_ReadOnly);
69if ds is None:
70 sys.exit('ERROR: Cannot open input file: ' + str(infile))
71
72band = ds.GetRasterBand(nband)
73if band is None:
74 sys.exit('Cannot access band %d', nband)
75
76if noverview is None:
77 src_band = band
78else:
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
86if 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
92pixel = src_band.ReadRaster(x - 1, y - 1, 1, 1, 1, 1)
93
94fmt = pt2fmt(src_band.DataType)
95pixval = struct.unpack(fmt, pixel)
96
97print("Pixel value -> %s" % str(pixval[0]))
#define str(s)
pt2fmt(pt)
Definition pixval.py:31