Skip to content

Commit

Permalink
folium plot workshop
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuel-schmid committed Oct 24, 2023
1 parent e30c3a6 commit f4f20da
Show file tree
Hide file tree
Showing 2 changed files with 500 additions and 0 deletions.
273 changes: 273 additions & 0 deletions climada-days/2023-10/Plotting Climada Data with Folium.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "b650547e-4c47-4df0-a3a7-047fefeceacc",
"metadata": {},
"source": [
"# Interactive maps with folium\n",
"\n",
"- home: [Folium](https://python-visualization.github.io/folium/latest/index.html)\n",
"- based on [Leaflet](https://leafletjs.com/)\n",
"- pretty cool library for creating interactive html/java-script maps from python"
]
},
{
"cell_type": "markdown",
"id": "1a44b15a-e7e1-4b5c-9b85-d36ca5d38627",
"metadata": {},
"source": [
"## Installation\n",
"\n",
"```bash\n",
"pip install folium\n",
"```\n",
"\n",
"## Basics"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16ca28c3-c6bb-4f53-a2a0-893369ff040c",
"metadata": {},
"outputs": [],
"source": [
"import folium\n",
"[x for x in dir(folium) if not x.startswith('_')]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c45a5d3b-2599-40af-aa3b-e22bd341477b",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# A map of the London Area (initially)\n",
"latlon = (51.5, 0.0)\n",
"m = folium.Map(location=latlon)\n",
"\n",
"# show coordingates\n",
"#folium.LatLngPopup().add_to(m)\n",
"\n",
"# a Marker\n",
"#folium.Marker(location=(51.3, -0.4), popup='just an ordinary marker').add_to(m)\n",
"\n",
"# a Circle with size in meters\n",
"#folium.Circle(location=(51.7, -0.4), radius=2000, color='red').add_to(m)\n",
"\n",
"# also a Circle but size in pixels\n",
"#folium.CircleMarker(location=(51.7, +0.4), radius=20, color='green').add_to(m)\n",
"\n",
"# a Polygon\n",
"#folium.Polygon(locations=[(51.31, 0.42),(51.31, 0.39),(51.29, 0.38),(51.29, 0.41)], fillColor='#3186cc', fillOpacity=0.4).add_to(m)\n",
"\n",
"# change the Tiles\n",
"#folium.TileLayer('stamenterrain').add_to(m)\n",
"\n",
"# switch between Tiles\n",
"#folium.LayerControl().add_to(m)\n",
"\n",
"m"
]
},
{
"cell_type": "markdown",
"id": "e0fef59f-1808-43a6-a616-d592a3a24957",
"metadata": {},
"source": [
"## Plotting Climada Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee4af038-dc2d-4433-9853-943cad93a888",
"metadata": {},
"outputs": [],
"source": [
"from climada.util.api_client import Client\n",
"c = Client()\n",
"\n",
"uk_lp_30 = c.get_litpop(country='GBR', exponents=(3,0))\n",
"uk_lp_01 = c.get_litpop(country='GBR', exponents=(0,1))\n",
"uk_lp_11 = c.get_litpop(country='GBR', exponents=(1,1))\n",
"\n",
"uk_lp_11.gdf.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90923796-76ca-4389-990a-a25704d2279c",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"folium.GeoJson?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d427d2ae-25e0-411d-ae8e-4a86181fa271",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"latlon = (51.5, 0.0)\n",
"m = folium.Map(location=latlon)\n",
"maxv = uk_lp_30.gdf.value.max()\n",
"folium.GeoJson(\n",
" data=uk_lp_30.gdf[\n",
" uk_lp_30.gdf.latitude.between(51.3, 51.7)\n",
" & uk_lp_30.gdf.longitude.between(-0.4, 0.4)\n",
" ].loc[:,['geometry', 'value']],\n",
" #marker=folium.Circle(radius=1500, fillColor='blue'),\n",
" #style_function=lambda lp: {\n",
" # 'opacity': 0,\n",
" # 'fillColor': 'blue',\n",
" # 'fillOpacity': 0.6 * lp['properties']['value']/maxv,\n",
" #},\n",
" #popup=folium.GeoJsonPopup(fields=['value']),\n",
").add_to(m)\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2f18d1c-c304-4094-925e-f35d529da3c7",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from shapely import Polygon\n",
"latlon = (51.5, 0.0)\n",
"m = folium.Map(location=latlon)\n",
"maxv = uk_lp_30.gdf.value.max()\n",
"\n",
"def raster_polygon(lat, lon, arc_res):\n",
" return Polygon([\n",
" (lon+arc_res/2, lat+arc_res/2),\n",
" (lon+arc_res/2, lat-arc_res/2),\n",
" (lon-arc_res/2, lat-arc_res/2),\n",
" (lon-arc_res/2, lat+arc_res/2)])\n",
" \n",
"data = uk_lp_30.gdf[\n",
" uk_lp_30.gdf.latitude.between(51.3, 51.7)\n",
" & uk_lp_30.gdf.longitude.between(-0.4, 0.4)\n",
" ]\n",
"\n",
"data['geometry'] = data.apply(lambda x: raster_polygon(x.latitude, x.longitude, 150/3600), axis=1)\n",
"folium.GeoJson(\n",
" data=data,\n",
" popup=folium.GeoJsonPopup(fields=['value']),\n",
" style_function=lambda lp: {\n",
" 'opacity': 0,\n",
" 'fillColor': 'blue',\n",
" 'fillOpacity':0.6 * lp['properties']['value']/maxv,\n",
" }\n",
").add_to(m)\n",
"m"
]
},
{
"cell_type": "markdown",
"id": "5a51710f-42ae-46ca-8b25-3b698ea4f6b4",
"metadata": {},
"source": [
"## Annotating large areas\n",
"\n",
"`folium.Choropleth` is better suited for large areas then `folium.GeoJson` and provides a sophisticated interface for plotting choropleth maps."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e4d32459-9ea3-4d68-9609-c9d26f0fe3a5",
"metadata": {},
"outputs": [],
"source": [
"folium.Choropleth?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "caac0f3b-f975-4a5b-acf2-994c20889f50",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from climada_folium import exposures_folium, raster_polygons\n",
"\n",
"latlon = (51.5, 0.0)\n",
"m = folium.Map(location=latlon)\n",
"\n",
"exposures_folium(\n",
" exposures=uk_lp_30,\n",
" polygon_maker=raster_polygons(150/3600),\n",
" add_to_map=m\n",
")\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41a1e692-f27e-4eaf-b120-11ed3591db82",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"exposures_folium(\n",
" exposures=uk_lp_01,\n",
" polygon_maker=raster_polygons(150/3600),\n",
" fill_color='YlGn',\n",
" add_to_map=m\n",
")\n",
"\n",
"exposures_folium(\n",
" exposures=uk_lp_11,\n",
" polygon_maker=raster_polygons(150/3600),\n",
" fill_color='OrRd',\n",
" add_to_map=m\n",
")\n",
"\n",
"folium.LayerControl().add_to(m)\n",
"\n",
"m"
]
}
],
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit f4f20da

Please sign in to comment.