Skip to content

Instantly share code, notes, and snippets.

@om-henners
Created September 6, 2013 00:58
Show Gist options
  • Save om-henners/6458233 to your computer and use it in GitHub Desktop.
Save om-henners/6458233 to your computer and use it in GitHub Desktop.
From a CSV which you can download from the [AEC](http://www.aec.gov.au/) generate a GeoJSON file containing locations of polling places in your electorate. See example output for the [Melbourne electoral division](http://www.aec.gov.au/profiles/vic/Melbourne.htm). Note, the script needs [Requests](http://docs.python-requests.org/en/latest/) to r…
"""
Where is my polling place?
==========================
Simple script to call the Google Geocoder service and get a map of polling
places. View the locations in Google Maps by converting the outout GeoJSON to
KML (Use ogr2ogr or `Jason Sanford's geojson-google-maps
<https://github.com/JasonSanford/geojson-google-maps>`_ utility) .Note that
the Google TOS require that the results of the geocode are used to be viewed
in google maps.
Built using `requests <http://docs.python-requests.org/en/latest/>`_ which you
should install if you want to use this.
"""
import csv
import argparse
import sys
import json
import requests
GEOCODER_URL = "http://maps.googleapis.com/maps/api/geocode/json"
def get_locn_candidates(path):
"""
.. function:: get_locn_candidates(path)
Generator for querying the Google Geocoding API and returning address candidate locations
:param path: Path to the CSV file from the AEC
"""
params = {
"region": "au",
"sensor": "false"
}
for row in csv.DictReader(open(path)):
params["address"] = "{Premises}, {Address}, {Suburb}, {State}".format(**row)
r = requests.get(GEOCODER_URL, params=params)
r.raise_for_status()
j = r.json()
if j["status"] == "OK":
yield j, row["Premises"]
elif j["status"] == "ZERO_RESULTS":
sys.stderr.write("Could not find a location for %s\r\n" % params["address"])
else:
raise requests.exceptions.HTTPError, "Google Geocoder returned status of %s" % j["status"]
def build_geojson(csv_path, geojson_path=None):
"""
.. function:: build_geojson(csv_path[, geojson_path=None)
Builds a geojson object from addresses.
:param csv_path: Path to the CSV file from the AEC
:param geojson_path: Path to geojson output. Will be overwritten.
"""
if geojson_path:
gj_out = open(geojson_path, 'wb')
else:
gj_out = sys.stdout
feature_collection = {
"type":"FeatureCollection",
"features":[]
}
for response, premises in get_locn_candidates(csv_path):
for result in response["results"]:
feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
result["geometry"]["location"]["lng"],
result["geometry"]["location"]["lat"]
]
},
"properties": {
"premises": premises,
"google_address": result["formatted_address"],
"accuracy": result["geometry"]["location_type"]
}
}
feature_collection["features"].append(feature)
json.dump(feature_collection, gj_out, indent=2)
if __name__ == "__main__":
args = argparse.ArgumentParser(description=__doc__)
args.add_argument("csv_path", help="Path to the CSV of polling places downloaded from the AEC")
args.add_argument("-g", "--geojson", help="Path to a file to write the geojson file to. Will be overwritten")
a = args.parse_args()
build_geojson(a.csv_path, a.geojson)
Display the source blob
Display the rendered blob
Raw
{
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "Point",
"coordinates": [
144.9257353,
-37.7741162
]
},
"type": "Feature",
"properties": {
"premises": "Ascot Vale Primary School",
"google_address": "Ascot Vale Primary School, Bank Street, Ascot Vale VIC 3032, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.910554,
-37.779932
]
},
"type": "Feature",
"properties": {
"premises": "Ascot Vale West Primary School",
"google_address": "8 Langs Road, Ascot Vale VIC 3032, Australia",
"accuracy": "ROOFTOP"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.972456,
-37.7787977
]
},
"type": "Feature",
"properties": {
"premises": "Brunswick South Primary School",
"google_address": "Brunswick South Primary School, 56 Brunswick Road, Brunswick East VIC 3057, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9695396,
-37.8019822
]
},
"type": "Feature",
"properties": {
"premises": "Carlton Gardens Primary School",
"google_address": "Carlton Gardens Primary, 215 Rathdowne Street, Carlton VIC 3053, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9722299,
-37.7911591
]
},
"type": "Feature",
"properties": {
"premises": "Carlton North Primary School",
"google_address": "Carlton North Primary School, Lee Street, Carlton North VIC 3054, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9685237,
-37.7998044
]
},
"type": "Feature",
"properties": {
"premises": "Carlton Primary School",
"google_address": "Drummond Street, Carlton VIC 3053, Australia",
"accuracy": "GEOMETRIC_CENTER"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.967331,
-37.8037764
]
},
"type": "Feature",
"properties": {
"premises": "Carlton Primary School",
"google_address": "Drummond Place, Carlton VIC 3053, Australia",
"accuracy": "GEOMETRIC_CENTER"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.989352,
-37.792509
]
},
"type": "Feature",
"properties": {
"premises": "Clifton Hill Primary School",
"google_address": "Clifton Hill Primary School, 185 Gold Street, Clifton Hill VIC 3068, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9899065,
-37.8033575
]
},
"type": "Feature",
"properties": {
"premises": "Collingwood College",
"google_address": "McCutcheon Way, Collingwood VIC 3066, Australia",
"accuracy": "GEOMETRIC_CENTER"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9935849,
-37.8038079
]
},
"type": "Feature",
"properties": {
"premises": "Collingwood Senior Citizens Centre",
"google_address": "Eddy Court, Abbotsford VIC 3067, Australia",
"accuracy": "GEOMETRIC_CENTER"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9963614,
-37.8115016
]
},
"type": "Feature",
"properties": {
"premises": "Community Hall 106",
"google_address": "106/106 Elizabeth Street, Richmond VIC 3121, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9985203,
-37.8117751
]
},
"type": "Feature",
"properties": {
"premises": "Community Hall 110",
"google_address": "110/110 Elizabeth Street, Richmond VIC 3121, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.981885,
-37.809572
]
},
"type": "Feature",
"properties": {
"premises": "Dallas Brooks Centre",
"google_address": "300 Albert Street, East Melbourne VIC 3002, Australia",
"accuracy": "ROOFTOP"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9882421,
-37.7843583
]
},
"type": "Feature",
"properties": {
"premises": "Fitzroy High School",
"google_address": "Falconer Street, Fitzroy North VIC 3068, Australia",
"accuracy": "GEOMETRIC_CENTER"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.983997,
-37.785165
]
},
"type": "Feature",
"properties": {
"premises": "Fitzroy North Primary School",
"google_address": "Fitzroy North Primary School, Fergie Street, Fitzroy North VIC 3068, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9813606,
-37.79946690000001
]
},
"type": "Feature",
"properties": {
"premises": "Fitzroy Primary School",
"google_address": "Fitzroy Primary School, 319 George Street, Fitzroy VIC 3065, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9287993,
-37.7952039
]
},
"type": "Feature",
"properties": {
"premises": "Holy Rosary School",
"google_address": "37 Gower Street, Kensington VIC 3031, Australia",
"accuracy": "RANGE_INTERPOLATED"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9266507,
-37.7880153
]
},
"type": "Feature",
"properties": {
"premises": "Kensington Community High School",
"google_address": "Kensington Community High School, 405 Racecourse Road, Kensington VIC 3031, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9270124,
-37.7926753
]
},
"type": "Feature",
"properties": {
"premises": "Kensington Primary School",
"google_address": "Kensington Primary School, 62-74 McCracken Street, Kensington VIC 3031, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9667679,
-37.8148203
]
},
"type": "Feature",
"properties": {
"premises": "Melbourne City Town Hall",
"google_address": "90-130 Swanston Street, Melbourne VIC 3000, Australia",
"accuracy": "ROOFTOP"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9334493,
-37.7821641
]
},
"type": "Feature",
"properties": {
"premises": "Mt Alexander College",
"google_address": "169 Mount Alexander Road, Travancore VIC 3032, Australia",
"accuracy": "RANGE_INTERPOLATED"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.950876,
-37.7983562
]
},
"type": "Feature",
"properties": {
"premises": "North Melbourne Primary School",
"google_address": "North Melbourne Primary School, 210 Errol Street, North Melbourne VIC 3051, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9682935,
-37.7803169
]
},
"type": "Feature",
"properties": {
"premises": "Princes Hill Primary School",
"google_address": "Princes Hill Primary School, Pigdon Street, Carlton North VIC 3054, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
145.0028419,
-37.8298967
]
},
"type": "Feature",
"properties": {
"premises": "Richmond Primary School",
"google_address": "Barkly Avenue, Richmond VIC 3121, Australia",
"accuracy": "GEOMETRIC_CENTER"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
145.0077592,
-37.8226048
]
},
"type": "Feature",
"properties": {
"premises": "Richmond Uniting Church",
"google_address": "271 Burnley Street, Richmond VIC 3121, Australia",
"accuracy": "RANGE_INTERPOLATED"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.997323,
-37.821621
]
},
"type": "Feature",
"properties": {
"premises": "St Ignatius'Church Hall",
"google_address": "St Ignatius Catholic Church, 326 Church Street, Richmond VIC 3121, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9547369,
-37.8035483
]
},
"type": "Feature",
"properties": {
"premises": "St Joseph's Flexible Learning Centre",
"google_address": "385 Queensberry Street, North Melbourne VIC 3051, Australia",
"accuracy": "RANGE_INTERPOLATED"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.981835,
-37.80161100000001
]
},
"type": "Feature",
"properties": {
"premises": "St Mark's Community Centre",
"google_address": "250 George Street, Fitzroy VIC 3065, Australia",
"accuracy": "ROOFTOP"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9114516,
-37.7744118
]
},
"type": "Feature",
"properties": {
"premises": "St Mary's School",
"google_address": "74 Roseberry Street, Ascot Vale VIC 3032, Australia",
"accuracy": "RANGE_INTERPOLATED"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9453989,
-37.7939358
]
},
"type": "Feature",
"properties": {
"premises": "St Michael's Catholic Primary School",
"google_address": "8 Brougham Street, North Melbourne VIC 3051, Australia",
"accuracy": "ROOFTOP"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.9460078,
-37.8150178
]
},
"type": "Feature",
"properties": {
"premises": "The Hub @ Docklands",
"google_address": "Docklands VIC, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.955199,
-37.797082
]
},
"type": "Feature",
"properties": {
"premises": "University High School",
"google_address": "The University High School, 77 Story Street, Parkville VIC 3052, Australia",
"accuracy": "APPROXIMATE"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
144.919517,
-37.779653
]
},
"type": "Feature",
"properties": {
"premises": "Wingate Community Centre",
"google_address": "13A Wingate Avenue, Ascot Vale VIC 3032, Australia",
"accuracy": "ROOFTOP"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
145.0077322,
-37.8145077
]
},
"type": "Feature",
"properties": {
"premises": "Yarra Primary School",
"google_address": "Davison Street, Richmond VIC 3121, Australia",
"accuracy": "GEOMETRIC_CENTER"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment