Skip to content

Commit

Permalink
Convert spatial_extent to list[float] for most functions
Browse files Browse the repository at this point in the history
  • Loading branch information
trey-stafford committed Nov 11, 2024
1 parent c89df21 commit b632617
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions icepyx/core/spatial.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from itertools import chain
import os
from typing import Literal, Optional, Union, cast
import warnings
Expand All @@ -16,6 +17,33 @@
ExtentType = Literal["bounding_box", "polygon"]


def _convert_spatial_extent_to_list_of_floats(
spatial_extent: Union[list[float], list[tuple[float, float]], Polygon],
) -> list[float]:
# This is already a list of floats
if isinstance(spatial_extent, list) and isinstance(spatial_extent[0], float):
spatial_extent = cast(list[float], spatial_extent)
return spatial_extent
elif isinstance(spatial_extent, Polygon):
# Convert `spatial_extent` into a list of floats like:
# `[longitude1, latitude1, longitude2, latitude2, ...]`
spatial_extent = [
float(coord) for point in spatial_extent.exterior.coords for coord in point
]
return spatial_extent
elif isinstance(spatial_extent, list) and isinstance(spatial_extent[0], tuple):
# Convert the list of tuples into a flat list of floats
spatial_extent = cast(list[tuple[float, float]], spatial_extent)
spatial_extent = list(chain.from_iterable(spatial_extent))
return spatial_extent
else:
raise TypeError(
"Unrecognized spatial_extent that"
" cannot be converted into a list of floats:"
f"{spatial_extent=}"
)


def _geodataframe_from_bounding_box(
spatial_extent: list[float],
xdateline: bool,
Expand Down Expand Up @@ -136,19 +164,22 @@ def geodataframe(
)

#### Non-file processing
# Most functions that this function calls requires the spatial extent as a
# list of floats. This function provides that.
spatial_extent_list = _convert_spatial_extent_to_list_of_floats(
spatial_extent=spatial_extent,
)

if xdateline is None:
assert isinstance(spatial_extent, list)
assert isinstance(spatial_extent[0], float)
spatial_extent = cast(list[float], spatial_extent)
xdateline = check_dateline(extent_type, spatial_extent)
xdateline = check_dateline(
extent_type,
spatial_extent_list,
)

# DevGoal: Currently this if/else within this elif are not tested...
if extent_type == "bounding_box":
assert isinstance(spatial_extent, list)
assert isinstance(spatial_extent[0], float)
spatial_extent = cast(list[float], spatial_extent)
return _geodataframe_from_bounding_box(
spatial_extent=spatial_extent,
spatial_extent=spatial_extent_list,
xdateline=xdateline,
)

Expand All @@ -161,11 +192,8 @@ def geodataframe(
)

# The input must be a list of floats.
assert isinstance(spatial_extent, list)
assert isinstance(spatial_extent[0], float)
spatial_extent = cast(list[float], spatial_extent)
return _geodataframe_from_polygon_list(
spatial_extent=spatial_extent,
spatial_extent=spatial_extent_list,
xdateline=xdateline,
)

Expand Down

0 comments on commit b632617

Please sign in to comment.