Skip to content

Commit

Permalink
add include_patrol_details parameter to get_patrol_observations()
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgitonga authored Sep 15, 2023
1 parent a48df29 commit f4e7742
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions ecoscope/io/earthranger.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@ def get_events(
return df.set_index("id")

def get_patrol_types(self):
return pd.DataFrame(self._get("activity/patrols/types"))
df = pd.DataFrame(self._get(path="activity/patrols/types/"))
return df.set_index("id")

def get_patrols(self, since=None, until=None, patrol_type=None, status=None, **addl_kwargs):
"""
Expand Down Expand Up @@ -657,46 +658,78 @@ def get_patrol_segments(self):
self.get_objects_multithreaded(object=object, threads=self.tcp_limit, page_size=self.sub_page_size)
)

def get_observations_for_patrols(self, patrols_df, **kwargs):
def get_patrol_observations(
self,
patrols_df,
patrol_type=None,
include_patrol_details=False,
**kwargs):
"""
Download observations for provided `patrols_df`.
Parameters
----------
patrols_df : pd.DataFrame
Data returned from a call to `get_patrols`.
patrol_type : UUID
Comma-separated list of type of patrol UUID
include_patrol_details : bool, optional
Whether to merge patrol details into dataframe
kwargs
Additional parameters to pass to `get_subject_observations`.
Returns
-------
relocations : ecoscope.base.Relocations
"""

params = self._clean_kwargs(
kwargs,
patrol_type=patrol_type,
include_patrol_details=include_patrol_details,
)

observations = []
for _, patrol in patrols_df.iterrows():
for patrol_segment in patrol["patrol_segments"]:
subject_id = (patrol_segment.get("leader") or {}).get("id")
start_time = (patrol_segment.get("time_range") or {}).get("start_time")
end_time = (patrol_segment.get("time_range") or {}).get("end_time")
patrol_start_time = (patrol_segment.get("time_range") or {}).get("start_time")
patrol_end_time = (patrol_segment.get("time_range") or {}).get("end_time")

if None in {subject_id, start_time}:
if None in {subject_id, patrol_start_time}:
continue
try:
observations.append(
self.get_subject_observations(
observation = self.get_subject_observations(
subject_ids=[subject_id],
since=start_time,
until=end_time,
**kwargs,
since=patrol_start_time,
until=patrol_end_time,
**kwargs
)
)
if include_patrol_details:
observation["patrol_id"] = patrol["id"]
observation["patrol_serial_number"] = patrol["serial_number"]
observation["patrol_start_time"] = patrol_start_time
observation["patrol_end_time"] = patrol_end_time
observation["patrol_type"] = patrol_type
observation = observation.reset_index().merge(
pd.DataFrame(self.get_patrol_types()).add_prefix("patrol_type__"),
left_on="patrol_type",
right_on="id"
).drop(
columns=[
"patrol_type__ordernum",
"patrol_type__icon_id",
"patrol_type__default_priority",
"patrol_type__is_active"
]
)
observations.append(observation)
except Exception as e:
# print(f'Getting observations for {subject_id=} {start_time=} {end_time=} failed for: {e}')
print(
f"Getting observations for subject_id={subject_id} start_time={start_time} end_time={end_time}"
f"Getting observations for subject_id={subject_id} start_time={patrol_start_time} end_time={patrol_end_time}"
f"failed for: {e}"
)
return ecoscope.base.Relocations(pd.concat(observations)).set_index("id")
return ecoscope.base.Relocations(pd.concat(observations))

def get_patrol_segment_events(
self,
patrol_segment_id=None,
Expand Down

0 comments on commit f4e7742

Please sign in to comment.