Skip to content

Commit

Permalink
update(GeoSpatialCollection): add support for geopandas GeoDataFrame …
Browse files Browse the repository at this point in the history
…objects

* update test_geospatial_util.py to include GeoDataFrame tests
* add geopandas as optional dependency
  • Loading branch information
jlarsen-usgs committed Jan 17, 2024
1 parent 97da396 commit d39b64c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
14 changes: 9 additions & 5 deletions autotest/test_geospatial_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def test_multilinestring(multilinestring):
assert gi1 == gi2, "GeoSpatialUtil multilinestring conversion error"


@requires_pkg("shapely", "geojson")
@requires_pkg("shapely", "geojson", "geopandas")
def test_polygon_collection(polygon, poly_w_hole, multipolygon):
col = [
Shape.from_geojson(polygon),
Expand All @@ -377,8 +377,9 @@ def test_polygon_collection(polygon, poly_w_hole, multipolygon):
points = gc1.points
geojson = gc1.geojson
fp_geo = gc1.flopy_geometry
gdf = gc1.geo_dataframe

collections = [shp, shply, points, geojson, fp_geo]
collections = [shp, shply, points, geojson, fp_geo, gdf]
for col in collections:
gc2 = GeoSpatialCollection(col, shapetype)

Expand Down Expand Up @@ -410,8 +411,9 @@ def test_point_collection(point, multipoint):
points = gc1.points
geojson = gc1.geojson
fp_geo = gc1.flopy_geometry
gdf = gc1.geo_dataframe

collections = [shp, shply, points, geojson, fp_geo]
collections = [shp, shply, points, geojson, fp_geo, gdf]
for col in collections:
gc2 = GeoSpatialCollection(col, shapetype)
gi2 = [i.flopy_geometry.__geo_interface__ for i in gc2]
Expand Down Expand Up @@ -439,8 +441,9 @@ def test_linestring_collection(linestring, multilinestring):
points = gc1.points
geojson = gc1.geojson
fp_geo = gc1.flopy_geometry
gdf = gc1.geo_dataframe

collections = [shp, shply, points, geojson, fp_geo]
collections = [shp, shply, points, geojson, fp_geo, gdf]
for col in collections:
gc2 = GeoSpatialCollection(col, shapetype)
gi2 = [i.flopy_geometry.__geo_interface__ for i in gc2]
Expand Down Expand Up @@ -485,8 +488,9 @@ def test_mixed_collection(
points = gc1.points
geojson = gc1.geojson
fp_geo = gc1.flopy_geometry
gdf = gc1.geo_dataframe

collections = [shp, shply, lshply, points, geojson, fp_geo]
collections = [shp, shply, lshply, points, geojson, fp_geo, gdf]
for col in collections:
gc2 = GeoSpatialCollection(col, shapetype)

Expand Down
32 changes: 32 additions & 0 deletions flopy/utils/geospatial_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ def __init__(self, obj, shapetype=None):
self.__shapefile = import_optional_dependency(
"shapefile", errors="silent"
)
gpd = import_optional_dependency(
"geopandas", errors="silent"
)

shapely_geo = import_optional_dependency(
"shapely.geometry", errors="silent"
)
Expand All @@ -275,6 +279,7 @@ def __init__(self, obj, shapetype=None):
self._flopy_geometry = None
self._points = None
self.__shapetype = None
self.__attributes = None

if isinstance(obj, Collection):
for shape in obj:
Expand Down Expand Up @@ -361,6 +366,16 @@ def __init__(self, obj, shapetype=None):
for geom in obj.geoms:
self.__collection.append(GeoSpatialUtil(geom))

if gpd is not None:
if isinstance(obj, gpd.GeoDataFrame):
self.__attributes = {}
for geom in obj.geometry.values:
self.__collection.append(GeoSpatialUtil(geom))

for k in list(obj):
if k != "geometry":
self.__attributes[k] = obj[k].values

if not self.__collection:
raise AssertionError(
f"Reader is not installed for collection type: {type(obj)}"
Expand Down Expand Up @@ -419,6 +434,23 @@ def shapely(self):
)
return self._shapely

@property
def geo_dataframe(self):
"""
Property that returns a geopandas DataFrame
Returns
-------
geopandas.GeoDataFrame
"""
gpd = import_optional_dependency("geopandas")
data = {"geometry": self.shapely.geoms}
if self.__attributes is not None:
for k, v in self.__attributes.items():
data[k] = v
gdf = gpd.GeoDataFrame(data)
return gdf

@property
def geojson(self):
"""
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ optional = [
"descartes",
"fiona",
"geojson",
"geopandas",
"imageio",
"netcdf4",
"pymetis ; platform_system != 'Windows'",
Expand Down

0 comments on commit d39b64c

Please sign in to comment.