{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Choose my Geolocator\n", "\n", "Given there's so many options on geolocator APIs, here I'm trying a few options.\n", "\n", "The goal is to pinpoint Wharton's [Huntsman Hall](http://www.facilities.upenn.edu/maps/locations/huntsman-hall-jon-m) from its address, and locate it on [Mapbox](https://www.mapbox.com/) service\n", "\n", "I now have this running at http://wtak23.github.io/plotly_mapbox.html\n", "\n", "**Table of Contents**\n", "\n", "
\n", "" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# get my mapbox token\n", "from os.path import expanduser\n", "\n", "with open(expanduser('~/mapbox-token-tak'), 'r') as f:\n", " mapbox_access_token = f.read()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Try to pinpoint Huntsman Hall\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# address of hunstman hall\n", "huntsman_address = '3730 Walnut St, Philadelphia, PA' " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Walnut Street, University City, Philadelphia, Philadelphia County, Pennsylvania, 19104, United States of America\n", "(39.9514112, -75.1808949)\n" ] } ], "source": [ "#https://wiki.openstreetmap.org/wiki/Nominatim\n", "# (turned out to be the most robust in my experience)\n", "from geopy.geocoders import Nominatim\n", "\n", "geolocator = Nominatim()\n", "location = geolocator.geocode(huntsman_address)\n", "print(location.address)\n", "print((location.latitude, location.longitude))\n", "lat = location.latitude\n", "lon = location.longitude\n", "\n", "# below threw an exception saying it cannot *locate* Huntsman..\n", "# from geopy.geocoders import GeocoderDotUS\n", "# geolocator = GeocoderDotUS(format_string=\"%s, Philadelphia PA\")\n", "# geolocator.geocode('241 South 49th Street')\n", "\n", "# geolocator = GeocoderDotUS(format_string=\"%s, Cleveland OH\")\n", "# address, (latitude, longitude) = geolocator.geocode(\"11111 Euclid Ave\")\n", "# print(address, latitude, longitude)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "from plotly.graph_objs import Data,Scattermapbox,Marker,Layout" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = Data([\n", " Scattermapbox(\n", " lat=[lat],\n", " lon=[lon],\n", " mode='markers',\n", " marker=Marker(\n", " size=14\n", " ),\n", " text=['Montreal'],\n", " )\n", "])\n", "layout = Layout(\n", " autosize=True,\n", " hovermode='closest',\n", " mapbox=dict(\n", " accesstoken=mapbox_access_token,\n", " bearing=0,\n", " center=dict(\n", " lat=lat,\n", " lon=lon\n", " ),\n", " pitch=0,\n", " zoom=15\n", " ),\n", ")\n", "\n", "fig1 = dict(data=data, layout=layout)\n", "py.iplot(fig1, filename='Huntsman-try1', validate=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hmmm...that's pretty off..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lemme try a different geolocator packages..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# URL Request using freegeopi" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(lat,lon) = (39.9597,-75.1968)\n" ] } ], "source": [ "import requests\n", "import json\n", "\n", "send_url = 'http://freegeoip.net/json'\n", "r = requests.get(send_url)\n", "j = json.loads(r.text)\n", "lat = j['latitude']\n", "lon = j['longitude']\n", "print \"(lat,lon) = ({},{})\".format(lat,lon)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = Data([\n", " Scattermapbox(\n", " lat=[lat],\n", " lon=[lon],\n", " mode='markers',\n", " marker=Marker(\n", " size=14\n", " ),\n", " text=['Huntman'],\n", " )\n", "])\n", "layout = Layout(\n", " autosize=True,\n", " hovermode='closest',\n", " mapbox=dict(\n", " accesstoken=mapbox_access_token,\n", " bearing=0,\n", " center=dict(\n", " lat=lat,\n", " lon=lon\n", " ),\n", " pitch=0,\n", " zoom=15\n", " ),\n", ")\n", "\n", "fig2 = dict(data=data, layout=layout)\n", "py.iplot(fig2, validate=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## That's also quite off....\n", "Now starting to wonder if it's the **geolocator** that is off or the **mapbox** app that is off..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Try geolocation-python\n", "https://github.com/slawek87/geolocation-python\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# %%bash\n", "# pip install geolocation-python --user" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n", "Philadelphia\n", "Walnut Street\n", "3730\n", "19104\n", "administrative_area_level_1: Pennsylvania (PA)\n", "administrative_area_level_2: Philadelphia County (Philadelphia County)\n", "United States\n", "US\n", "Jon M. Huntsman Hall, 3730 Walnut St, Philadelphia, PA 19104, USA\n", "39.9529683\n", "-75.1982049\n" ] } ], "source": [ "from geolocation.main import GoogleMaps\n", "\n", "# address = \"New York City Wall Street 12\"\n", "\n", "google_maps = GoogleMaps(api_key='') \n", "\n", "location = google_maps.search(location=huntsman_address) # sends search to Google Maps.\n", "\n", "print(location.all()) # returns all locations.\n", "\n", "my_location = location.first() # returns only first location.\n", "\n", "print(my_location.city)\n", "print(my_location.route)\n", "print(my_location.street_number)\n", "print(my_location.postal_code)\n", "\n", "for administrative_area in my_location.administrative_area:\n", " print(\"{}: {} ({})\".format(administrative_area.area_type, \n", " administrative_area.name, \n", " administrative_area.short_name))\n", "\n", "print(my_location.country)\n", "print(my_location.country_shortcut)\n", "\n", "print(my_location.formatted_address)\n", "\n", "print(my_location.lat)\n", "print(my_location.lng)\n", "\n", "# # reverse geocode\n", "# lat = 40.7060008\n", "# lng = -74.0088189\n", "# my_location = google_maps.search(lat=lat, lng=lng).first()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lat=my_location.lat\n", "lon=my_location.lng\n", "data = Data([\n", " Scattermapbox(\n", " lat=[lat],\n", " lon=[lon],\n", " mode='markers',\n", " marker=Marker(\n", " size=14\n", " ),\n", " text=['Huntsman Hall'],\n", " )\n", "])\n", "layout = Layout(\n", " autosize=True,\n", " hovermode='closest',\n", " mapbox=dict(\n", " accesstoken=mapbox_access_token,\n", " bearing=0,\n", " center=dict(\n", " lat=lat,\n", " lon=lon\n", " ),\n", " pitch=0,\n", " zoom=15\n", " ),\n", ")\n", "\n", "fig2 = dict(data=data, layout=layout)\n", "py.iplot(fig2, filename='Huntsman', validate=False)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Success!\n", "I guess i'll stick with `geolocation` package based on this trial run.\n", "Let's save the result in plotly" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "u'https://plot.ly/~takanori/64'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "py.plot(fig2, filename='Huntsman', validate=False)" ] } ], "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.12" } }, "nbformat": 4, "nbformat_minor": 0 }