Skip to content

Commit

Permalink
spatial.geodataframe supports a list of tuples for spatial_extent
Browse files Browse the repository at this point in the history
Also added tests for the Polygon and `list[tuple[float, float]]` case.
  • Loading branch information
trey-stafford committed Oct 30, 2024
1 parent cbc761c commit 52037e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
17 changes: 14 additions & 3 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 @@ -18,7 +19,7 @@

def geodataframe(
extent_type: ExtentType,
spatial_extent: Union[str, list[float], Polygon],
spatial_extent: Union[str, list[float], list[tuple[float, float]], Polygon],
file: bool = False,
xdateline: Optional[bool] = None,
) -> gpd.GeoDataFrame:
Expand All @@ -31,7 +32,9 @@ def geodataframe(
One of 'bounding_box' or 'polygon', indicating what type of input the spatial extent is
spatial_extent :
A list containing the spatial extent, a shapely.Polygon, OR a string containing a filename.
A list containing the spatial extent, a shapely.Polygon, a list of
tuples (i.e.,, `[(longitude1, latitude1), (longitude2, latitude2),
...]`)containing floats, OR a string containing a filename.
If file is False, spatial_extent should be a shapely.Polygon,
list of bounding box coordinates in decimal degrees of [lower-left-longitude,
lower-left-latitute, upper-right-longitude, upper-right-latitude] or polygon vertices as
Expand Down Expand Up @@ -84,9 +87,17 @@ def geodataframe(
# Convert `spatial_extent` into a list of floats like:
# `[longitude1, latitude1, longitude2, latitude2, ...]`
spatial_extent = [
coord for point in spatial_extent.exterior.coords for coord in point
float(coord) for point in spatial_extent.exterior.coords for coord in point
]

# We are dealing with a `list[tuple[float, float]]`
if 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))

spatial_extent = cast(list[float], spatial_extent)

if xdateline is not None:
xdateline = xdateline
else:
Expand Down
25 changes: 25 additions & 0 deletions icepyx/tests/unit/test_spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,31 @@ def test_gdf_from_multi_bbox():
assert obs.geometry[0].equals(exp.geometry[0])


def test_gdf_from_polygon():
polygon = Polygon(list(zip([-55, -55, -48, -48, -55], [68, 71, 71, 68, 68])))
obs = spat.geodataframe("polygon", polygon)
exp = gpd.GeoDataFrame(geometry=[polygon])

# make sure there is only one geometry before comparing them
assert len(obs.geometry) == 1
assert len(exp.geometry) == 1
assert obs.geometry[0].equals(exp.geometry[0])


def test_gdf_from_list_tuples():
polygon_tuples = list(
zip([-55.0, -55.0, -48.0, -48.0, -55.0], [68.0, 71.0, 71.0, 68.0, 68.0])
)
obs = spat.geodataframe("polygon", polygon_tuples)
geom = [Polygon(polygon_tuples)]
exp = gpd.GeoDataFrame(geometry=geom)

# make sure there is only one geometry before comparing them
assert len(obs.geometry) == 1
assert len(exp.geometry) == 1
assert obs.geometry[0].equals(exp.geometry[0])


# Potential tests to include once multipolygon and complex polygons are handled

# def test_gdf_from_strpoly_one_simple():
Expand Down

0 comments on commit 52037e9

Please sign in to comment.