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 ,
@@ -189,10 +188,12 @@ def _datetime_filter(
189188 minimum_distance : TimeDelta
190189):
191190 """
191+ Returns all the points of the DataFrame which are in a temporal distance.
192+
192193 Given a row referencing to a point, a DataFrame with
193194 multiple points and a minimum distance, it returns
194195 all the points of the DataFrame which are in a temporal
195- distance equal or smaller than the minimum distance
196+ distance equal or smaller than the minimum distance
196197 parameter.
197198
198199 Parameters
@@ -211,16 +212,19 @@ def _datetime_filter(
211212 a temporal distance equal or smaller than the minimum
212213 distance parameter.
213214 """
214- datetime = row ['datetime' ];
215- move_df ['temporal_distance' ] = (move_df ['datetime' ] - datetime ).abs ();
216- filtered = move_df [(move_df ['temporal_distance' ] < minimum_distance ) & (move_df ['temporal_distance' ] > - minimum_distance )];
217-
215+ datetime = row ['datetime' ]
216+ move_df ['temporal_distance' ] = (move_df ['datetime' ] - datetime ).abs ()
217+ filtered = move_df [
218+ (move_df ['temporal_distance' ] < minimum_distance )
219+ & (move_df ['temporal_distance' ] > - minimum_distance )
220+ ]
221+
218222 if (filtered .shape [0 ] > 0 ):
219223 filtered ['target_id' ] = row ['id' ]
220224 filtered ['target_lat' ] = row ['lat' ]
221225 filtered ['target_lon' ] = row ['lon' ]
222226 filtered ['target_datetime' ] = row ['datetime' ]
223-
227+
224228 return filtered
225229
226230
@@ -229,10 +233,12 @@ def _meters_filter(
229233 move_df : DataFrame ,
230234 minimum_distance : float ):
231235 """
236+ Returns all the points of the DataFrame which are in a spatial distance.
237+
232238 Given a row referencing to a point, a DataFrame with
233239 multiple points and a minimum distance, it returns
234240 all the points of the DataFrame which are in a spatial
235- distance (in meters) equal or smaller than the minimum distance
241+ distance (in meters) equal or smaller than the minimum distance
236242 parameter.
237243
238244 Parameters
@@ -243,24 +249,27 @@ def _meters_filter(
243249 The input trajectory data.
244250 minimum_distance: float
245251 the minimum spatial distance between the points in meters.
252+
246253 Returns
247- -------
254+ -------
248255 DataFrame
249256 dataframe with all the points of move_df which are in
250257 a spatial distance equal or smaller than the minimum
251258 distance parameter.
252259 """
253260 lat = row ['lat' ]
254261 lon = row ['lon' ]
255- move_df ['spatial_distance' ] = distances .euclidean_distance_in_meters (lat1 = lat , lon1 = lon , lat2 = move_df ['lat' ], lon2 = move_df ['lon' ])
262+ move_df ['spatial_distance' ] = distances .euclidean_distance_in_meters (
263+ lat1 = lat , lon1 = lon , lat2 = move_df ['lat' ], lon2 = move_df ['lon' ]
264+ )
256265 filtered = move_df [move_df ['spatial_distance' ] < minimum_distance ]
257-
266+
258267 if (filtered .shape [0 ] > 0 ):
259268 filtered ['target_id' ] = row ['id' ]
260269 filtered ['target_lat' ] = row ['lat' ]
261270 filtered ['target_lon' ] = row ['lon' ]
262271 filtered ['target_datetime' ] = row ['datetime' ]
263-
272+
264273 return filtered
265274
266275
@@ -271,8 +280,10 @@ def query_all_points_by_range(
271280 minimum_time : Optional [TimeDelta ] = timedelta (minutes = 2 ),
272281 datetime_label : Optional [Text ] = DATETIME ):
273282 """
274- Selects only the points between two Move Dataframes
275- that have the closest point within a spatial range
283+ Queries closest point within a spatial range based on meters and a temporal range.
284+
285+ Selects only the points between two Move Dataframes
286+ that have the closest point within a spatial range
276287 based on meters and a temporal range.
277288
278289 Parameters
@@ -295,21 +306,24 @@ def query_all_points_by_range(
295306 a spatial distance and temporal distance equal or smaller
296307 than the minimum distance parameters.
297308 """
298- result = pd .DataFrame ([]);
309+ if minimum_time is None :
310+ minimum_time = timedelta (minutes = 2 )
311+
312+ result = pd .DataFrame ([])
299313 total = traj1 .shape [0 ]
300314 count = 0
301- for index , row in traj1 .iterrows ():
315+ for _ , row in traj1 .iterrows ():
302316 clear_output (wait = True )
303- print (" {} de {}" .format (count , total ))
304- print (" {:.2f}%" .format ((count * 100 / total )))
317+ print (' {} de {}' .format (count , total ))
318+ print (' {:.2f}%' .format ((count * 100 / total )))
305319 coinc_points = _meters_filter (row , move_df , minimum_meters )
306320 coinc_points = _datetime_filter (row , coinc_points , minimum_time )
307321 result = coinc_points .append (result )
308-
322+
309323 count += 1
310-
324+
311325 clear_output (wait = True )
312- print (" {} de {}" .format (count , total ))
313- print (" {:.2f}%" .format ((count * 100 / total )))
326+ print (' {} de {}' .format (count , total ))
327+ print (' {:.2f}%' .format ((count * 100 / total )))
314328
315329 return result
0 commit comments