{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**Note** : The examples presented below are the copy of the ones presented on https://github.com/bbecquet/Leaflet.PolylineOffset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Demo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- The dashed line is the \"model\", with no offset applied. \n", "- The Red line is with a -5px offset,\n", "- The Green line is with a 10px offset.\n", "The three are distinct Polyline objects but uses the same coordinate array" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import folium\n", "from folium import plugins\n", "\n", "\n", "m = folium.Map(location=[58.0, -11.0], zoom_start=4, tiles=\"cartodbpositron\")\n", "\n", "coords = [\n", " [58.44773, -28.65234],\n", " [53, -23.33496],\n", " [53, -14.32617],\n", " [58.1707, -10.37109],\n", " [59, -13],\n", " [57, -15],\n", " [57, -18],\n", " [60, -18],\n", " [63, -5],\n", " [59, -7],\n", " [58, -3],\n", " [56, -3],\n", " [60, -4],\n", "]\n", "\n", "plugins.PolyLineOffset(\n", " coords, weight=2, dash_array=\"5,10\", color=\"black\", opacity=1\n", ").add_to(m)\n", "\n", "plugins.PolyLineOffset(coords, color=\"#f00\", opacity=1, offset=-5).add_to(m)\n", "\n", "plugins.PolyLineOffset(coords, color=\"#080\", opacity=1, offset=10).add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bus Lines" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A more complex demo. \n", "Offsets are computed automatically depending on the number of bus lines using the same segment.\n", "Other non-offset polylines are used to achieve the white and black outline effect." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[48.868, 2.365], zoom_start=15)\n", "\n", "geojson = {\n", " \"type\": \"FeatureCollection\",\n", " \"features\": [\n", " {\n", " \"type\": \"Feature\",\n", " \"properties\": {\"lines\": [0, 1]},\n", " \"geometry\": {\n", " \"type\": \"LineString\",\n", " \"coordinates\": [\n", " [2.357919216156006, 48.87621773324153],\n", " [2.357339859008789, 48.874834693731664],\n", " [2.362983226776123, 48.86855408432749],\n", " [2.362382411956787, 48.86796126699168],\n", " [2.3633265495300293, 48.86735432768131],\n", " ],\n", " },\n", " },\n", " {\n", " \"type\": \"Feature\",\n", " \"properties\": {\"lines\": [2, 3]},\n", " \"geometry\": {\n", " \"type\": \"LineString\",\n", " \"coordinates\": [\n", " [2.351503372192383, 48.86443950493823],\n", " [2.361609935760498, 48.866775611250205],\n", " [2.3633265495300293, 48.86735432768131],\n", " ],\n", " },\n", " },\n", " {\n", " \"type\": \"Feature\",\n", " \"properties\": {\"lines\": [1, 2]},\n", " \"geometry\": {\n", " \"type\": \"LineString\",\n", " \"coordinates\": [\n", " [2.369627058506012, 48.86619159489603],\n", " [2.3724031448364253, 48.8626397112042],\n", " [2.3728322982788086, 48.8616233285001],\n", " [2.372767925262451, 48.86080456075567],\n", " ],\n", " },\n", " },\n", " {\n", " \"type\": \"Feature\",\n", " \"properties\": {\"lines\": [0]},\n", " \"geometry\": {\n", " \"type\": \"LineString\",\n", " \"coordinates\": [\n", " [2.3647427558898926, 48.86653565369396],\n", " [2.3647642135620117, 48.86630981023694],\n", " [2.3666739463806152, 48.86314789481612],\n", " [2.3673176765441895, 48.86066339254944],\n", " ],\n", " },\n", " },\n", " {\n", " \"type\": \"Feature\",\n", " \"properties\": {\"lines\": [0, 1, 2, 3]},\n", " \"geometry\": {\n", " \"type\": \"LineString\",\n", " \"coordinates\": [\n", " [2.3633265495300293, 48.86735432768131],\n", " [2.3647427558898926, 48.86653565369396],\n", " ],\n", " },\n", " },\n", " {\n", " \"type\": \"Feature\",\n", " \"properties\": {\"lines\": [1, 2, 3]},\n", " \"geometry\": {\n", " \"type\": \"LineString\",\n", " \"coordinates\": [\n", " [2.3647427558898926, 48.86653565369396],\n", " [2.3650002479553223, 48.86660622956524],\n", " [2.365509867668152, 48.866987337550164],\n", " [2.369627058506012, 48.86619159489603],\n", " ],\n", " },\n", " },\n", " {\n", " \"type\": \"Feature\",\n", " \"properties\": {\"lines\": [3]},\n", " \"geometry\": {\n", " \"type\": \"LineString\",\n", " \"coordinates\": [\n", " [2.369627058506012, 48.86619159489603],\n", " [2.372349500656128, 48.865702850895744],\n", " ],\n", " },\n", " },\n", " ],\n", "}\n", "\n", "# manage overlays in groups to ease superposition order\n", "outlines = folium.FeatureGroup(\"outlines\")\n", "line_bg = folium.FeatureGroup(\"lineBg\")\n", "bus_lines = folium.FeatureGroup(\"busLines\")\n", "bus_stops = folium.FeatureGroup(\"busStops\")\n", "\n", "line_weight = 6\n", "line_colors = [\"red\", \"#08f\", \"#0c0\", \"#f80\"]\n", "stops = []\n", "for line_segment in geojson[\"features\"]:\n", " # Get every bus line coordinates\n", " segment_coords = [[x[1], x[0]] for x in line_segment[\"geometry\"][\"coordinates\"]]\n", " # Get bus stops coordinates\n", " stops.append(segment_coords[0])\n", " stops.append(segment_coords[-1])\n", " # Get number of bus lines sharing the same coordinates\n", " lines_on_segment = line_segment[\"properties\"][\"lines\"]\n", " # Width of segment proportional to the number of bus lines\n", " segment_width = len(lines_on_segment) * (line_weight + 1)\n", " # For the white and black outline effect\n", " folium.PolyLine(\n", " segment_coords, color=\"#000\", weight=segment_width + 5, opacity=1\n", " ).add_to(outlines)\n", " folium.PolyLine(\n", " segment_coords, color=\"#fff\", weight=segment_width + 3, opacity=1\n", " ).add_to(line_bg)\n", " # Draw parallel bus lines with different color and offset\n", " for j, line_number in enumerate(lines_on_segment):\n", " plugins.PolyLineOffset(\n", " segment_coords,\n", " color=line_colors[line_number],\n", " weight=line_weight,\n", " opacity=1,\n", " offset=j * (line_weight + 1) - (segment_width / 2) + ((line_weight + 1) / 2),\n", " ).add_to(bus_lines)\n", "\n", "# Draw bus stops\n", "for stop in stops:\n", " folium.CircleMarker(\n", " stop,\n", " color=\"#000\",\n", " fill_color=\"#ccc\",\n", " fill_opacity=1,\n", " radius=10,\n", " weight=4,\n", " opacity=1,\n", " ).add_to(bus_stops)\n", "\n", "outlines.add_to(m)\n", "line_bg.add_to(m)\n", "bus_lines.add_to(m)\n", "bus_stops.add_to(m)\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.9.0" } }, "nbformat": 4, "nbformat_minor": 1 }