Created
February 22, 2018 21:22
-
-
Save batemapf/e6e0114245226f3e5c2f3409cbb3bb44 to your computer and use it in GitHub Desktop.
Philly Ward Mapper
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 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