Created
September 18, 2015 17:59
-
-
Save andrewxhill/5eb6cc421c2a6f173c15 to your computer and use it in GitHub Desktop.
Use yahoo string geolocation api and drop results into cartodb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import string | |
import sys | |
import urllib | |
import urllib2 | |
#Uses Yahoo Placemaker and the python-placemaker library | |
from placemaker import placemaker | |
CARTODB_API_KEY = 'api-key-here' | |
CARTODB_ACCOUNT = 'account-name-here' | |
CARTODB_TABLE = 'table-name-here' | |
YAHOO_API_KEY = 'api-key-here' | |
LINE_FILE = 'pg3200.txt' | |
p = placemaker(YAHOO_API_KEY) | |
base_sql = "INSERT INTO "+CARTODB_TABLE+" (name, woeid, placetype, the_geom, line, data) VALUES " | |
base_url = "https://%s.cartodb.com/api/v1/sql" % CARTODB_ACCOUNT | |
cur = 1 | |
fails = open("log.txt", "a") | |
for line in open(LINE_FILE): | |
if len(line.strip()) > 1: | |
try: | |
p.find_places(line.strip()) | |
for place in p.places: | |
sql = "('%s', %i, '%s', CDB_LatLng(%f, %f), %i, '%s', '%s')" % (place.name.replace("'", "''"), place.woeid, place.placetype.replace("'", "''"), place.centroid.latitude, place.centroid.longitude, cur, source, line.strip()) | |
params = { | |
'api_key' : CARTODB_API_KEY, | |
'q' : base_sql + sql | |
} | |
# create your HTTP request | |
try: | |
req = urllib2.Request(base_url, urllib.urlencode(params)) | |
response = urllib2.urlopen(req) | |
except: | |
# print 'geocoder failed on ', cur | |
# print base_sql + sql | |
# print '' | |
fails.write(str(cur) + ',') | |
pass | |
except: | |
fails.write(str(cur) + ',') | |
pass | |
cur+=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment