forked from perliedman/shadow-mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrtm.py
More file actions
191 lines (165 loc) · 7.01 KB
/
srtm.py
File metadata and controls
191 lines (165 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
# Pylint: Disable name warnings
# pylint: disable-msg=C0103
"""
Load and process SRTM data.
Downloaded from http://svn.openstreetmap.org/applications/utils/import/srtm2wayinfo/python/srtm.py
Copyright (c) 2009 Hermann Kraus
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from __future__ import print_function
from __future__ import division
#import xml.dom.minidom
from past.utils import old_div
import zipfile
import array
import math
class NoSuchTileError(Exception):
"""Raised when there is no tile for a region."""
def __init__(self, lat, lon):
self.lat = lat
self.lon = lon
def __str__(self):
return "No SRTM tile for %d, %d available!" % (self.lat, self.lon)
class WrongTileError(Exception):
"""Raised when the value of a pixel outside the tile area is requested."""
def __init__(self, tile_lat, tile_lon, req_lat, req_lon):
self.tile_lat = tile_lat
self.tile_lon = tile_lon
self.req_lat = req_lat
self.req_lon = req_lon
def __str__(self):
return "SRTM tile for %d, %d does not contain data for %d, %d!" % (
self.tile_lat, self.tile_lon, self.req_lat, self.req_lon)
class InvalidTileError(Exception):
"""Raised when the SRTM tile file contains invalid data."""
def __init__(self, lat, lon):
self.lat = lat
self.lon = lon
def __str__(self):
return "SRTM tile for %d, %d is invalid!" % (self.lat, self.lon)
class SRTMTile():
"""Base class for all SRTM tiles.
Each SRTM tile is size x size pixels big and contains
data for the area from (lat, lon) to (lat+1, lon+1) inclusive.
This means there is a 1 pixel overlap between tiles. This makes it
easier for as to interpolate the value, because for every point we
only have to look at a single tile.
"""
def __init__(self, f, lat, lon):
zipf = zipfile.ZipFile(f, 'r')
names = zipf.namelist()
if len(names) != 1:
raise InvalidTileError(lat, lon)
self.lat = lat
self.lon = lon
self._setData(zipf.read(names[0]))
def _setData(self, data):
self.size = int(math.sqrt(old_div(len(data),2))) # 2 bytes per sample
# Currently only SRTM1/3 is supported
if self.size not in (1201, 3601):
raise InvalidTileError(self.lat, self.lon)
self.data = array.array('h', data)
self.data.byteswap()
if len(self.data) != self.size * self.size:
raise InvalidTileError(self.lat, self.lon)
@staticmethod
def _avg(value1, value2, weight):
"""Returns the weighted average of two values and handles the case where
one value is None. If both values are None, None is returned.
"""
if value1 is None:
return value2
if value2 is None:
return value1
return value2 * weight + value1 * (1 - weight)
def calcOffset(self, x, y):
"""Calculate offset into data array. Only uses to test correctness
of the formula."""
# Datalayout
# X = longitude
# Y = latitude
# Sample for size 1201x1201
# ( 0/1200) ( 1/1200) ... (1199/1200) (1200/1200)
# ( 0/1199) ( 1/1199) ... (1199/1199) (1200/1199)
# ... ... ... ...
# ( 0/ 1) ( 1/ 1) ... (1199/ 1) (1200/ 1)
# ( 0/ 0) ( 1/ 0) ... (1199/ 0) (1200/ 0)
# Some offsets:
# (0/1200) 0
# (1200/1200) 1200
# (0/1199) 1201
# (1200/1199) 2401
# (0/0) 1201*1200
# (1200/0) 1201*1201-1
return x + self.size * (self.size - y - 1)
def getPixelValue(self, x, y):
"""Get the value of a pixel from the data, handling voids in the
SRTM data."""
assert x < self.size, "x: %d<%d" % (x, self.size)
assert y < self.size, "y: %d<%d" % (y, self.size)
# Same as calcOffset, inlined for performance reasons
offset = x + self.size * (self.size - y - 1)
#print offset
value = self.data[offset]
if value == -32768:
return None # -32768 is a special value for areas with no data
return value
def getAltitudeFromLatLon(self, lat, lon):
"""Get the altitude of a lat lon pair, using the four neighbouring
pixels for interpolation.
"""
# print "-----\nFromLatLon", lon, lat
lat -= self.lat
lon -= self.lon
# print "lon, lat", lon, lat
if lat < 0.0 or lat >= 1.0 or lon < 0.0 or lon >= 1.0:
raise WrongTileError(self.lat, self.lon, self.lat+lat, self.lon+lon)
x = lon * (self.size - 1)
y = lat * (self.size - 1)
# print "x,y", x, y
x_int = int(x)
x_frac = x - int(x)
y_int = int(y)
y_frac = y - int(y)
# print "frac", x_int, x_frac, y_int, y_frac
value00 = self.getPixelValue(x_int, y_int)
value10 = self.getPixelValue(x_int+1, y_int)
value01 = self.getPixelValue(x_int, y_int+1)
value11 = self.getPixelValue(x_int+1, y_int+1)
value1 = self._avg(value00, value10, x_frac)
value2 = self._avg(value01, value11, x_frac)
value = self._avg(value1, value2, y_frac)
# print "%4d %4d | %4d\n%4d %4d | %4d\n-------------\n%4d" % (
# value00, value10, value1, value01, value11, value2, value)
return value
class VTPTile(SRTMTile):
"""As SRTMTile, but for the file format used by
http://vterrain.org/Elevation/global.html"""
def __init__(self, f, lat, lon):
self.lat = lat
self.lon = lon
self._setData(f.read())
#DEBUG ONLY
if __name__ == '__main__':
# downloader = SRTMDownloader()
# downloader.loadFileList()
# tile = downloader.getTile(49, 12):
from sys import argv
with open(argv[1], 'rb') as f:
tile = VFPTile(f, int(argv[2]), int(argv[3]))
print(tile.getAltitudeFromLatLon(float(argv[4]), float(argv[5])))