2
###############################################################################
3
# $Id: val_at_coord.py 20674 2010-09-22 17:50:33Z rouault $
5
# Project: GDAL Python samples
6
# Purpose: Outputs the value of the raster bands at a given
7
# (longitude, latitude) or (X, Y) location.
10
###############################################################################
11
# Copyright (c) 2010, Even Rouault <even dot rouault at mines dash paris dot org>
13
# Permission is hereby granted, free of charge, to any person obtaining a
14
# copy of this software and associated documentation files (the "Software"),
15
# to deal in the Software without restriction, including without limitation
16
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
17
# and/or sell copies of the Software, and to permit persons to whom the
18
# Software is furnished to do so, subject to the following conditions:
20
# The above copyright notice and this permission notice shall be included
21
# in all copies or substantial portions of the Software.
23
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
# DEALINGS IN THE SOFTWARE.
30
###############################################################################
33
from osgeo import gdal
40
# =============================================================================
42
print('Usage: val_at_coord.py [-display_xy] [longitude latitude | -coordtype=georef X Y] filename')
44
print('By default, the 2 first arguments are supposed to be the location')
45
print('in longitude, latitude order. If -coordtype=georef is specified before')
46
print('the next 2 values will be interpretated as the X and Y coordinates')
47
print('in the dataset spatial reference system.')
50
# =============================================================================
53
coordtype_georef = False
58
# =============================================================================
59
# Parse command line arguments.
60
# =============================================================================
62
while i < len(sys.argv):
65
if arg == '-coordtype=georef':
66
coordtype_georef = True
68
elif arg == '-display_xy':
71
elif longitude is None:
72
longitude = float(arg)
74
elif latitude is None:
77
elif filename is None:
93
ds = gdal.Open( filename, gdal.GA_ReadOnly )
95
print('Cannot open %s' % filename)
98
# Build Spatial Reference object based on coordinate system, fetched from the
104
srs = osr.SpatialReference()
105
srs.ImportFromWkt(ds.GetProjection())
107
srsLatLong = srs.CloneGeogCS()
108
# Convert from (longitude,latitude) to projected coordinates
109
ct = osr.CoordinateTransformation(srsLatLong, srs)
110
(X, Y, height) = ct.TransformPoint(longitude, latitude)
112
# Read geotransform matrix and calculate corresponding pixel coordinates
113
geomatrix = ds.GetGeoTransform()
114
(success, inv_geometrix) = gdal.InvGeoTransform(geomatrix)
115
x = int(inv_geometrix[0] + inv_geometrix[1] * X + inv_geometrix[2] * Y)
116
y = int(inv_geometrix[3] + inv_geometrix[4] * X + inv_geometrix[5] * Y)
119
print('x=%d, y=%d' % (x, y))
121
if x < 0 or x >= ds.RasterXSize or y < 0 or y >= ds.RasterYSize:
122
print('Passed coordinates are not in dataset extent')
125
res = ds.ReadAsArray(x,y,1,1)
126
if len(res.shape) == 2: