{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipyleaflet import (\n", " Map,\n", " Marker,\n", " MarkerCluster,\n", " TileLayer,\n", " ImageOverlay,\n", " Polyline,\n", " Polygon,\n", " Rectangle,\n", " Circle,\n", " CircleMarker,\n", " Popup,\n", " GeoJSON,\n", " DrawControl,\n", " basemaps,\n", ")\n", "\n", "from ipywidgets import HTML" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "center = [34.6252978589571, -77.34580993652344]\n", "zoom = 10" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = Map(center=center, zoom=zoom)\n", "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.interact(zoom=(5, 10, 1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.clear_layers()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.add(basemaps.Esri.DeLorme)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(m.center)\n", "print(m.zoom)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Marker" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mark = Marker(location=m.center)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m += mark" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mark.interact(opacity=(0.0, 1.0, 0.01))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Popup" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "html_widget = HTML(\n", " value=\"\"\"\n", "
Some html with a list of items\n", "
\"\"\",\n", " placeholder=\"\",\n", " description=\"\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mark.popup = html_widget" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Marker Cluster" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we have many markers on a map, it is helpful to cluster them together at high zoom levels. First, we create a small grid of markers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xscale = 5\n", "yscale = 10\n", "\n", "x = [m.center[0] + i * xscale * 0.05 for i in (-1, 0, 1)]\n", "y = [m.center[1] + i * yscale * 0.05 for i in (-1, 0, 1)]\n", "\n", "from itertools import product\n", "\n", "locations = product(x, y)\n", "markers = [Marker(location=loc) for loc in locations]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we add them all to a `MarkerCluster`, which automatically clusters them at appropriate zoom levels." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "marker_cluster = MarkerCluster(markers=markers)\n", "m += marker_cluster" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.remove_layer(marker_cluster)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Image Overlay" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "io = ImageOverlay(url=\"http://ipython.org/_static/IPy_header.png\", bounds=m.bounds)\n", "m.add(io)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.bounds" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.remove_layer(io)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Polyline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pl = Polyline(locations=m.bounds_polygon)\n", "m += pl" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pl.fill_color = \"#F00\"\n", "pl.fill_opacity = 1.0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m -= pl" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Polygon" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pg = Polygon(\n", " locations=m.bounds_polygon,\n", " weight=3,\n", " color=\"#F00\",\n", " opacity=0.8,\n", " fill_opacity=0.8,\n", " fill_color=\"#0F0\",\n", ")\n", "m += pg" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m -= pg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rectangle" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = Rectangle(bounds=m.bounds, weight=10, fill_opacity=0.0)\n", "m += r" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m -= r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Circle" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = Circle(location=m.center)\n", "m.add(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c.interact(weight=(0, 10, 1), opacity=(0.0, 1.0, 0.01))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c.model_id" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.remove_layer(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.layers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c2 = Circle(\n", " location=m.center,\n", " radius=30,\n", " weight=1,\n", " color=\"#F00\",\n", " opacity=1.0,\n", " fill_opacity=1.0,\n", " fill_color=\"#0F0\",\n", ")\n", "m.add(c2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c2.model_id" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.remove_layer(c2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c2.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CircleMarker" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cm = CircleMarker(\n", " location=m.center,\n", " radius=30,\n", " weight=2,\n", " color=\"#F00\",\n", " opacity=1.0,\n", " fill_opacity=1.0,\n", " fill_color=\"#0F0\",\n", ")\n", "m.add(cm)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cm.model_id" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.remove_layer(cm)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cm.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiple Circles" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "circles = []\n", "for pos in m.bounds_polygon:\n", " c = Circle(location=pos, radius=1000)\n", " circles.append(c)\n", " m.add(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for c in circles:\n", " m.remove_layer(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.5" } }, "nbformat": 4, "nbformat_minor": 4 }