{ "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": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countrylatlon
0Côte d'Ivoire (Ivory Coast)7.989737-5.567946
1Italy42.63842612.674297
2USA39.783730-100.445882
3South Africa-28.81662424.991639
4Philippines12.750349122.731210
\n", "
" ], "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": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0countrylatlon
00Côte d'Ivoire (Ivory Coast)7.989737-5.567946
11Italy42.63842612.674297
22USA39.783730-100.445882
33South Africa-28.81662424.991639
44Philippines12.750349122.731210
\n", "
" ], "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": [ "" ], "text/plain": [ "" ] }, "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 }