Created
January 29, 2014 10:34
-
-
Save m-vdb/8685384 to your computer and use it in GitHub Desktop.
Retrieve the french department coordinates using wikipedia and google geolocation API.
This file contains hidden or 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
from collections import OrderedDict | |
from itertools import tee | |
import time | |
from xml.etree import ElementTree as ET | |
from pygeocoder import Geocoder | |
import requests | |
wiki_url = u"http://fr.wikipedia.org/wiki/Liste_des_pr%C3%A9fectures_de_France" | |
def get_prefectures(): | |
resp = requests.get(wiki_url) | |
resp.raise_for_status() | |
tree = ET.fromstring(resp.content) | |
table = tree.find(".//center//table") | |
data = OrderedDict() | |
for row in table.iterfind(".//tr"): | |
if row.find("./td"): | |
code = row.find("./th").text | |
dep = row.find("./td[1]/a").text | |
pref = row.find("./td[2]/a").text | |
data["%s (%s)" % (dep, code)] = pref | |
return data | |
def get_coords(): # FYI, it'll work by batches, because we get kicked out by google API | |
data = get_prefectures() | |
items = data.items() | |
for dep, prefecture in items: | |
lat, lon = Geocoder.geocode("%s France" % prefecture)[0].coordinates | |
print '"%s": {"lat": %s, "lon": %s}' % (dep, lat, lon) | |
if __name__ == "__main__": | |
get_coords() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment