{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using GeoJSON Point Features with Markers" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "import os\n", "import folium\n", "import geopandas as gpd" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "rootpath = os.path.abspath(os.getcwd())" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "gdf = gpd.read_file(os.path.join(rootpath, \"data\", \"subwaystations.geojson\"))" ] }, { "cell_type": "code", "execution_count": 25, "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", " \n", " \n", " \n", " \n", " \n", " \n", "
nameurllineobjectidnotesgeometry
0Astor Plhttp://web.mta.info/nyct/service/4-6-6 Express14 nights, 6-all times, 6 Express-weekdays AM s...POINT (-73.99107 40.73005)
1Canal Sthttp://web.mta.info/nyct/service/4-6-6 Express24 nights, 6-all times, 6 Express-weekdays AM s...POINT (-74.00019 40.71880)
250th Sthttp://web.mta.info/nyct/service/1-231-all times, 2-nightsPOINT (-73.98385 40.76173)
3Bergen Sthttp://web.mta.info/nyct/service/2-3-444-nights, 3-all other times, 2-all timesPOINT (-73.97500 40.68086)
4Pennsylvania Avehttp://web.mta.info/nyct/service/3-454-nights, 3-all other timesPOINT (-73.89489 40.66471)
\n", "
" ], "text/plain": [ " name url line \\\n", "0 Astor Pl http://web.mta.info/nyct/service/ 4-6-6 Express \n", "1 Canal St http://web.mta.info/nyct/service/ 4-6-6 Express \n", "2 50th St http://web.mta.info/nyct/service/ 1-2 \n", "3 Bergen St http://web.mta.info/nyct/service/ 2-3-4 \n", "4 Pennsylvania Ave http://web.mta.info/nyct/service/ 3-4 \n", "\n", " objectid notes \\\n", "0 1 4 nights, 6-all times, 6 Express-weekdays AM s... \n", "1 2 4 nights, 6-all times, 6 Express-weekdays AM s... \n", "2 3 1-all times, 2-nights \n", "3 4 4-nights, 3-all other times, 2-all times \n", "4 5 4-nights, 3-all other times \n", "\n", " geometry \n", "0 POINT (-73.99107 40.73005) \n", "1 POINT (-74.00019 40.71880) \n", "2 POINT (-73.98385 40.76173) \n", "3 POINT (-73.97500 40.68086) \n", "4 POINT (-73.89489 40.66471) " ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gdf.head()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "gdf['href'] = '' + gdf.url + \"\"\n", "gdf['service_level'] = gdf.notes.str.split(', ').apply(lambda x: len([v for v in x if \"all\" in v]))\n", "gdf['lines_served'] = gdf.line.str.split('-').apply(lambda x: len(x))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 0]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "service_levels = gdf.service_level.unique().tolist()\n", "service_levels" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "colors = [\"orange\", \"yellow\", \"green\", \"blue\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Use a Circle as a Marker" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[40.75, -73.95], zoom_start=13)\n", "\n", "folium.GeoJson(\n", " gdf,\n", " name=\"Subway Stations\",\n", " marker=folium.Circle(radius=4, fill_color=\"orange\", fill_opacity=0.4, color=\"black\", weight=1),\n", " tooltip=folium.GeoJsonTooltip(fields=[\"name\", \"line\", \"notes\"]),\n", " popup=folium.GeoJsonPopup(fields=[\"name\", \"line\", \"href\", \"notes\"]),\n", " style_function=lambda x: {\n", " \"fillColor\": colors[x['properties']['service_level']],\n", " \"radius\": (x['properties']['lines_served'])*30,\n", " },\n", " highlight_function=lambda x: {\"fillOpacity\": 0.8},\n", " zoom_on_click=True,\n", ").add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Or use a DivIcon" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[40.75, -73.95], zoom_start=13)\n", "\n", "\n", "def style_function(feature):\n", " props = feature.get('properties')\n", " markup = f\"\"\"\n", " \n", "
\n", "
\n", "
\n", " {props.get('name')}\n", "
\n", "
\n", " \"\"\"\n", " return {\"html\": markup}\n", "\n", "\n", "folium.GeoJson(\n", " gdf,\n", " name=\"Subway Stations\",\n", " marker=folium.Marker(icon=folium.DivIcon()),\n", " tooltip=folium.GeoJsonTooltip(fields=[\"name\", \"line\", \"notes\"]),\n", " popup=folium.GeoJsonPopup(fields=[\"name\", \"line\", \"href\", \"notes\"]),\n", " style_function=style_function,\n", " zoom_on_click=True,\n", ").add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use a Marker" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[40.75, -73.95], zoom_start=13)\n", "\n", "marker_colors = [\"red\", \"orange\", \"green\", \"blue\"]\n", "\n", "folium.GeoJson(\n", " gdf,\n", " name=\"Subway Stations\",\n", " zoom_on_click=True,\n", " marker=folium.Marker(icon=folium.Icon(icon='star')),\n", " tooltip=folium.GeoJsonTooltip(fields=[\"name\", \"line\", \"notes\"]),\n", " popup=folium.GeoJsonPopup(fields=[\"name\", \"line\", \"href\", \"notes\"]),\n", " style_function=lambda x: {\n", " 'markerColor': marker_colors[x['properties']['service_level']],\n", " },\n", ").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.7.2" } }, "nbformat": 4, "nbformat_minor": 4 }