Skip to content

Commit

Permalink
add function to extract spatial features
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgitonga committed Nov 21, 2023
1 parent 4b8908e commit 962beb1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
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
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 962beb1

Please sign in to comment.