33
44range_query,
55knn_query,
6- query_all_points_by_range,
6+ query_all_points_by_range,
77
88"""
99
10+ from datetime import timedelta
1011from typing import Optional , Text
1112
1213import numpy as np
1314import pandas as pd
15+ from IPython .display import clear_output
1416from pandas import DataFrame
1517
1618from pymove .utils import distances
1719from pymove .utils .constants import DATETIME , LATITUDE , LONGITUDE , MEDP , MEDT , TRAJ_ID
1820from pymove .utils .log import logger , progress_bar
1921
20- from datetime import timedelta
21- from IPython .display import clear_output
22-
2322
2423def range_query (
2524 traj : DataFrame ,
@@ -185,10 +184,12 @@ def dist_measure(traj, this, latitude, longitude, datetime):
185184
186185def _datetime_filter (row , move_df , minimum_distance ):
187186 """
187+ Returns all the points of the DataFrame which are in a temporal distance.
188+
188189 Given a row referencing to a point, a DataFrame with
189190 multiple points and a minimum distance, it returns
190191 all the points of the DataFrame which are in a temporal
191- distance equal or smaller than the minimum distance
192+ distance equal or smaller than the minimum distance
192193 parameter.
193194
194195 Parameters
@@ -207,25 +208,30 @@ def _datetime_filter(row, move_df, minimum_distance):
207208 a temporal distance equal or smaller than the minimum
208209 distance parameter.
209210 """
210- datetime = row ['datetime' ];
211- move_df ['temporal_distance' ] = (move_df ['datetime' ] - datetime ).abs ();
212- filtered = move_df [(move_df ['temporal_distance' ] < minimum_distance ) & (move_df ['temporal_distance' ] > - minimum_distance )];
213-
211+ datetime = row ['datetime' ]
212+ move_df ['temporal_distance' ] = (move_df ['datetime' ] - datetime ).abs ()
213+ filtered = move_df [
214+ (move_df ['temporal_distance' ] < minimum_distance )
215+ & (move_df ['temporal_distance' ] > - minimum_distance )
216+ ]
217+
214218 if (filtered .shape [0 ] > 0 ):
215219 filtered ['target_id' ] = row ['id' ]
216220 filtered ['target_lat' ] = row ['lat' ]
217221 filtered ['target_lon' ] = row ['lon' ]
218222 filtered ['target_datetime' ] = row ['datetime' ]
219-
223+
220224 return filtered
221225
222226
223227def _meters_filter (row , move_df , minimum_distance ):
224228 """
229+ Returns all the points of the DataFrame which are in a spatial distance.
230+
225231 Given a row referencing to a point, a DataFrame with
226232 multiple points and a minimum distance, it returns
227233 all the points of the DataFrame which are in a spatial
228- distance (in meters) equal or smaller than the minimum distance
234+ distance (in meters) equal or smaller than the minimum distance
229235 parameter.
230236
231237 Parameters
@@ -236,31 +242,42 @@ def _meters_filter(row, move_df, minimum_distance):
236242 The input trajectory data.
237243 minimum_distance: float
238244 the minimum spatial distance between the points in meters.
245+
239246 Returns
240- -------
247+ -------
241248 DataFrame
242249 dataframe with all the points of move_df which are in
243250 a spatial distance equal or smaller than the minimum
244251 distance parameter.
245252 """
246253 lat = row ['lat' ]
247254 lon = row ['lon' ]
248- move_df ['spatial_distance' ] = distances .euclidean_distance_in_meters (lat1 = lat , lon1 = lon , lat2 = move_df ['lat' ], lon2 = move_df ['lon' ])
255+ move_df ['spatial_distance' ] = distances .euclidean_distance_in_meters (
256+ lat1 = lat , lon1 = lon , lat2 = move_df ['lat' ], lon2 = move_df ['lon' ]
257+ )
249258 filtered = move_df [move_df ['spatial_distance' ] < minimum_distance ]
250-
259+
251260 if (filtered .shape [0 ] > 0 ):
252261 filtered ['target_id' ] = row ['id' ]
253262 filtered ['target_lat' ] = row ['lat' ]
254263 filtered ['target_lon' ] = row ['lon' ]
255264 filtered ['target_datetime' ] = row ['datetime' ]
256-
265+
257266 return filtered
258267
259268
260- def query_all_points_by_range (traj1 , move_df , minimum_meters = 100 , minimum_time = timedelta (minutes = 2 ), datetime_label = DATETIME ):
269+ def query_all_points_by_range (
270+ traj1 ,
271+ move_df ,
272+ minimum_meters = 100 ,
273+ minimum_time = None ,
274+ datetime_label = DATETIME
275+ ):
261276 """
262- Selects only the points between two Move Dataframes
263- that have the closest point within a spatial range
277+ Queries closest point within a spatial range based on meters and a temporal range.
278+
279+ Selects only the points between two Move Dataframes
280+ that have the closest point within a spatial range
264281 based on meters and a temporal range.
265282
266283 Parameters
@@ -283,21 +300,24 @@ def query_all_points_by_range(traj1, move_df, minimum_meters=100, minimum_time=t
283300 a spatial distance and temporal distance equal or smaller
284301 than the minimum distance parameters.
285302 """
286- result = pd .DataFrame ([]);
303+ if minimum_time is None :
304+ minimum_time = timedelta (minutes = 2 )
305+
306+ result = pd .DataFrame ([])
287307 total = traj1 .shape [0 ]
288308 count = 0
289- for index , row in traj1 .iterrows ():
309+ for _ , row in traj1 .iterrows ():
290310 clear_output (wait = True )
291- print (" {} de {}" .format (count , total ))
292- print (" {:.2f}%" .format ((count * 100 / total )))
311+ print (' {} de {}' .format (count , total ))
312+ print (' {:.2f}%' .format ((count * 100 / total )))
293313 coinc_points = _meters_filter (row , move_df , minimum_meters )
294314 coinc_points = _datetime_filter (row , coinc_points , minimum_time )
295315 result = coinc_points .append (result )
296-
316+
297317 count += 1
298-
318+
299319 clear_output (wait = True )
300- print (" {} de {}" .format (count , total ))
301- print (" {:.2f}%" .format ((count * 100 / total )))
320+ print (' {} de {}' .format (count , total ))
321+ print (' {:.2f}%' .format ((count * 100 / total )))
302322
303323 return result
0 commit comments