{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8.3+52.g2758dc7.dirty\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 track(s)\n", "1 segment(s)\n", "1027 point(s)\n" ] } ], "source": [ "import gpxpy\n", "import requests\n", "\n", "\n", "url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data'\n", "fname = f'{url}/2014_08_05_farol.gpx'\n", "gpx = gpxpy.parse(requests.get(fname).text)\n", "\n", "print('{} track(s)'.format(len(gpx.tracks)))\n", "track = gpx.tracks[0]\n", "\n", "print('{} segment(s)'.format(len(track.segments)))\n", "segment = track.segments[0]\n", "\n", "print('{} point(s)'.format(len(segment.points)))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "data = []\n", "segment_length = segment.length_3d()\n", "for point_idx, point in enumerate(segment.points):\n", " data.append(\n", " [\n", " point.longitude,\n", " point.latitude,\n", " point.elevation,\n", " point.time,\n", " segment.get_speed(point_idx)\n", " ]\n", " )" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LongitudeLatitudeAltitudeTimeSpeed
0-38.502595-13.00539010.92014-08-05 17:52:49.330000+00:00NaN
1-38.502605-13.00541511.82014-08-05 17:52:49.770000+00:002.677749
2-38.502575-13.00550711.72014-08-05 17:52:54.730000+00:003.065225
3-38.502545-13.00559511.62014-08-05 17:52:57.750000+00:004.228354
4-38.502515-13.00568011.42014-08-05 17:53:00.720000+00:003.947037
\n", "
" ], "text/plain": [ " Longitude Latitude Altitude Time Speed\n", "0 -38.502595 -13.005390 10.9 2014-08-05 17:52:49.330000+00:00 NaN\n", "1 -38.502605 -13.005415 11.8 2014-08-05 17:52:49.770000+00:00 2.677749\n", "2 -38.502575 -13.005507 11.7 2014-08-05 17:52:54.730000+00:00 3.065225\n", "3 -38.502545 -13.005595 11.6 2014-08-05 17:52:57.750000+00:00 4.228354\n", "4 -38.502515 -13.005680 11.4 2014-08-05 17:53:00.720000+00:00 3.947037" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pandas import DataFrame\n", "\n", "columns = ['Longitude', 'Latitude', 'Altitude', 'Time', 'Speed']\n", "df = DataFrame(data, columns=columns)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from geographiclib.geodesic import Geodesic\n", "\n", "angles = [90]\n", "for i in range(len(df) - 1):\n", " info = Geodesic.WGS84.Inverse(\n", " df.iloc[i, 1], df.iloc[i, 0],\n", " df.iloc[i + 1, 1], df.iloc[i + 1, 0]\n", " )\n", " angles.append(info['azi2'])\n", "\n", "# Change from CW-from-North to CCW-from-East.\n", "angles = np.deg2rad(450 - np.array(angles))\n", "\n", "# Normalize the speed to use as the length of the arrows.\n", "r = df['Speed'] / df['Speed'].max()\n", "df['u'] = r * np.cos(angles)\n", "df['v'] = r * np.sin(angles)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import mplleaflet\n", "import matplotlib.pyplot as plt\n", "\n", "fig, ax = plt.subplots()\n", "df = df.dropna()\n", "\n", "# This style was lost below.\n", "ax.plot(\n", " df['Longitude'],\n", " df['Latitude'],\n", " color='darkorange',\n", " linewidth=5,\n", " alpha=0.5\n", ")\n", "\n", "# This is preserved in the SVG icon.\n", "sub = 10\n", "kw = {'color': 'deepskyblue', 'alpha': 0.8, 'scale': 10}\n", "ax.quiver(df['Longitude'][::sub],\n", " df['Latitude'][::sub],\n", " df['u'][::sub],\n", " df['v'][::sub], **kw)\n", "\n", "gj = mplleaflet.fig_to_geojson(fig=fig)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import folium\n", "\n", "lon, lat = -38.51386097, -13.00868051\n", "zoom_start = 14\n", "\n", "m = folium.Map(\n", " location=[lat, lon],\n", " tiles='Cartodb Positron',\n", " zoom_start=zoom_start\n", ")\n", "\n", "# The first geometry is a lineString.\n", "line_string = gj['features'][0]\n", "gjson = folium.features.GeoJson(line_string)\n", "\n", "m.add_child(gjson)\n", "m.save(os.path.join('results', 'Folium_and_mplleaflet_0.html'))\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'color': '#FF8C00', 'weight': 5.0, 'opacity': 0.5}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line_string['properties']" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "This should be darkorange!" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import HTML\n", "\n", "msg = 'This should be darkorange!'.format\n", "HTML(msg(line_string['properties']['color']))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(\n", " location=[lat, lon],\n", " tiles='Cartodb Positron',\n", " zoom_start=zoom_start\n", ")\n", "\n", "icon_size = (14, 14)\n", "\n", "for feature in gj['features']:\n", " if feature['geometry']['type'] == 'LineString':\n", " continue\n", " elif feature['geometry']['type'] == 'Point':\n", " lon, lat = feature['geometry']['coordinates']\n", " html = feature['properties']['html']\n", "\n", " icon_anchor = (feature['properties']['anchor_x'],\n", " feature['properties']['anchor_y'])\n", "\n", " icon = folium.features.DivIcon(html=html,\n", " icon_size=(14, 14),\n", " icon_anchor=icon_anchor)\n", " marker = folium.map.Marker([lat, lon], icon=icon)\n", " m.add_child(marker)\n", " else:\n", " msg = 'Unexpected geometry {}'.format\n", " raise ValueError(msg(feature['geometry']))\n", "\n", "m.save(os.path.join('results', 'Folium_and_mplleaflet_1.html'))\n", "\n", "m" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 1 }