Skip to content

Commit

Permalink
update(GeoSpatialCollection): Add support for geopandas GeoSeries and…
Browse files Browse the repository at this point in the history
… GeoArray (#2085)
  • Loading branch information
jlarsen-usgs committed Feb 2, 2024
1 parent b9ca771 commit 86eb092
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
44 changes: 44 additions & 0 deletions autotest/test_geospatial_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,47 @@ def test_mixed_collection(
is_equal = gi2 == gi1[ix]

assert is_equal, "GeoSpatialCollection conversion error"


@requires_pkg("geopandas")
def test_geopandas_dtypes(
polygon,
poly_w_hole,
multipolygon,
point,
multipoint,
linestring,
multilinestring,
):
col = [
Shape.from_geojson(polygon),
Shape.from_geojson(poly_w_hole),
Shape.from_geojson(multipolygon),
Shape.from_geojson(point),
Shape.from_geojson(multipoint),
Shape.from_geojson(linestring),
Shape.from_geojson(multilinestring),
]

gi1 = [i.__geo_interface__ for i in col]
col = Collection(col)

gc1 = GeoSpatialCollection(col)
gdf = gc1.geo_dataframe

collections = [gdf, gdf.geometry, gdf.geometry.values]
for col in collections:
gc2 = GeoSpatialCollection(col)

for ix, gi in enumerate(gc2):
t = gi.flopy_geometry
gi2 = t.__geo_interface__

is_equal = gi2 == gi1[ix]

if not is_equal:
t = reversed(t)
gi2 = t.__geo_interface__
is_equal = gi2 == gi1[ix]

assert is_equal, "GeoSpatialCollection conversion error"
8 changes: 8 additions & 0 deletions flopy/utils/geospatial_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,14 @@ def __init__(self, obj, shapetype=None):
if k != "geometry":
self.__attributes[k] = obj[k].values

elif isinstance(obj, gpd.GeoSeries):
for geom in obj.values:
self.__collection.append(GeoSpatialUtil(geom))

elif isinstance(obj, gpd.array.GeometryArray):
for geom in obj:
self.__collection.append(GeoSpatialUtil(geom))

if not self.__collection:
raise AssertionError(
f"Reader is not installed for collection type: {type(obj)}"
Expand Down

0 comments on commit 86eb092

Please sign in to comment.