{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import folium\n", "import pandas as pd\n", "import json\n", "\n", "\n", "url = (\"https://raw.githubusercontent.com/python-visualization/\"\n", " \"folium/master/examples/data\")\n", "\n", "us_states = f\"{url}/us-states.json\"\n", "US_Unemployment_Oct2012 = f\"{url}/US_Unemployment_Oct2012.csv\"\n", "\n", "state_data = pd.read_csv(US_Unemployment_Oct2012)\n", "\n", "m = folium.Map(location=[48, -102], zoom_start=3)\n", "folium.Choropleth(\n", " geo_data=us_states,\n", " data=state_data,\n", " columns=[\"State\", \"Unemployment\"],\n", " key_on=\"feature.id\",\n", " fill_color=\"YlGn\",\n", " fill_opacity=0.7,\n", " line_opacity=0.2,\n", " legend_name=\"Unemployment Rate (%)\",\n", " highlight=True\n", ").add_to(m)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from folium.plugins import Draw\n", "\n", "\n", "m = folium.Map(\n", " location=[-27.23, -48.36],\n", " zoom_start=10,\n", " tiles=\"https://server.arcgisonline.com/ArcGIS/rest/services/Ocean_Basemap/MapServer/tile/{z}/{y}/{x}\",\n", "\tattr=\"Tiles © Esri — Sources: GEBCO, NOAA, CHS, OSU, UNH, CSUMB, National Geographic, DeLorme, NAVTEQ, and Esri\",\n", ")\n", "\n", "draw = Draw(export=True)\n", "draw.add_to(m)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from owslib.wms import WebMapService\n", "\n", "\n", "url = \"http://oos.soest.hawaii.edu/thredds/wms/hioos/satellite/dhw_5km\"\n", "\n", "web_map_services = WebMapService(url)\n", "\n", "layer = \"CRW_SST\"\n", "wms = web_map_services.contents[layer]\n", "\n", "name = wms.title\n", "\n", "lon = (wms.boundingBox[0] + wms.boundingBox[2]) / 2.\n", "lat = (wms.boundingBox[1] + wms.boundingBox[3]) / 2.\n", "center = lat, lon\n", "\n", "time_interval = \"{0}/{1}\".format(\n", " wms.timepositions[0].strip(),\n", " wms.timepositions[-1].strip()\n", ")\n", "style = \"boxfill/sst_36\"\n", "\n", "if style not in wms.styles:\n", " style = None" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(\n", " location=[lat, lon],\n", " zoom_start=5,\n", " control_scale=True,\n", " tiles=\"Stamen Toner\",\n", ")\n", "\n", "w = folium.raster_layers.WmsTileLayer(\n", " url=url,\n", " name=\"sea_surface_temperature\",\n", " styles=style,\n", " fmt=\"image/png\",\n", " transparent=True,\n", " layers=\"CRW_SST\",\n", " overlay=True,\n", ").add_to(m)\n", "\n", "time = folium.plugins.TimestampedWmsTileLayers(\n", " [w],\n", " period=\"PT1H\",\n", " time_interval=time_interval,\n", ")\n", "\n", "time.add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "df = pd.DataFrame(data=[[\"apple\", \"oranges\"], [\"other\", \"stuff\"]], columns=[\"cats\", \"dogs\"])\n", "\n", "m = folium.Map(\n", " [43, -100],\n", " zoom_start=4,\n", " tiles=\"cartodbpositron\",\n", ")\n", "\n", "html = df.to_html(\n", " classes=(\n", " \"table table-striped \"\n", " \"table-hover \"\n", " \"table-condensed \"\n", " \"table-responsive\"\n", " )\n", ")\n", "\n", "popup = folium.Popup(html)\n", "\n", "folium.Marker([30, -100], popup=popup).add_to(m)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.plugins.DualMap(\n", " location=(52.1, 5.1),\n", " tiles=None,\n", " zoom_start=8,\n", ")\n", "\n", "folium.TileLayer(\"Stamen Watercolor\").add_to(m.m1)\n", "folium.TileLayer(\"cartodbpositron\").add_to(m.m2)\n", "\n", "folium.LayerControl(collapsed=False).add_to(m)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lat = +38.89399\n", "lon = -77.03659\n", "zoom_start = 17\n", "\n", "m = folium.Map(\n", " tiles=\"Stamen Terrain\",\n", " location=[lat, lon],\n", " zoom_start=zoom_start\n", ")\n", "\n", "kw = {\n", " \"color\": \"red\",\n", " \"fill\": True,\n", " \"radius\": 20\n", "}\n", "\n", "folium.CircleMarker([38.89415, -77.03738], **kw).add_to(m)\n", "folium.CircleMarker([38.89415, -77.03578], **kw).add_to(m)\n", "\n", "\n", "locations = [\n", " [(38.893596444352134, -77.03814983367920), (38.893379333722040, -77.03792452812195)],\n", " [(38.893379333722040, -77.03792452812195), (38.893162222428310, -77.03761339187622)],\n", " [(38.893162222428310, -77.03761339187622), (38.893028615148424, -77.03731298446655)],\n", " [(38.893028615148424, -77.03731298446655), (38.892920059048464, -77.03691601753235)],\n", " [(38.892920059048464, -77.03691601753235), (38.892903358095296, -77.03637957572937)],\n", " [(38.892903358095296, -77.03637957572937), (38.893011914220770, -77.03592896461487)],\n", " [(38.893011914220770, -77.03592896461487), (38.893162222428310, -77.03549981117249)],\n", " [(38.893162222428310, -77.03549981117249), (38.893404384982480, -77.03514575958252)],\n", " [(38.893404384982480, -77.03514575958252), (38.893596444352134, -77.03496336936950)]\n", "]\n", "\n", "folium.PolyLine(\n", " locations=locations,\n", " color=\"orange\",\n", " weight=8,\n", " opacity=1,\n", " smooth_factor=0,\n", ").add_to(m)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from cartopy.io.shapereader import natural_earth\n", "\n", "import geopandas\n", "\n", "\n", "fname = natural_earth(\n", " name=\"admin_0_countries\",\n", " resolution=\"10m\",\n", " category=\"cultural\"\n", ")\n", "gdf = geopandas.read_file(fname)\n", "\n", "south_america = gdf[gdf[\"CONTINENT\"] == \"South America\"]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from shapely.ops import unary_union\n", "\n", "\n", "geometries = [\n", " country[\"geometry\"] for k, country in south_america.iterrows()\n", "]\n", "sa = unary_union(geometries)\n", "sa.boundary" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "fname = natural_earth(\n", " name=\"rivers_lake_centerlines\",\n", " resolution=\"10m\",\n", " category=\"physical\"\n", ")\n", "\n", "rivers = geopandas.read_file(fname)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Ajajú',\n", " 'Amapari',\n", " 'Amazonas',\n", " 'Apure',\n", " 'Araguaia',\n", " 'Aripuanã',\n", " 'Atrato',\n", " 'Beni',\n", " 'Bermejo',\n", " 'Bois',\n", " 'Braco Menor',\n", " 'Branco',\n", " 'BÃ\\xado-BÃ\\xado',\n", " 'Caquetá',\n", " 'Carcarañá',\n", " 'CaronÃ\\xad',\n", " 'Casiquiare',\n", " 'Cauca',\n", " 'Chico',\n", " 'Chubut',\n", " 'Coari',\n", " 'Coco',\n", " 'Cojedes',\n", " 'Colorado',\n", " 'Contas',\n", " 'Copiapó',\n", " 'Corantijn',\n", " 'Corumbá',\n", " 'Courantyne',\n", " 'Culuene',\n", " 'Cuyuni',\n", " 'Desaguadero',\n", " 'Deseado',\n", " 'Doce',\n", " 'Dulce',\n", " 'Essequibo',\n", " 'Gallegos',\n", " 'Grande',\n", " 'GuainÃ\\xada',\n", " 'Guaporé',\n", " 'Gurupi',\n", " 'Guárico',\n", " 'Huallaga',\n", " 'IbicuÃ\\xad',\n", " 'Indaiá',\n", " 'Iriri',\n", " 'Itiquira',\n", " 'Ituxi',\n", " 'IvaÃ\\xad',\n", " 'Ivinheima',\n", " 'Jamanxim',\n", " 'Japurá',\n", " 'Jari',\n", " 'Javari',\n", " 'JejuÃ\\xad Guazú',\n", " 'Jequitinhonha',\n", " 'Jiparaná',\n", " 'Juruena',\n", " 'Juruá',\n", " 'JutaÃ\\xad',\n", " 'Kabalebo',\n", " 'Lempa',\n", " 'Limay',\n", " 'Litani',\n", " 'Madeira',\n", " 'Madre de Dios',\n", " 'Magdalena',\n", " 'Maicuru',\n", " 'Mamoré',\n", " 'Marañón',\n", " 'Maroni',\n", " 'Mearim',\n", " 'Mendoza',\n", " 'Meta',\n", " 'Mico',\n", " 'Mira',\n", " 'Miranda',\n", " 'Napo',\n", " 'Negro',\n", " 'Neuquén',\n", " 'Nhamundá',\n", " None,\n", " 'Ocoña',\n", " 'Orinoco',\n", " 'Panama Canal',\n", " 'Paraiba',\n", " 'Paranapanema',\n", " 'ParanaÃ\\xadba',\n", " 'Paraná',\n", " 'Pardo',\n", " 'ParnaÃ\\xadba',\n", " 'PiauÃ\\xad',\n", " 'Pilcomayo',\n", " 'Pisco',\n", " 'Purús',\n", " 'Putumayo',\n", " 'Ribeira',\n", " 'Rio Negro',\n", " 'Rio Rapel',\n", " 'Rio das Mortes',\n", " 'RÃ\\xado Chirripó del Atlántico',\n", " 'RÃ\\xado Grande de Matagalpa',\n", " 'RÃ\\xado Negro',\n", " 'Salado',\n", " 'San Juan',\n", " 'San Martin',\n", " 'San Miguel',\n", " 'Santa',\n", " 'Santa Cruz',\n", " 'Santa Maria',\n", " 'Santiago',\n", " 'Suriname',\n", " 'São Francisco',\n", " 'Tambo',\n", " 'Tapajós',\n", " 'Taquari',\n", " 'Teles Pires',\n", " 'Tercero',\n", " 'Tietê',\n", " 'Tocantins',\n", " 'Trombetas',\n", " 'Tumbes',\n", " 'Ucayali',\n", " 'Una',\n", " 'Unini',\n", " 'Uruguay',\n", " 'Vaupés',\n", " 'Velhas',\n", " 'Ventuari',\n", " 'Verde',\n", " 'Xingu',\n", " 'Yapacani'}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from shapely.geometry import box\n", "\n", "sasq = box(*sa.bounds)\n", "\n", "mask = []\n", "for k, river in rivers.iterrows():\n", " geom = river[\"geometry\"]\n", " if geom:\n", " ans = sasq.contains(geom)\n", " else:\n", " ans = False\n", " mask.append(ans)\n", " \n", "\n", "sa_rivers = rivers[mask]\n", "\n", "# I ? unicode!\n", "set(sa_rivers[\"name\"])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "include = {\n", " 2: \"Amazonas\",\n", " 78: \"Negro\",\n", " 91: \"Madeira\",\n", " 99: \"Paraná\",\n", " 100: \"Araguaia\",\n", " 104: \"São Francisco\",\n", " 108: \"Paranaíba\",\n", " 115: \"Tocantins\",\n", " 197: \"Xingu\",\n", " 200: \"Tapajós\",\n", " 284: \"Jequitinhonha\",\n", " 293: \"Uruguay\",\n", " 335: \"Tocantins\",\n", " 400: \"Paranapanema\",\n", " 436: \"Parnaíba\",\n", " 515: \"Doce\",\n", " 688: \"Tietê\",\n", "}" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "for rivernum, name in include.items():\n", " rivers.loc[rivers[\"rivernum\"] == rivernum, \"name\"] = name" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def invert(coords):\n", " if hasattr(coords, '__iter__'):\n", " if hasattr(coords[0], '__iter__'):\n", " return list(map(invert, coords))\n", " else:\n", " return list(coords[::-1])\n", " else:\n", " return coords\n", "\n", "\n", "def polyline(geom):\n", " line = unary_union(geom)\n", " if isinstance(geom, MultiLineString):\n", " line = linemerge(line)\n", " coords = line.__geo_interface__['coordinates']\n", " return invert(coords)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from shapely.ops import unary_union, linemerge\n", "from shapely.geometry import MultiLineString\n", "from shapely.geometry import LineString\n", "\n", "import folium\n", "from folium.plugins import Fullscreen\n", "from folium.plugins import AntPath\n", "\n", "\n", "url = \"https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png\"\n", "attr = \"Map data: © OpenStreetMap, SRTM | Map style: © OpenTopoMap (CC-BY-SA)\"\n", "\n", "m = folium.Map(tiles=url, attr=attr)\n", "Fullscreen(position=\"topright\", force_separate_button=True).add_to(m)\n", "\n", "for name in set(include.values()):\n", " geom = rivers.loc[rivers[\"name\"] == name, \"geometry\"].squeeze()\n", " if name in [\"Amazonas\", \"Tocantins\"]:\n", " reverse = True\n", " else:\n", " reverse = False\n", " AntPath(\n", " locations=polyline(geom),\n", " tooltip=f\"{name}\",\n", " reverse=reverse,\n", " ).add_to(m)\n", " \n", "\n", "m.fit_bounds(m.get_bounds())\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.3" } }, "nbformat": 4, "nbformat_minor": 1 }