Skip to content

Commit 2acf94d

Browse files
committed
Can now get elevation profile
1 parent 9de07d3 commit 2acf94d

2 files changed

Lines changed: 54 additions & 16 deletions

File tree

raytracer.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import math
33
import os
44
from pathlib import Path
5-
from typing import NamedTuple, Dict, Tuple, Generator
5+
from typing import NamedTuple, Dict, Tuple, Generator, List
66
from zipfile import ZipFile
77

88
HGT_DIR = Path(os.environ["SRTM_DIR"])
@@ -229,15 +229,35 @@ def get_elevation_profile(
229229
end_latitude: float,
230230
end_longitude: float,
231231
):
232-
# y = mx + c
233-
pass
232+
# TODO: Remove magic number to setting
233+
def to_int(lat_lng: float) -> int:
234+
return round(lat_lng * 1200)
235+
236+
def to_float(lat_lng_int: int) -> float:
237+
return lat_lng_int / 1200
238+
239+
points = points_on_line(
240+
x1=to_int(start_latitude),
241+
y1=to_int(start_longitude),
242+
x2=to_int(end_latitude),
243+
y2=to_int(end_longitude),
244+
)
245+
converted_points = [(to_float(x), to_float(y)) for x, y in points]
246+
247+
elevations = []
248+
for latitude, longitude in converted_points:
249+
elevations.append(self.get_height_for_latitude_and_longitude(
250+
latitude, longitude
251+
))
252+
return elevations
234253

235254

236255
def points_on_line(
237-
x1: float, y1: float, x2: float, y2: float
238-
) -> Generator[Tuple[float, float], None, None]:
256+
x1: int, y1: int, x2: int, y2: int
257+
) -> List[Tuple[int, int]]:
258+
# Credit: https://stackoverflow.com/questions/25837544
239259
points = []
240-
issteep = abs(y2 - y1) > abs(x2 - x1)
260+
issteep = abs(y2-y1) > abs(x2-x1)
241261
if issteep:
242262
x1, y1 = y1, x1
243263
x2, y2 = y2, x2
@@ -247,25 +267,25 @@ def points_on_line(
247267
y1, y2 = y2, y1
248268
rev = True
249269
deltax = x2 - x1
250-
deltay = abs(y2 - y1)
270+
deltay = abs(y2-y1)
251271
error = int(deltax / 2)
252272
y = y1
253273
ystep = None
254274
if y1 < y2:
255275
ystep = 1
256276
else:
257277
ystep = -1
258-
259-
x_range = range(x1, x2 + 1)
260-
if rev:
261-
x_range = reversed(x_range)
262-
263-
for x in x_range:
278+
for x in range(x1, x2 + 1):
264279
if issteep:
265-
yield y, x
280+
points.append((y, x))
266281
else:
267-
yield x, y
282+
points.append((x, y))
268283
error -= deltay
269284
if error < 0:
270285
y += ystep
271286
error += deltax
287+
# Reverse the list if the coordinates were reversed
288+
if rev:
289+
points.reverse()
290+
return points
291+

tests.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import timeit
23
from pathlib import Path
34

45
import pytest
@@ -157,8 +158,25 @@ def test_height_map_collection_load_area():
157158
breakpoint()
158159

159160

161+
def test_height_map_collection_get_elevation_profile():
162+
collection = HeightMapCollection()
163+
collection.build_file_index()
164+
profile = collection.get_elevation_profile(
165+
start_latitude=40.103284,
166+
start_longitude=-7.453766,
167+
end_latitude=40.073772,
168+
end_longitude=-7.432998,
169+
)
170+
breakpoint()
171+
assert profile[0] == 566
172+
assert profile[-1] == 424
173+
assert len(profile) == 36
174+
175+
160176
# points_on_line()
161177

162178

163179
def test_points_on_line():
164-
assert points_on_line(0, 0, 10, 10) == 1
180+
assert list(points_on_line(0, 0, 2, 2)) == [(0, 0), (1, 1), (2, 2)]
181+
assert list(points_on_line(2, 2, 0, 0)) == [(2, 2), (1, 1), (0, 0)]
182+
assert list(points_on_line(0, 0, 0, 0)) == [(0, 0)]

0 commit comments

Comments
 (0)