{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3.0.dev\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 track(s)\n", "1 segment(s)\n", "1027 point(s)\n" ] } ], "source": [ "import gpxpy\n", "\n", "fname = os.path.join('data', '2014_08_05_farol.gpx')\n", "gpx = gpxpy.parse(open(fname))\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": { "collapsed": false }, "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": { "collapsed": false }, "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", "
LongitudeLatitudeAltitudeTimeSpeed
0-38.502595-13.00539010.92014-08-05 17:52:49.330NaN
1-38.502605-13.00541511.82014-08-05 17:52:49.7702.672951
2-38.502575-13.00550711.72014-08-05 17:52:54.7303.059732
3-38.502545-13.00559511.62014-08-05 17:52:57.7504.220779
4-38.502515-13.00568011.42014-08-05 17:53:00.7203.939967
\n", "
" ], "text/plain": [ " Longitude Latitude Altitude Time Speed\n", "0 -38.502595 -13.005390 10.9 2014-08-05 17:52:49.330 NaN\n", "1 -38.502605 -13.005415 11.8 2014-08-05 17:52:49.770 2.672951\n", "2 -38.502575 -13.005507 11.7 2014-08-05 17:52:54.730 3.059732\n", "3 -38.502545 -13.005595 11.6 2014-08-05 17:52:57.750 4.220779\n", "4 -38.502515 -13.005680 11.4 2014-08-05 17:53:00.720 3.939967" ] }, "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": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import seawater as sw\n", "\n", "_, angles = sw.dist(df['Latitude'], df['Longitude'])\n", "angles = np.r_[0, np.deg2rad(angles)]\n", "\n", "# Normalize the speed to use as the length of the arrows.\n", "r = df['Speed'] / df['Speed'].max()\n", "kw = dict(window_len=31, window='hanning')\n", "df['u'] = r * np.cos(angles)\n", "df['v'] = r * np.sin(angles)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "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 = dict(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": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/filipe/miniconda3/envs/FOLIUM/lib/python3.5/site-packages/ipykernel/__main__.py:15: FutureWarning: Method `add_children` is deprecated. Please use `add_child` instead.\n" ] }, { "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", "line_string = gj['features'][0] # The first geometry is a lineString.\n", "gjson = folium.features.GeoJson(line_string)\n", "\n", "m.add_children(gjson)\n", "m.save(os.path.join('results', 'Folium_and_mplleaflet_0.html'))\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'color': '#FF8C00',\n", " 'highlight': {},\n", " 'opacity': 0.5,\n", " 'style': {},\n", " 'weight': 5.0}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line_string['properties']" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "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": { "collapsed": false }, "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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }