From 9c46b2bea466dbc687c1d67f29da12dc5d2eab45 Mon Sep 17 00:00:00 2001 From: Nicholas Reinicke Date: Thu, 30 May 2024 09:32:23 -0600 Subject: [PATCH 1/2] Fix how we download grade; bump version --- python/nrel/routee/compass/io/utils.py | 46 ++++++++++---------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/python/nrel/routee/compass/io/utils.py b/python/nrel/routee/compass/io/utils.py index 380f92b5..e7de9de4 100644 --- a/python/nrel/routee/compass/io/utils.py +++ b/python/nrel/routee/compass/io/utils.py @@ -3,8 +3,6 @@ from enum import Enum from pathlib import Path -import math -import itertools import logging from typing import Union, Optional @@ -38,29 +36,25 @@ def from_int(self, int: int) -> TileResolution: ) -def _cover_floats_with_integers(float_min: float, float_max: float) -> list[int]: - if float_max < float_min: - raise ValueError("float max must be greater than float min") - - start = math.floor(float_min) - end = math.ceil(float_max) +def get_usgs_tiles(lat_lon_pairs: list[tuple[float, float]]) -> list[str]: + def tile_index(lat, lon): + if lat < 0 or lon > 0: + raise ValueError( + f"USGS Tiles are not available for point ({lat}, {lon}). " + "Consider re-running with `grade=False`." + ) - integers = list(range(start, end + 1)) - return integers + lat_deg = int(lat) + 1 + lon_deg = abs(int(lon)) + 1 + return f"n{lat_deg:02}w{lon_deg:03}" -def _lat_lon_to_tile(coord: tuple[int, int]) -> str: - lat, lon = coord - if lat < 0: - lat_prefix = "s" - else: - lat_prefix = "n" - if lon < 0: - lon_prefix = "w" - else: - lon_prefix = "e" + tiles = set() + for lat, lon in lat_lon_pairs: + tile = tile_index(lat, lon) + tiles.add(tile) - return f"{lat_prefix}{abs(lat):02}{lon_prefix}{abs(lon):03}" + return list(tiles) def _build_download_link(tile: str, resolution=TileResolution.ONE_ARC_SECOND) -> str: @@ -146,15 +140,9 @@ def add_grade_to_graph( if api_key is None: node_gdf = ox.graph_to_gdfs(g, nodes=True, edges=False) - min_lat = node_gdf.y.min() - max_lat = node_gdf.y.max() - min_lon = node_gdf.x.min() - max_lon = node_gdf.x.max() - - lats = _cover_floats_with_integers(min_lat, max_lat) - lons = _cover_floats_with_integers(min_lon, max_lon) + all_points = [(t.y, t.x) for t in node_gdf.itertuples()] - tiles = map(_lat_lon_to_tile, itertools.product(lats, lons)) + tiles = get_usgs_tiles(all_points) if isinstance(resolution_arc_seconds, int): resolution = TileResolution.from_int(resolution_arc_seconds) From 0c5b031f834248819e2d92895f44a18c5a3fcc59 Mon Sep 17 00:00:00 2001 From: Nicholas Reinicke Date: Thu, 30 May 2024 09:37:35 -0600 Subject: [PATCH 2/2] more informative download error --- pyproject.toml | 2 +- python/nrel/routee/compass/io/utils.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9009ddf0..60b1b18c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "maturin" [project] name = "nrel.routee.compass" -version = "0.8.0" +version = "0.8.1" description = "An eco-routing tool build upon RouteE-Powertrain" readme = "README.md" documentation = "nrel.github.io/routee-compass" diff --git a/python/nrel/routee/compass/io/utils.py b/python/nrel/routee/compass/io/utils.py index e7de9de4..6cbab26a 100644 --- a/python/nrel/routee/compass/io/utils.py +++ b/python/nrel/routee/compass/io/utils.py @@ -86,7 +86,13 @@ def _download_tile( with requests.get(url, stream=True) as r: log.info(f"downloading {tile}") - r.raise_for_status() + try: + r.raise_for_status() + except requests.exceptions.HTTPError as e: + raise ValueError( + f"Failed to download USGS tile {tile} from {url}. " + "If this road network is outside of the US, consider re-running with `grade=False`." + ) from e destination.parent.mkdir(exist_ok=True)