{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipyleaflet import (\n", " Map,\n", " Marker,\n", " TileLayer,\n", " ImageOverlay,\n", " Polyline,\n", " Polygon,\n", " Rectangle,\n", " Circle,\n", " CircleMarker,\n", " GeoJSON,\n", " DrawControl,\n", ")" ] }, { "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": [ "import matplotlib as mpl\n", "import matplotlib.cm\n", "import matplotlib.colors\n", "import numpy as np\n", "\n", "\n", "def n_colors(n, colormap=mpl.cm.Blues):\n", " data = np.linspace(0.0, 1.0, n)\n", " c = [mpl.colors.rgb2hex(d[0:3]) for d in colormap(data)]\n", " return c\n", "\n", "\n", "def data_to_colors(data, colormap=mpl.cm.Blues):\n", " c = [mpl.colors.rgb2hex(d[0:3]) for d in colormap(mpl.colors.Normalize()(data))]\n", " return c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "with open(\"demo.json\") as f:\n", " data = json.load(f)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n_features = len(data[\"features\"])\n", "colors = n_colors(n_features)\n", "print(n_features)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "colors" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for feature, color in zip(data[\"features\"], colors):\n", " feature[\"properties\"][\"style\"] = {\n", " \"color\": color,\n", " \"weight\": 1,\n", " \"fillColor\": color,\n", " \"fillOpacity\": 0.5,\n", " }" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data[\"features\"][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "g = GeoJSON(data=data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.add(g)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# m.remove_layer(g)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# g.close()" ] } ], "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 }