Skip to content

Instantly share code, notes, and snippets.

@andrewxhill
Created Sep 18, 2015
Embed
What would you like to do?
Use yahoo string geolocation api and drop results into cartodb
#!/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