diff --git "a/notebooks/folium_map.ipynb" "b/notebooks/folium_map.ipynb" new file mode 100644--- /dev/null +++ "b/notebooks/folium_map.ipynb" @@ -0,0 +1,350 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "eac62f3c-471e-4e97-8e0e-6fee3fa15f04", + "metadata": {}, + "outputs": [], + "source": [ + "import folium\n", + "import requests\n", + "import pandas" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "49e838aa-22bc-417a-b688-06f72aaa10df", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "world_map = folium.Map((30, 0), zoom_start=2, tiles=\"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryTopo/MapServer/tile/{z}/{y}/{x}\", \n", + " attr='Tiles courtesy of the U.S. Geological Survey')\n", + "\n", + "geojson_data = requests.get(\n", + " \"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/world_countries.json\"\n", + ").json()\n", + "\n", + "land_defenders_attacks = pandas.read_csv(\n", + " \"attacks_by_country_for_folium.csv\"\n", + ")\n", + "\n", + "land_defenders_map = folium.Choropleth(\n", + " name=\"Map of lethal attacks against land defenders\",\n", + " geo_data=geojson_data,\n", + " data=land_defenders_attacks,\n", + " columns=[\"country\", \"number_of_victims\"],\n", + " key_on=\"feature.properties.name\",\n", + " fill_color=\"YlOrRd\",\n", + " bins=[1, 25, 50, 100, 150, 200, 250, 300, 342], #bins=9,\n", + " highlight=True,\n", + " nan_fill_color=\"grey\",\n", + " fill_opacity=0.8,\n", + " line_opacity=0.6,\n", + " line_weight=0.5,\n", + " legend_name=\"Number of attacks\",\n", + ").add_to(world_map)\n", + "\n", + "land_defenders_indexed = land_defenders_attacks.set_index('country')\n", + "\n", + "for feature in land_defenders_map.geojson.data['features']:\n", + " try:\n", + " feature['properties']['number of victims'] = repr(land_defenders_indexed.loc[feature['properties']['name'], 'number_of_victims'])\n", + " except KeyError:\n", + " feature['properties']['number of victims'] = \"no data\"\n", + "\n", + "folium.GeoJsonTooltip(['name', 'number of victims']).add_to(land_defenders_map.geojson)\n", + " \n", + "folium.LayerControl().add_to(world_map)\n", + "\n", + "world_map" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "112259f5-cdfe-40d7-8909-0f0414984f96", + "metadata": {}, + "outputs": [], + "source": [ + "world_map.save(\"land-defenders-map.html\")" + ] + } + ], + "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.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}