Skip to content

Commit

Permalink
Get patrol events (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Wu authored Jul 24, 2024
1 parent d26f327 commit 218fcf1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
33 changes: 33 additions & 0 deletions ecoscope/io/earthranger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import requests
from dateutil import parser
from erclient.client import ERClient, ERClientException, ERClientNotFound
from shapely.geometry import shape
from tqdm.auto import tqdm

import ecoscope
Expand Down Expand Up @@ -694,6 +695,38 @@ def get_patrols(self, since=None, until=None, patrol_type=None, status=None, **a
df = df.sort_values(by="serial_number").reset_index(drop=True)
return df

def get_patrol_events(self, since=None, until=None, patrol_type=None, status=None, **addl_kwargs):
"""
Parameters
----------
since:
lower date range
until:
upper date range
patrol_type:
Comma-separated list of type of patrol UUID
status
Comma-separated list of 'scheduled'/'active'/'overdue'/'done'/'cancelled'
Returns
-------
events : pd.DataFrame
DataFrame of queried patrols
"""
patrol_df = self.get_patrols(since=since, until=until, patrol_type=patrol_type, status=status, **addl_kwargs)

events = []
for _, row in patrol_df.iterrows():
if row["patrol_segments"]:
for segment in row["patrol_segments"]:
for event in segment.get("events", []):
event["patrol_id"] = row.get("id")
event["patrol_segment_id"] = segment.get("id")
events.append(event)
events_df = pd.DataFrame(events)

events_df["geometry"] = events_df["geojson"].apply(lambda x: shape(x.get("geometry")))
return gpd.GeoDataFrame(events_df, geometry="geometry", crs=4326)

def get_patrol_segments_from_patrol_id(self, patrol_id, **addl_kwargs):
"""
Download patrols for a given `patrol id`.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_earthranger_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ def test_get_patrols(er_io):
assert len(patrols) > 0


def test_get_patrol_events(er_io):
events = er_io.get_patrol_events()
assert "id" in events
assert "event_type" in events
assert "geometry" in events
assert "patrol_id" in events
assert "patrol_segment_id" in events


def test_post_observations(er_io):
observations = gpd.GeoDataFrame.from_dict(
[
Expand Down

0 comments on commit 218fcf1

Please sign in to comment.