{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3.0+162.g0540622.dirty\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "\n", "df = pd.DataFrame.from_csv(\n", " os.path.join('data', 'highlight_flight_trajectories.csv')\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us take a glance at the data.\n", "Each row represents the trajectory of a flight,\n", "and the last column contains the coordinates of the flight path in `GeoJSON` format." ] }, { "cell_type": "code", "execution_count": 3, "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", "
depdestgeojson
0Place_MontpellierMediterranee_AirportPlace_BastiaPoretta_Airport{\"type\": \"LineString\", \"coordinates\": [[3.9613...
1Place_Bristol___LulsgatePlace_TenerifeSur_ReinaSofia_Airport{\"type\": \"LineString\", \"coordinates\": [[-2.719...
2Place_Valencia_Manises_AirportPlace_Bucuresti_HenriCoanda_Airport{\"type\": \"LineString\", \"coordinates\": [[-0.481...
\n", "
" ], "text/plain": [ " dep \\\n", "0 Place_MontpellierMediterranee_Airport \n", "1 Place_Bristol___Lulsgate \n", "2 Place_Valencia_Manises_Airport \n", "\n", " dest \\\n", "0 Place_BastiaPoretta_Airport \n", "1 Place_TenerifeSur_ReinaSofia_Airport \n", "2 Place_Bucuresti_HenriCoanda_Airport \n", "\n", " geojson \n", "0 {\"type\": \"LineString\", \"coordinates\": [[3.9613... \n", "1 {\"type\": \"LineString\", \"coordinates\": [[-2.719... \n", "2 {\"type\": \"LineString\", \"coordinates\": [[-0.481... " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(\n", " location=[40, 10],\n", " zoom_start=4,\n", " control_scale=True,\n", " prefer_canvas=True\n", ")\n", "\n", "\n", "def style_function(feature):\n", " return {\n", " 'fillColor': '#ffaf00',\n", " 'color': 'blue',\n", " 'weight': 1.5,\n", " 'dashArray': '5, 5'\n", " }\n", "\n", "\n", "def highlight_function(feature):\n", " return {\n", " 'fillColor': '#ffaf00',\n", " 'color': 'green',\n", " 'weight': 3,\n", " 'dashArray': '5, 5'\n", " }\n", "\n", "\n", "for index, row in df.iterrows():\n", " c = folium.GeoJson(\n", " row['geojson'],\n", " name=('{}{}'.format(row['dep'], row['dest'])),\n", " overlay=False,\n", " style_function=style_function,\n", " highlight_function=highlight_function\n", " )\n", " folium.Popup('{}\\n{}'.format(row['dep'], row['dest'])).add_to(c)\n", " c.add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "m.save(os.path.join('results', 'Highlight_Function.html'))\n", "\n", "m" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 2 }