11import numpy as np
22import pandas as pd
3- from shapely import wkt
4- from shapely .geometry import Point
53
64
75def distance_on_sphere (p1 , p2 ):
86 """
9- p1 and p2 are two lists that have two elements. They are numpy arrays of the long and lat
10- coordinates of the points in set1 and set2
7+ p1 and p2 are two lists that have two elements. They are numpy arrays of the long
8+ and lat coordinates of the points in set1 and set2
119
12- Calculate the distance between two points on the Earth's surface using the haversine formula.
10+ Calculate the distance between two points on the Earth's surface using the
11+ haversine formula.
1312
1413 Args:
15- p1 (list): Array containing the longitude and latitude coordinates of points FROM which the distance to be calculated in degree
16- p2 (list): Array containing the longitude and latitude coordinates of points TO which the distance to be calculated in degree
14+ p1 (list): Array containing the longitude and latitude coordinates of points
15+ FROM which the distance to be calculated in degree
16+ p2 (list): Array containing the longitude and latitude coordinates of points
17+ TO which the distance to be calculated in degree
1718
1819 Returns:
19- numpy.ndarray: Array containing the distances between the two points on the sphere in kilometers.
20+ numpy.ndarray: Array containing the distances between the two points on the
21+ sphere in kilometers.
2022
21- This function computes the distance between two points on the Earth's surface using the haversine formula,
22- which takes into account the spherical shape of the Earth. The input arrays `p1` and `p2` should contain
23- longitude and latitude coordinates in degrees. The function returns an array containing the distances
23+ This function computes the distance between two points on the Earth's surface
24+ using the haversine formula, which takes into account the spherical shape of the
25+ Earth. The input arrays `p1` and `p2` should contain longitude and latitude
26+ coordinates in degrees. The function returns an array containing the distances
2427 between corresponding pairs of points.
2528 """
2629 earth_radius = 6371 # km
@@ -41,7 +44,7 @@ def distance_on_sphere(p1, p2):
4144 return distances
4245
4346
44- """----------------------------------- Filtering Points----------------- -------------------------------"""
47+ """----------------------------Filtering Points-------------------------------"""
4548
4649
4750def filter_points (df , threshold_distance ):
@@ -56,10 +59,11 @@ def filter_points(df, threshold_distance):
5659 pandas.DataFrame: Filtered DataFrame containing selected points.
5760 float: Total road length calculated from the selected points.
5861
59- This function filters points from a DataFrame based on the given threshold distance. It calculates
60- distances between consecutive points and accumulates them until the accumulated distance surpasses
61- the threshold distance. It then selects those points and constructs a new DataFrame. Additionally,
62- it manually checks the last point to include it if it satisfies the length condition. The function
62+ This function filters points from a DataFrame based on the given threshold
63+ distance. It calculates distances between consecutive points and accumulates them
64+ until the accumulated distance surpasses the threshold distance. It then selects
65+ those points and constructs a new DataFrame. Additionally, it manually checks the
66+ last point to include it if it satisfies the length condition. The function
6367 returns the filtered DataFrame along with the calculated road length.
6468 """
6569 road_length = 0
@@ -83,7 +87,8 @@ def filter_points(df, threshold_distance):
8387 accumulated_distance = 0 # Reset accumulated distance
8488
8589 to_be_returned_df = df [mask ]
86- # since the last point has to be omitted in the vectorized distance calculation, it is being checked manually
90+ # since the last point has to be omitted in the vectorized distance calculation,
91+ # it is being checked manually
8792 p2 = to_be_returned_df .iloc [0 ]
8893 distance = distance_on_sphere (
8994 [float (p2 ["long" ]), float (p2 ["lat" ])], [long [- 1 ], lat [- 1 ]]
@@ -114,10 +119,10 @@ def spatial_sampling(df, interval_length):
114119 geopandas.GeoDataFrame: Filtered GeoDataFrame containing selected points.
115120 float: Total road length calculated from the selected points.
116121
117- This function calculates the spacing between points in a GeoDataFrame by filtering points
118- based on the provided interval length. It first sorts the GeoDataFrame by timestamp and
119- then filters points using the filter_points function. The function returns the filtered
120- GeoDataFrame along with the total road length.
122+ This function calculates the spacing between points in a GeoDataFrame by filtering
123+ points based on the provided interval length. It first sorts the GeoDataFrame by
124+ timestamp and then filters points using the filter_points function. The function
125+ returns the filtered GeoDataFrame along with the total road length.
121126 """
122127 if len (df ) == 1 :
123128 return df
0 commit comments