root/spike/wktraster/scripts/pixval.py

Revision 4115, 2.9 KB (checked in by mloskot, 3 years ago)

[wktraster] Minor fixes to pixval.py script.

  • Property svn:executable set to *
  • Property svn:keywords set to
    Id
    Revision
Line 
1#! /usr/bin/env python
2#
3# $Id$
4#
5# This is a simple script based on GDAL and used to retrieve value of single raster pixel.
6# It is used in WKTRaster testing to compare raster samples.
7#
8# Copyright (C) 2009 Mateusz Loskot <mateusz@loskot.net>
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23#
24from osgeo import gdal
25from osgeo import osr
26import osgeo.gdalconst as gdalc
27import struct
28import sys
29
30def 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
42if 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
51infile = sys.argv[1]
52nband = int(sys.argv[2])
53x = int(sys.argv[3])
54y = int(sys.argv[4])
55if len(sys.argv) > 5:
56    noverview = int(sys.argv[5])
57else:
58    noverview = None
59
60print "File : %s" % infile
61print "Band : %d" % nband
62if noverview is not None:
63    print "Overview: %d" % noverview
64print "Pixel: %d x %d" % (x, y)
65
66ds = gdal.Open(infile, gdalc.GA_ReadOnly);
67if ds is None:
68    sys.exit('ERROR: Cannot open input file: ' + str(infile))
69
70band = ds.GetRasterBand(nband)
71if band is None:
72    sys.exit('Cannot access band %d', nband)
73
74if noverview is None:
75    src_band = band
76else:
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
84if 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
90pixel = src_band.ReadRaster(x - 1, y - 1, 1, 1, 1, 1)
91
92fmt = pt2fmt(src_band.DataType)
93pixval = struct.unpack(fmt, pixel)
94
95print "Pixel value -> %s" % str(pixval[0])
Note: See TracBrowser for help on using the browser.