Last active
November 27, 2015 11:58
-
-
Save revox/bf19d438aff6ff3b63e6 to your computer and use it in GitHub Desktop.
Scrape KML name ans boundaries out of mapitmysociety suitable for import to fusion tables. Currently set to scrape wards from the LB of Tower Hamlets
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
import scraperwiki | |
import simplejson | |
import urllib2 | |
import csv | |
from lxml import etree | |
# the borough key from mapitmysociety URL for the borough page | |
TYP = 'LBW' | |
KEY = '2506' | |
# typs are: | |
'''CTY (county council), CED (county ward), COI (Isles of Scilly), | |
COP (Isles of Scilly parish), CPC (civil parish/community), | |
CPW (civil parish/community ward), DIS (district council), | |
DIW (district ward), EUR (Euro region), GLA (London Assembly), | |
LAC (London Assembly constituency), LBO (London borough), | |
LBW (London ward), LGD (NI council), LGE (NI electoral area), | |
LGW (NI ward), MTD (Metropolitan district), MTW (Metropolitan ward), | |
NIE (NI Assembly constituency), OLF (Lower Layer Super Output Area, Full), | |
OLG (Lower Layer Super Output Area, Generalised), | |
OMF (Middle Layer Super Output Area, Full), | |
OMG (Middle Layer Super Output Area, Generalised), | |
SPC (Scottish Parliament constituency), | |
SPE (Scottish Parliament region), UTA (Unitary authority), | |
UTE (Unitary authority electoral division), UTW (Unitary authority ward), | |
WAC (Welsh Assembly constituency), WAE (Welsh Assembly region), | |
WMC (UK Parliamentary constituency)''' | |
csvfile = open(str(KEY) + 'ward_data.csv', 'w') | |
csvwriter = csv.writer(csvfile) | |
csvwriter.writerow(['name','geomtery']) | |
#Get a stub KML file for the local council level | |
url='http://mapit.mysociety.org/area/'+str(KEY)+'.kml' | |
xmlraw = urllib2.urlopen(url).read() | |
xml=etree.fromstring(xmlraw) | |
#Get the list of electoral wards covered by that council area | |
wards=simplejson.load(urllib2.urlopen('http://mapit.mysociety.org/area/'+str(KEY)+'/covers?type='+TYP)) | |
#Get the name and Polgon for each ward, stick them in a CSV | |
for ward in wards: | |
url='http://mapit.mysociety.org/area/'+ward+'.kml' | |
xmlraw = scraperwiki.scrape(url) | |
xml2=etree.fromstring(xmlraw) | |
# print etree.tostring(xml2) | |
names = xml2.xpath('//kml:name', namespaces={'kml': "http://www.opengis.net/kml/2.2"}) | |
poly = xml2.xpath('//kml:Polygon', namespaces={'kml': "http://www.opengis.net/kml/2.2"}) | |
print names[0].text | |
csvwriter.writerow([names[0].text,etree.tostring(poly[0])]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment