lighthouse/plotter.py
2025-08-17 22:41:27 -04:00

93 lines
2.6 KiB
Python

import pandas as pd
import gpxpy
import folium
from glob import glob
def load_data_records():
df = pd.read_csv("./records.csv")
#df = df[(df.latitude != pd.NA) & (df.longitude != pd.NA)]
df = df.dropna(subset=['latitude', 'longitude'])
df.timestamp = pd.to_datetime(df.timestamp, format="%Y-%m-%d %H:%M:%S %Z")
print(df)
return df
def load_data_gpx():
lats = []
longs = []
timestamps = []
courses = []
speeds = []
gpx_files = sorted(glob("./gpx_files/*.gpx"))
for file in gpx_files:
print(f" + Loading GPX file {file}")
data = gpxpy.parse(open(file,"r"))
for track in data.tracks:
for segment in track.segments:
for p in segment.points:
lats.append(p.latitude)
longs.append(p.longitude)
timestamps.append(p.time)
courses.append(p.course)
speeds.append(p.speed)
df = pd.DataFrame({"latitude":lats,
"longitude":longs,
"course":courses,
"speed":speeds})
return df
def find_stops(df):
"""Get groups of consecutive positions unchanged for 4+ hours"""
stops = []
lc_row = df.iloc[0]
current_group = [0]
for i in range(1,len(df)):
row = df.iloc[i]
if (row.latitude == lc_row.latitude) and (row.longitude == lc_row.longitude):
current_group.append(i)
#print(f"added position {i} to the current group")
else:
#print(f"position changed")
if len(current_group) > 1:
delta = df.iloc[current_group[-1]].timestamp - df.iloc[current_group[0]].timestamp
#print(delta)
if delta > pd.Timedelta(value=4, unit="h"):
stops.append(current_group)
#print(f"added current group to stops")
current_group = [i]
lc_row = df.iloc[i]
return stops
def plot_map(df):
decimation = 1
m = folium.Map(location=(df.latitude.iloc[0], df.longitude.iloc[0]),
zoom_start=12,
tiles='OpenStreetMap')
folium.Marker([df.latitude.iloc[0], df.longitude.iloc[0]],
popup='Mouillage',
icon=folium.Icon(icon='anchor',prefix="fa")).add_to(m)
folium.PolyLine(
locations=df[['latitude', 'longitude']].values[::decimation].tolist(),
color='blue',
weight=3,
opacity=0.8
).add_to(m)
m.save('my_map.html')
df = load_data_gpx()
plot_map(df)