Skip to content

Commit

Permalink
Eco 231 featutre/add spatial feature extraction (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgitonga authored Nov 22, 2023
1 parent ef5c775 commit 1ee6efe
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
55 changes: 55 additions & 0 deletions ecoscope/io/earthranger.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ def get_patrol_segments(self):
def get_patrol_observations(self, patrols_df, include_patrol_details=False, **kwargs):
"""
Download observations for provided `patrols_df`.
Parameters
----------
patrols_df : pd.DataFrame
Expand All @@ -694,6 +695,7 @@ def get_patrol_observations(self, patrols_df, include_patrol_details=False, **kw
Whether to merge patrol details into dataframe
kwargs
Additional parameters to pass to `get_subject_observations`.
Returns
-------
relocations : ecoscope.base.Relocations
Expand Down Expand Up @@ -776,6 +778,54 @@ def get_patrol_segment_events(
)
)

def get_spatial_features_group(self, spatial_features_group_id=None, **addl_kwargs):
"""
Download spatial features in a spatial features group for a given `spatial features group id`.
Parameters
----------
spatial_features_group_id :
Spatial Features Group UUID.
kwargs
Additional parameters to pass to `_get`.
Returns
-------
dataframe : GeoDataFrame of spatial features in a spatial features group.
"""
params = self._clean_kwargs(addl_kwargs, spatial_features_group_id=spatial_features_group_id)

object = f"spatialfeaturegroup/{spatial_features_group_id}/"
spatial_features_group = self._get(object, **params)

spatial_features = []
for spatial_feature in spatial_features_group["features"]:
spatial_features.append(spatial_feature["features"][0])

return gpd.GeoDataFrame.from_features(spatial_features)

def get_spatial_feature(self, spatial_feature_id=None, **addl_kwargs):
"""
Download spatial feature for a given `spatial feature id`.
Parameters
----------
spatial_feature_id :
Spatial Feature UUID.
kwargs
Additional parameters to pass to `_get`.
Returns
-------
dataframe : GeoDataFrame of spatial feature.
"""

params = self._clean_kwargs(addl_kwargs, spatial_feature_id=spatial_feature_id)

object = f"spatialfeature/{spatial_feature_id}/"
spatial_feature = self._get(object, **params)
return gpd.GeoDataFrame.from_features(spatial_feature["features"])

"""
POST Functions
"""
Expand All @@ -797,6 +847,7 @@ def post_source(
model_name
provider
additional
Returns
-------
pd.DataFrame
Expand Down Expand Up @@ -864,6 +915,7 @@ def post_subjectsource(
lower_bound_assigned_range
upper_bound_assigned_range
additional
Returns
-------
pd.DataFrame
Expand Down Expand Up @@ -899,6 +951,7 @@ def post_observations(
The source column in the observation dataframe
recorded_at_col : str
The observation recorded time column in the dataframe
Returns
-------
None
Expand Down Expand Up @@ -931,6 +984,7 @@ def post_event(
Parameters
----------
events
Returns
-------
pd.DataFrame:
Expand Down Expand Up @@ -1068,6 +1122,7 @@ def patch_event(
event_id
UUID for the event that will be updated.
events
Returns
-------
pd.DataFrame:
Expand Down
47 changes: 46 additions & 1 deletion notebooks/01. IO/EarthRanger_IO.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"tags": []
},
Expand Down Expand Up @@ -624,6 +624,51 @@
"relocs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## GET `SPATIAL FEATURES`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### GET `SPATIAL FEATURE` from `spatial_feature_id`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"spatial_feature = er_io.get_spatial_feature(\n",
" spatial_feature_id=\"8868718f-0154-45bf-a74d-a66706ef958f\"\n",
" )\n",
"spatial_feature"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### GET `SPATIAL FEATURES` from `spatial_features_group_id`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"spatial_features = er_io.get_spatial_features_group(\n",
" spatial_features_group_id=\"15698426-7e0f-41df-9bc3-495d87e2e097\"\n",
" )\n",
"spatial_features"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
12 changes: 12 additions & 0 deletions tests/test_earthranger_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,15 @@ def test_get_patrol_observations(er_io):
def test_users(er_io):
users = pd.DataFrame(er_io.get_users())
assert not users.empty


def test_get_spatial_feature(er_io):
spatial_feature = er_io.get_spatial_feature(spatial_feature_id="8868718f-0154-45bf-a74d-a66706ef958f")
assert not spatial_feature.empty


def test_get_spatial_features_group(er_io):
spatial_features = er_io.get_spatial_features_group(
spatial_features_group_id="15698426-7e0f-41df-9bc3-495d87e2e097"
)
assert not spatial_features.empty

0 comments on commit 1ee6efe

Please sign in to comment.