Skip to content

Instantly share code, notes, and snippets.

@batemapf
Created February 22, 2018 21:22
Show Gist options
  • Save batemapf/e6e0114245226f3e5c2f3409cbb3bb44 to your computer and use it in GitHub Desktop.
Save batemapf/e6e0114245226f3e5c2f3409cbb3bb44 to your computer and use it in GitHub Desktop.
Philly Ward Mapper
import geojson
import geojsonio
import csv, os
# Requires mapshaper, a Node package. See https://github.com/mbloch/mapshaper/wiki/Introduction-to-the-Command-Line-Tool.
def load_map_data(filename):
with open(filename) as infile:
return geojson.loads(infile.read())
def display_ward(data, ward):
ward_data = [ d for d in data.features if d.properties['WARD_NUM'] == str(ward) ][0]
geojsonio.display(ward_data)
def display_map(data):
data = [ d for d in data.features ]
geojsonio.display(data)
def load_machine_data(filename):
with open(filename) as infile:
return [ r for r in csv.DictReader(infile) ]
def filter_machine_data(data, k, v):
return [ r for r in data if r[k] == v ]
def clean_map_data(data, wards):
data['features'] = [ d for d in data['features'] \
if str(d.properties['WARD_NUM']) in wards]
return data
def save_map_data(filename, geo_data):
with open(filename, 'w') as outfile:
return outfile.write(geojson.dumps(geo_data))
# Load map data.
data = load_map_data('Political_Wards.geojson')
# Load machine error data.
machine_data = load_machine_data('data.csv')
# Filter errors.
machine_data = filter_machine_data(
machine_data, 'REPAIRS MADE', 'MACHINE RESET')
# Gather wards.
wards = [ m['WARD'] for m in machine_data ]
# Clean map data based on wards.
data = clean_map_data(data, wards)
# Save cleaned data.
save_map_data('errors_only.geojson', data)
# Simplify geometery.
os.system("mapshaper errors_only.geojson -simplify 100% -o simply.geojson")
# Load simplified data.
data = load_map_data('simply.geojson')
# Display
display_map(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment