#!/usr/bin/env python # coding: utf-8 # In[1]: import os import folium print(folium.__version__) # In[2]: import gpxpy fname = os.path.join('data', '2014_08_05_farol.gpx') gpx = gpxpy.parse(open(fname)) print('{} track(s)'.format(len(gpx.tracks))) track = gpx.tracks[0] print('{} segment(s)'.format(len(track.segments))) segment = track.segments[0] print('{} point(s)'.format(len(segment.points))) # In[3]: data = [] segment_length = segment.length_3d() for point_idx, point in enumerate(segment.points): data.append( [ point.longitude, point.latitude, point.elevation, point.time, segment.get_speed(point_idx) ] ) # In[4]: from pandas import DataFrame columns = ['Longitude', 'Latitude', 'Altitude', 'Time', 'Speed'] df = DataFrame(data, columns=columns) df.head() # In[5]: import numpy as np from geographiclib.geodesic import Geodesic angles = [90] for i in range(len(df) - 1): info = Geodesic.WGS84.Inverse( df.iloc[i, 1], df.iloc[i, 0], df.iloc[i + 1, 1], df.iloc[i + 1, 0] ) angles.append(info['azi2']) # Change from CW-from-North to CCW-from-East. angles = np.deg2rad(450 - np.array(angles)) # Normalize the speed to use as the length of the arrows. r = df['Speed'] / df['Speed'].max() df['u'] = r * np.cos(angles) df['v'] = r * np.sin(angles) # In[6]: import mplleaflet import matplotlib.pyplot as plt fig, ax = plt.subplots() df = df.dropna() # This style was lost below. ax.plot( df['Longitude'], df['Latitude'], color='darkorange', linewidth=5, alpha=0.5 ) # This is preserved in the SVG icon. sub = 10 kw = {'color': 'deepskyblue', 'alpha': 0.8, 'scale': 10} ax.quiver(df['Longitude'][::sub], df['Latitude'][::sub], df['u'][::sub], df['v'][::sub], **kw) gj = mplleaflet.fig_to_geojson(fig=fig) # In[7]: import folium lon, lat = -38.51386097, -13.00868051 zoom_start = 14 m = folium.Map( location=[lat, lon], tiles='Cartodb Positron', zoom_start=zoom_start ) # The first geometry is a lineString. line_string = gj['features'][0] gjson = folium.features.GeoJson(line_string) m.add_child(gjson) m.save(os.path.join('results', 'Folium_and_mplleaflet_0.html')) m # In[8]: line_string['properties'] # In[9]: from IPython.display import HTML msg = 'This should be darkorange!'.format HTML(msg(line_string['properties']['color'])) # In[10]: m = folium.Map( location=[lat, lon], tiles='Cartodb Positron', zoom_start=zoom_start ) icon_size = (14, 14) for feature in gj['features']: if feature['geometry']['type'] == 'LineString': continue elif feature['geometry']['type'] == 'Point': lon, lat = feature['geometry']['coordinates'] html = feature['properties']['html'] icon_anchor = (feature['properties']['anchor_x'], feature['properties']['anchor_y']) icon = folium.features.DivIcon(html=html, icon_size=(14, 14), icon_anchor=icon_anchor) marker = folium.map.Marker([lat, lon], icon=icon) m.add_child(marker) else: msg = 'Unexpected geometry {}'.format raise ValueError(msg(feature['geometry'])) m.save(os.path.join('results', 'Folium_and_mplleaflet_1.html')) m