Skip to content

Instantly share code, notes, and snippets.

@joezuntz
Created November 20, 2012 14:12
Show Gist options
  • Save joezuntz/4118179 to your computer and use it in GitHub Desktop.
Save joezuntz/4118179 to your computer and use it in GitHub Desktop.
Python script to get lat/lon of all towns in England
#Needs Python Wikipedia Robot Framework
import wikipedia
import sys
p=wikipedia.Page('en','List_of_towns_in_England')
town_pages=p.linkedPages()[7:-9]
class LatLonError(ValueError):
pass
def infobox_latlon(info):
for param in info:
if param.startswith('latitude'):
lat = float(param.split('=')[1].strip())
elif param.startswith('longitude'):
lon = float(param.split('=')[1].strip())
try:
return lat,lon
except NameError:
raise LatLonError("Could not get Lat/Lon")
for town_page in town_pages:
if town_page.isRedirectPage():
town_page = town_page.getRedirectTarget()
templates = dict(town_page.templatesWithParams())
try:
place_info = templates['Infobox UK place']
lat, lon = infobox_latlon(place_info)
print lat, lon, town_page.titleForFilename()
except (LatLonError, KeyError):
sys.stderr.write('Could not parse %s\n' % town_page.titleForFilename())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment