diff --git a/ecoscope/io/earthranger.py b/ecoscope/io/earthranger.py index 73bf2f4d..daa6417b 100644 --- a/ecoscope/io/earthranger.py +++ b/ecoscope/io/earthranger.py @@ -676,51 +676,56 @@ def get_patrol_observations(self, patrols_df, patrol_type=None, include_patrol_d relocations : ecoscope.base.Relocations """ - observations = [] - for _, patrol in patrols_df.iterrows(): - for patrol_segment in patrol["patrol_segments"]: - subject_id = (patrol_segment.get("leader") or {}).get("id") - 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, patrol_start_time}: - continue - try: - observation = self.get_subject_observations( - subject_ids=[subject_id], since=patrol_start_time, until=patrol_end_time - ) - 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={subject_id} start_time={patrol_start_time}" - f"end_time={patrol_end_time} failed for: {e}" - ) - df = ecoscope.base.Relocations(pd.concat(observations)) - if include_patrol_details: - return df.set_index("id") - return df + observations = [] + for _, patrol in patrols_df.iterrows(): + for patrol_segment in patrol["patrol_segments"]: + subject_id = (patrol_segment.get("leader") or {}).get("id") + 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, patrol_start_time}: + continue + + try: + observation = self.get_subject_observations( + subject_ids=[subject_id], + since=patrol_start_time, + until=patrol_end_time + ) + 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={subject_id} start_time={patrol_start_time}" + f"end_time={patrol_end_time} failed for: {e}" + ) + + df = ecoscope.base.Relocations(pd.concat(observations)) + if include_patrol_details: + return df.set_index("id") + return df def get_patrol_segment_events( self,