Skip to content

Commit

Permalink
no need for 'approximation' assuming the data is good quality
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeacloudberry committed Apr 28, 2024
1 parent 1adf7be commit a402f39
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 105 deletions.
18 changes: 4 additions & 14 deletions webtrack_cli/src/elevation/ele_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,20 @@ def build_url(tilename: str, version: str) -> str:
return f"https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL1.003/2000.02.11/{tilename}.SRTMGL1.hgt.zip"
raise AttributeError("Bad version")

def get_elevation(
self,
latitude: float,
longitude: float,
approximate: Optional[bool] = None,
version: Optional[str] = None,
) -> Optional[float]:
def get_elevation(self, latitude: float, longitude: float) -> Optional[float]:
"""
Return the elevation at the point specified.
Args:
latitude: float of the latitude in decimal degrees
longitude: float of the longitude in decimal degrees
approximate: bool passed to GeoElevationFile.get_elevation
version: str of the SRTM data version and resolution to get.
Values are determined by build_url()
Returns:
A float passed back from GeoElevationFile.get_elevation().
Value should be the elevation of the point in meters.
"""
if version is None:
version = self.version
version = self.version
tilename = GeoElevationData.get_tilename(latitude, longitude)
geo_elevation_file: Optional[mod_ele_file.GeoElevationFile]
filename = f"{tilename}_{version}"
Expand All @@ -183,7 +173,7 @@ def get_elevation(
geo_elevation_file = self._load_tile(tilename, version)

if geo_elevation_file:
return geo_elevation_file.get_elevation(latitude, longitude, approximate)
return geo_elevation_file.get_elevation(latitude, longitude)
return None

def _fetch(self, url: str) -> bytes:
Expand Down Expand Up @@ -242,7 +232,7 @@ def _load_tile(self, tilename: str, version: str) -> mod_ele_file.GeoElevationFi
data = GeoElevationData.file_write(f"{filename}.{extension}", data)

# Create GeoElevationFile
tile = mod_ele_file.GeoElevationFile(tilename + ".hgt", data, self)
tile = mod_ele_file.GeoElevationFile(tilename + ".hgt", data)
if self.batch_mode:
self.tiles = {filename: tile}
else:
Expand Down
92 changes: 2 additions & 90 deletions webtrack_cli/src/elevation/ele_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
from typing import Optional
from typing import Tuple

from . import ele_data as mod_ele_data

ONE_DEGREE = 1000.0 * 10000.8 / 90.0


Expand All @@ -37,27 +35,15 @@ class GeoElevationFile:
it may need elevations from nearby files.
"""

url: Optional[str] = None
latitude: Optional[float] = None
longitude: Optional[float] = None

def __init__(
self,
file_name: str,
data: bytes,
geo_elevation_data: mod_ele_data.GeoElevationData,
):
def __init__(self, file_name: str, data: bytes):
"""Data is a raw file contents of the file."""

self.geo_elevation_data = geo_elevation_data
self.file_name = file_name

self.parse_file_name_starting_position()

self.data = data

square_side = mod_math.sqrt(len(self.data) / 2.0)

self.resolution = 1.0 / (square_side - 1)
self.square_side = int(square_side)

Expand All @@ -82,9 +68,7 @@ def distance(

return mod_math.sqrt(x * x + y * y) * ONE_DEGREE

def get_elevation(
self, latitude: float, longitude: float, approximate: Optional[bool] = False
) -> Optional[float]:
def get_elevation(self, latitude: float, longitude: float) -> Optional[float]:
"""
If approximate is True, then only the points from SRTM grid will be
used, otherwise a basic approximation of nearby points will be calculated.
Expand All @@ -97,80 +81,8 @@ def get_elevation(
raise Exception(f"Invalid longitude {longitude} for file {self.file_name}")

row, column = self.get_row_and_column(latitude, longitude)

if approximate:
return self.approximation(latitude, longitude)
return self.get_elevation_from_row_and_column(row, column)

def approximation(self, latitude: float, longitude: float) -> Optional[float]:
"""
Dummy approximation with nearest points. The nearest the neighbour the
more important will be its elevation.
"""
d = 1.0 / self.square_side
d_meters = d * ONE_DEGREE

# Since the less the distance => the more important should be the
# distance of the point, we'll use d-distance as importance coef
# here:
importance_1 = d_meters - GeoElevationFile.distance(
latitude + d, longitude, latitude, longitude
)
elevation_1 = self.geo_elevation_data.get_elevation(
latitude + d, longitude, approximate=False
)

importance_2 = d_meters - GeoElevationFile.distance(
latitude - d, longitude, latitude, longitude
)
elevation_2 = self.geo_elevation_data.get_elevation(
latitude - d, longitude, approximate=False
)

importance_3 = d_meters - GeoElevationFile.distance(
latitude, longitude + d, latitude, longitude
)
elevation_3 = self.geo_elevation_data.get_elevation(
latitude, longitude + d, approximate=False
)

importance_4 = d_meters - GeoElevationFile.distance(
latitude, longitude - d, latitude, longitude
)
elevation_4 = self.geo_elevation_data.get_elevation(
latitude, longitude - d, approximate=False
)
# TODO: Check if coordinates inside the same file, and only then decide if to call
# self.geo_elevation_data.get_elevation or just self.get_elevation

if (
elevation_1 is None
or elevation_2 is None
or elevation_3 is None
or elevation_4 is None
):
elevation = self.get_elevation(latitude, longitude, approximate=False)
if not elevation:
return None
elevation_1 = elevation_1 or elevation
elevation_2 = elevation_2 or elevation
elevation_3 = elevation_3 or elevation
elevation_4 = elevation_4 or elevation

# Normalize importance:
sum_importances = float(
importance_1 + importance_2 + importance_3 + importance_4
)

result = (
importance_1 / sum_importances * elevation_1
+ importance_2 / sum_importances * elevation_2
+ importance_3 / sum_importances * elevation_3
+ importance_4 / sum_importances * elevation_4
)

return result

def get_elevation_from_row_and_column(
self, row: int, column: int
) -> Optional[float]:
Expand Down
2 changes: 1 addition & 1 deletion webtrack_cli/src/gpx_to_webtrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def gpx_to_webtrack_with_elevation(
waypoints = []
for waypoint in gpx.waypoints:
point_ele = elevation_data.get_elevation(
waypoint.latitude, waypoint.longitude, approximate=False
waypoint.latitude, waypoint.longitude
)
waypoints.append(
[
Expand Down

0 comments on commit a402f39

Please sign in to comment.