{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Geocoding with geopy and plotting with geoplotlib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Geopy: https://github.com/geopy/geopy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Geocode with geoPy:\n", "from geopy.geocoders import Nominatim" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "geolocator = Nominatim()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Testing the geocode function:__" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "location = geolocator.geocode(\"London\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "London, Greater London, England, United Kingdom\n" ] } ], "source": [ "print location.address" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(51.5073219, -0.1276473)\n" ] } ], "source": [ "print(location.latitude, location.longitude)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Now use on the countries of interest:__" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "countries = [u\"C\\xf4te d'Ivoire (Ivory Coast)\", u'Italy', u'USA', \n", " u'South Africa', u'Philippines', u'Democratic Republic of the Congo', \n", " u'Gabon', u'Sudan (South Sudan)', u'Uganda', u'England', u'Russia']" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "locations = [geolocator.geocode(c) for c in countries]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-5.5679458, 12.674297, -100.4458825, 24.991639, 122.7312101, 23.8222636, 11.6899699, 29.6667897, 32.2166578, -0.540240236617432, 97.7453061]\n" ] } ], "source": [ "longitudes = [l.longitude for l in locations]\n", "\n", "print longitudes" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[7.9897371, 42.6384261, 39.7837304, -28.8166236, 12.7503486, -2.9814344, -0.8999695, 7.8699431, 1.5333554, 52.7954791, 64.6863136]\n" ] } ], "source": [ "latitudes = [l.latitude for l in locations]\n", "\n", "print latitudes" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'lat': [7.9897371, 42.6384261, 39.7837304, -28.8166236, 12.7503486, -2.9814344, -0.8999695, 7.8699431, 1.5333554, 52.7954791, 64.6863136], 'country': [u\"C\\xf4te d'Ivoire (Ivory Coast)\", u'Italy', u'USA', u'South Africa', u'Philippines', u'Democratic Republic of the Congo', u'Gabon', u'Sudan (South Sudan)', u'Uganda', u'England', u'Russia'], 'lon': [-5.5679458, 12.674297, -100.4458825, 24.991639, 122.7312101, 23.8222636, 11.6899699, 29.6667897, 32.2166578, -0.540240236617432, 97.7453061]}\n" ] } ], "source": [ "dict_countries = {\"country\": countries, \"lat\": latitudes, \"lon\": longitudes}\n", "\n", "print dict_countries" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df_countries = pd.DataFrame(dict_countries)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "<div>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>country</th>\n", " <th>lat</th>\n", " <th>lon</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>Côte d'Ivoire (Ivory Coast)</td>\n", " <td>7.989737</td>\n", " <td>-5.567946</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>Italy</td>\n", " <td>42.638426</td>\n", " <td>12.674297</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>USA</td>\n", " <td>39.783730</td>\n", " <td>-100.445882</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>South Africa</td>\n", " <td>-28.816624</td>\n", " <td>24.991639</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>Philippines</td>\n", " <td>12.750349</td>\n", " <td>122.731210</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " country lat lon\n", "0 Côte d'Ivoire (Ivory Coast) 7.989737 -5.567946\n", "1 Italy 42.638426 12.674297\n", "2 USA 39.783730 -100.445882\n", "3 South Africa -28.816624 24.991639\n", "4 Philippines 12.750349 122.731210" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_countries.head()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df_countries.to_csv(\"data/out/countries_ebola.csv\", encoding=\"utf-8\", index_col=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Geoplotlib https://github.com/andrea-cuttone/geoplotlib" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Plot with geoplotlib\n", "import geoplotlib\n", "\n", "from geoplotlib.utils import read_csv" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DataAccessObject(['', 'country', 'lat', 'lon'] x 11)\n" ] } ], "source": [ "data_eb = read_csv(\"data/out/countries_ebola.csv\")\n", "\n", "print data_eb" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [], "source": [ "geoplotlib.dot(data_eb)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [], "source": [ "geoplotlib.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Folium https://github.com/python-visualization/folium" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Plot with Folium\n", "import folium\n", "\n", "from pandas import read_csv" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "<div>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>Unnamed: 0</th>\n", " <th>country</th>\n", " <th>lat</th>\n", " <th>lon</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>0</td>\n", " <td>Côte d'Ivoire (Ivory Coast)</td>\n", " <td>7.989737</td>\n", " <td>-5.567946</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>1</td>\n", " <td>Italy</td>\n", " <td>42.638426</td>\n", " <td>12.674297</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>2</td>\n", " <td>USA</td>\n", " <td>39.783730</td>\n", " <td>-100.445882</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>3</td>\n", " <td>South Africa</td>\n", " <td>-28.816624</td>\n", " <td>24.991639</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>4</td>\n", " <td>Philippines</td>\n", " <td>12.750349</td>\n", " <td>122.731210</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " Unnamed: 0 country lat lon\n", "0 0 Côte d'Ivoire (Ivory Coast) 7.989737 -5.567946\n", "1 1 Italy 42.638426 12.674297\n", "2 2 USA 39.783730 -100.445882\n", "3 3 South Africa -28.816624 24.991639\n", "4 4 Philippines 12.750349 122.731210" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "countries_ebola = read_csv(\"data/out/countries_ebola.csv\")\n", "\n", "countries_ebola.head()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [], "source": [ "map_eb = folium.Map(zoom_start=2, location=[40.486037, -24.090964])\n", "\n", "for i in range(len(countries_ebola)):\n", " map_eb.simple_marker([countries_ebola['lat'][i], countries_ebola['lon'][i]],\n", " popup=countries_ebola['country'][i], marker_color='green')" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "<iframe srcdoc=\"<!DOCTYPE html>\n", "<head>\n", " <meta http-equiv="content-type" content="text/html; charset=UTF-8" />\n", " <link rel="stylesheet" href="https://rawgit.com/andrewgiessel/leafletstuff/master/leaflet.css" />\n", " <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>\n", "\n", " <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>\n", "\n", " <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">\n", " <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">\n", " <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>\n", "\n", " <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">\n", "\n", " <link rel="stylesheet" href="https://rawgit.com/lvoogdt/Leaflet.awesome-markers/2.0/develop/dist/leaflet.awesome-markers.css">\n", " <script src="https://rawgithub.com/lvoogdt/Leaflet.awesome-markers/2.0/develop/dist/leaflet.awesome-markers.js"></script>\n", "\n", "\n", " <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.Default.css">\n", " <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.css">\n", " <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster-src.js"></script>\n", " <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster.js"></script>\n", "\n", " <link rel="stylesheet" href="https://birdage.github.io/Leaflet.awesome-markers/dist/leaflet.awesome.rotate.css">\n", "\n", " \n", " \n", " \n", " \n", "\n", " <style>\n", "\n", " html, body {\n", " width: 100%;\n", " height: 100%;\n", " margin: 0;\n", " padding: 0;\n", " }\n", "\n", " #map {\n", " position:absolute;\n", " top:0;\n", " bottom:0;\n", " right:0;\n", " left:0;\n", " }\n", "\n", " </style>\n", "</head>\n", "\n", "<body>\n", "\n", " <div class="folium-map" id="folium_8fd6f1659caf479b8752b6019e79d6aa" style="width: 100%; height: 100%"></div>\n", "\n", " <script>\n", "\n", " \n", "\n", " var base_tile = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {\n", " maxZoom: 18,\n", " minZoom: 1,\n", " attribution: 'Map data (c) <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'\n", " });\n", "\n", " var baseLayer = {\n", " "Base Layer": base_tile\n", " };\n", "\n", " /*\n", " addition of the wms layers\n", " */\n", "\n", " \n", "\n", " /*\n", " addition of the tile layers\n", " */\n", " \n", "\n", " /*\n", " list of layers to be added\n", " */\n", " var layer_list = {\n", " \n", " };\n", "\n", " /*\n", " Bounding box.\n", " */\n", " var southWest = L.latLng(-90, -180),\n", " northEast = L.latLng(90, 180),\n", " bounds = L.latLngBounds(southWest, northEast);\n", "\n", " /*\n", " Creates the map and adds the selected layers\n", " */\n", " var map = L.map('folium_8fd6f1659caf479b8752b6019e79d6aa', {\n", " center:[40.486037, -24.090964],\n", " zoom: 2,\n", " maxBounds: bounds,\n", " layers: [base_tile]\n", " });\n", "\n", " L.control.layers(baseLayer, layer_list).addTo(map);\n", "\n", " /*\n", " addition of the image layers\n", " */\n", " \n", " \n", " //cluster group\n", " var clusteredmarkers = L.markerClusterGroup();\n", " //section for adding clustered markers\n", " \n", " //add the clustered markers to the group anyway\n", " map.addLayer(clusteredmarkers);\n", "\n", " \n", " var marker_1_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_1 = L.marker([7.9897371, \n", "\t\t\t\t\t\t\t-5.5679458],\n", "\t\t\t\t\t\t\t{'icon':marker_1_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_1.bindPopup("C\\u00f4te d'Ivoire (Ivory Coast)");\n", " marker_1._popup.options.maxWidth = 300;\n", " map.addLayer(marker_1)\n", " \n", " var marker_2_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_2 = L.marker([42.6384261, \n", "\t\t\t\t\t\t\t12.674297],\n", "\t\t\t\t\t\t\t{'icon':marker_2_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_2.bindPopup("Italy");\n", " marker_2._popup.options.maxWidth = 300;\n", " map.addLayer(marker_2)\n", " \n", " var marker_3_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_3 = L.marker([39.7837304, \n", "\t\t\t\t\t\t\t-100.4458825],\n", "\t\t\t\t\t\t\t{'icon':marker_3_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_3.bindPopup("USA");\n", " marker_3._popup.options.maxWidth = 300;\n", " map.addLayer(marker_3)\n", " \n", " var marker_4_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_4 = L.marker([-28.8166236, \n", "\t\t\t\t\t\t\t24.991639],\n", "\t\t\t\t\t\t\t{'icon':marker_4_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_4.bindPopup("South Africa");\n", " marker_4._popup.options.maxWidth = 300;\n", " map.addLayer(marker_4)\n", " \n", " var marker_5_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_5 = L.marker([12.7503486, \n", "\t\t\t\t\t\t\t122.7312101],\n", "\t\t\t\t\t\t\t{'icon':marker_5_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_5.bindPopup("Philippines");\n", " marker_5._popup.options.maxWidth = 300;\n", " map.addLayer(marker_5)\n", " \n", " var marker_6_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_6 = L.marker([-2.9814344, \n", "\t\t\t\t\t\t\t23.8222636],\n", "\t\t\t\t\t\t\t{'icon':marker_6_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_6.bindPopup("Democratic Republic of the Congo");\n", " marker_6._popup.options.maxWidth = 300;\n", " map.addLayer(marker_6)\n", " \n", " var marker_7_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_7 = L.marker([-0.8999695, \n", "\t\t\t\t\t\t\t11.6899699],\n", "\t\t\t\t\t\t\t{'icon':marker_7_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_7.bindPopup("Gabon");\n", " marker_7._popup.options.maxWidth = 300;\n", " map.addLayer(marker_7)\n", " \n", " var marker_8_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_8 = L.marker([7.8699431, \n", "\t\t\t\t\t\t\t29.6667897],\n", "\t\t\t\t\t\t\t{'icon':marker_8_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_8.bindPopup("Sudan (South Sudan)");\n", " marker_8._popup.options.maxWidth = 300;\n", " map.addLayer(marker_8)\n", " \n", " var marker_9_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_9 = L.marker([1.5333554, \n", "\t\t\t\t\t\t\t32.2166578],\n", "\t\t\t\t\t\t\t{'icon':marker_9_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_9.bindPopup("Uganda");\n", " marker_9._popup.options.maxWidth = 300;\n", " map.addLayer(marker_9)\n", " \n", " var marker_10_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_10 = L.marker([52.7954791, \n", "\t\t\t\t\t\t\t-0.540240236617],\n", "\t\t\t\t\t\t\t{'icon':marker_10_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_10.bindPopup("England");\n", " marker_10._popup.options.maxWidth = 300;\n", " map.addLayer(marker_10)\n", " \n", " var marker_11_icon = L.AwesomeMarkers.icon({ icon: 'info-sign',markerColor: 'green',prefix: 'glyphicon',extraClasses: 'fa-rotate-0'});\n", " var marker_11 = L.marker([64.6863136, \n", "\t\t\t\t\t\t\t97.7453061],\n", "\t\t\t\t\t\t\t{'icon':marker_11_icon}\n", "\t\t\t\t\t\t\t);\n", " marker_11.bindPopup("Russia");\n", " marker_11._popup.options.maxWidth = 300;\n", " map.addLayer(marker_11)\n", " \n", "\n", " \n", "\n", " \n", "\n", " \n", "\n", " \n", "\n", " \n", "\n", "\n", "\n", " </script>\n", "\n", "</body>\" style=\"width: 100%; height: 500px; border: none\"></iframe>" ], "text/plain": [ "<folium.folium.Map at 0x7fd4e1bc9790>" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map_eb" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [], "source": [ "map_eb.create_map(path='data/out/ebola_countries.html')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }