Skip to content

Commit

Permalink
feat!: remove out_crs as an arg in get_map since 3dep is just inconsi…
Browse files Browse the repository at this point in the history
…stent when this value is not its native 3857. while frustrating, at the end of the day this makes using the package more consistent.
  • Loading branch information
Taher Chegini committed Jan 20, 2025
1 parent 944bdd6 commit a2f62fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/seamless_3dep/_https_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class ServiceError(Exception):

def __init__(self, url: str, err: str) -> None:
self.message = (
f"Service returned the following error:\nURL: {url}\nERROR: {err}" if url else err
f"3DEP web service returned the following error:\nURL: {url}\nERROR: {err}"
if url
else err
)
super().__init__(self.message)

Expand Down
38 changes: 17 additions & 21 deletions src/seamless_3dep/seamless_3dep.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> f
def decompose_bbox(
bbox: tuple[float, float, float, float],
res: int,
pixel_max: int | None = MAX_PIXELS,
pixel_max: int | None,
buff_npixels: float = 0.0,
) -> tuple[list[tuple[float, float, float, float]], int, int]:
"""Divide a Bbox into equal-area sub-bboxes based on pixel count.
Expand All @@ -87,9 +87,9 @@ def decompose_bbox(
Bounding box coordinates in decimal degrees like so: (west, south, east, north).
res : int
Resolution of the domain in meters.
pixel_max : int, optional
pixel_max : int
Maximum number of pixels allowed in each sub-bbox. If None, the bbox
is not decomposed. Defaults to 8 million.
is not decomposed.
buff_npixels : float, optional
Number of pixels to buffer each sub-bbox by, defaults to 0.
Expand Down Expand Up @@ -194,8 +194,10 @@ def get_dem(
Target resolution of the DEM in meters, by default 10.
Must be one of 10, 30, or 60.
pixel_max : int, optional
Maximum number of pixels allowed in decomposing the bbox into equal-area
sub-bboxes, by default 8 million. If ``None``, the bbox is not decomposed.
Maximum number of pixels allowed in each sub-bbox for decomposing the bbox
into equal-area sub-bboxes, defaults to 8 million. If ``None``, the bbox
is not decomposed and is downloaded as a single file. Values more than
8 million are not allowed.
Returns
-------
Expand All @@ -205,6 +207,9 @@ def get_dem(
if res not in VRTLinks:
raise ValueError("`res` must be one of 10, 30, or 60 meters.")

if pixel_max is not None and pixel_max > MAX_PIXELS:
raise ValueError(f"`pixel_max` must be less than {MAX_PIXELS}.")

bbox_list, _, _ = decompose_bbox(bbox, res, pixel_max)

vrt_pool = VRTPool.get_dataset_reader(res)
Expand Down Expand Up @@ -247,10 +252,9 @@ def get_map(
bbox: tuple[float, float, float, float],
save_dir: str | Path,
res: int = 10,
out_crs: int = 5070,
pixel_max: int | None = MAX_PIXELS,
) -> list[Path]:
"""Get topo maps within US from 3DEP at any resolution.
"""Get topo maps in 3857 coordinate system within US from 3DEP at any resolution.
Parameters
----------
Expand All @@ -275,14 +279,11 @@ def get_map(
Path to save the GeoTiff files.
res : int, optional
Target resolution of the map in meters, by default 10.
out_crs : int, optional
Coordinate reference system of the output GeoTiff, by default 5070. This must
be a well-known ID (WKID) like 3857 or 5070. Do not use 4326 since at the moment
the 3DEP web service does not return correct results.
pixel_max : int, optional
Maximum number of pixels allowed in each sub-bbox for decomposing the bbox
into equal-area sub-bboxes, defaults to 8 million. If ``None``, the bbox
is not decomposed and is downloaded as a single file.
is not decomposed and is downloaded as a single file. Values more than
8 million are not allowed.
Returns
-------
Expand All @@ -306,13 +307,8 @@ def get_map(
if map_type not in valid_types:
raise ValueError(f"`map_type` must be one of {valid_types}.")

if not isinstance(out_crs, int):
raise TypeError("`out_crs` must be an integer representing WKID.")

if out_crs == 4326:
msg = "`out_crs` cannot be 4326 since 3DEP does not return correct results."
msg += " Use a different CRS like 3857 or 5070."
raise ValueError(msg)
if pixel_max is not None and pixel_max > MAX_PIXELS:
raise ValueError(f"`pixel_max` must be less than {MAX_PIXELS}.")

bbox_list, sub_width, sub_height = decompose_bbox(bbox, res, pixel_max)

Expand All @@ -321,10 +317,10 @@ def get_map(
save_dir.mkdir(parents=True, exist_ok=True)

rule = map_type.replace(" ", "_").lower()
tiff_list = [save_dir / f"{rule}_{_create_hash(box, res, out_crs)}.tiff" for box in bbox_list]
tiff_list = [save_dir / f"{rule}_{_create_hash(box, res, 3857)}.tiff" for box in bbox_list]
params = {
"bboxSR": 4326,
"imageSR": out_crs,
"imageSR": 3857,
"size": f"{sub_width},{sub_height}",
"format": "tiff",
"interpolation": "RSP_BilinearInterpolation",
Expand Down

0 comments on commit a2f62fb

Please sign in to comment.