I have many GPS tracks (long, lat, alt, timestamp) from heavy trucks. Trucks are go on some paths. How can I extract the path from GPS tracks without deviation and convert it to one MultiLineString?

Raw Data

I found Cushen five steps advice based on PostGIS.

I have done steps 1 (get clusters) and 2 (get centers):


But I don't know how do step 3 "generate a line that connects each cluster centre point"?

How can I connect these points to a MultiLineString using GeoPandas?

  • With the timestamp you could add the points in order to a multilinestring? Sort the points in an array by timestamp and then in a for loop add to a linestring as described here gis.stackexchange.com/questions/95670/… Aug 21, 2021 at 21:47
  • I have multiple tracks with gps deviation on the same roads. The problem is to remove deviation. At first step I use KMeans clusterisation and loose timestamps.
    – Max Sh
    Aug 21, 2021 at 22:10
  • Please do not comment on your own question. Instead Edit the question to contain the clarification.
    – Vince
    Aug 21, 2021 at 23:15
  • You can surely keep the indexes of the source points after the clustering and join the timestamps again Aug 22, 2021 at 7:38
  • Can you share your data
    – BERA
    Aug 22, 2021 at 15:05

1 Answer 1


First, you need to have a column for the unique truck_id and a time variable. Sort the gdf by time so the sequence of the points is correct. Then you can use groupby and agg method to make a list of points for each route.

Then use apply and LineString for converting list of points to lines:

import geopandas as gpd
from shapely.geometry import LineString

gdf = gdf.sort_values('time').reset_index(dtopr=True)

gdf_grouped = gdf.groupby(['truck_id']).agg({'geometry':list})

gdf_grouped['geometry'] = gdf_grouped['geometry'].apply(lambda x: LineString(x))

gdf_grouped = gpd.GeoDataFrame(gdf_grouped)

If you are expecting any break for a truck (two routes with same truck_id) then you need to find a way to get a unique route_id instead.

Not the answer you're looking for? Browse other questions tagged or ask your own question.