Skip to content

Commit

Permalink
Add to_geopandas() method (#38)
Browse files Browse the repository at this point in the history
* Add to_geopandas method

* mock patch ImportError

* Update placeholder

* Fix flake8 error

* Remove the `from_geopandas()` method

* Remove the unnecessary serialization step

* Fix isort issue

* Assert that df has expected features

* Allow passing the crs parameter in as a kwarg
  • Loading branch information
andersy005 authored Feb 5, 2020
1 parent ddecfca commit f74a8f3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions ci/environment-dev-3.6.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ dependencies:
- xarray
- rasterio
- ipywidgets
- geopandas
- pip:
- sphinx_copybutton
1 change: 1 addition & 0 deletions ci/environment-dev-3.7-upstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies:
- xarray
- rasterio
- ipywidgets
- geopandas
- pip:
- sphinx_copybutton
- git+https://github.com/sat-utils/sat-stac.git
Expand Down
1 change: 1 addition & 0 deletions ci/environment-dev-3.7.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ dependencies:
- xarray
- rasterio
- ipywidgets
- geopandas
- pip:
- sphinx_copybutton
27 changes: 27 additions & 0 deletions intake_stac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@ def _load(self):
def _get_metadata(self, **kwargs):
return kwargs

def to_geopandas(self, crs=None):
"""
Load the STAC Item Collection into a geopandas GeoDataFrame
Parameters
----------
crs : str or dict (optional)
Coordinate reference system to set on the resulting frame.
Returns
-------
GeoDataFrame
"""
try:
import geopandas as gpd
except ImportError:
raise ImportError(
"Using to_geopandas requires the `geopandas` package."
"You can install it via Pip or Conda."
)

if crs is None:
crs = {"init": "epsg:4326"}
gf = gpd.GeoDataFrame.from_features(self._stac_obj.geojson(), crs=crs)
return gf


class StacCollection(AbstractStacCatalog):
"""
Expand Down
34 changes: 34 additions & 0 deletions intake_stac/tests/test_catalog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys

import intake
import pytest
Expand Down Expand Up @@ -215,6 +216,39 @@ def test_stac_entry_constructor():
assert d["metadata"] == item


def test_cat_to_geopandas(stac_item_collection_obj):
import geopandas

cat = StacItemCollection(stac_item_collection_obj)
df = cat.to_geopandas()
assert isinstance(df, geopandas.GeoDataFrame)
assert len(df) == len(cat._stac_obj)
assert isinstance(df.geometry, geopandas.GeoSeries)
assert isinstance(df.geometry.values, geopandas.array.GeometryArray)
assert isinstance(df.geometry.dtype, geopandas.array.GeometryDtype)
assert df.crs == {"init": "epsg:4326"}


@pytest.mark.parametrize("crs", ["IGNF:ETRS89UTM28", "epsg:26909"])
def test_cat_to_geopandas_crs(crs, stac_item_collection_obj):
import geopandas

cat = StacItemCollection(stac_item_collection_obj)
df = cat.to_geopandas(crs=crs)
assert isinstance(df, geopandas.GeoDataFrame)
assert len(df) == len(cat._stac_obj)
assert df.crs == crs


def test_cat_to_missing_geopandas(stac_item_collection_obj, monkeypatch):
from unittest import mock

with pytest.raises(ImportError):
with mock.patch.dict(sys.modules, {"geopandas": None}):
cat = StacItemCollection(stac_item_collection_obj)
_ = cat.to_geopandas()


# TODO - Add tests for:
# StacEntry._get_driver()
# StacEntryy._get_args()
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest>=4.5.0
pytest-cov>=2.7.1
geopandas
-r requirements.txt

0 comments on commit f74a8f3

Please sign in to comment.