Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Federal Electoral Districts in Waterloo Region
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from functools import reduce
import json
import fiona
import pyproj
shp = "zip://"
# fio info zip:// reports "EPSG:3347"
# "EPSG:4269" for geojson?
transformer = pyproj.Transformer.from_crs(crs_from="EPSG:3347", crs_to="EPSG:4269")
waterloo_district_ids = (35016, 35045, 35046, 35047, 35112)
def reducer(value, function):
"""Use with reduce() to apply a sequence of functions to a value"""
return function(value)
def district_id(feature):
"""Get FEDNUM property for a district"""
return feature.get("properties", {}).get("FEDNUM", 0)
def coordinates(feature):
"""Get (Polygon) coordinates for a district"""
return feature.get("geometry", {}).get("coordinates", [[(0, 0)]])
def transform(feature, transformer=transformer):
"""Transform each point in a Polygon"""
feature["geometry"]["coordinates"] = [
[transformer.transform(x, y) for x, y in ring]
for ring in coordinates(feature)
return feature
def swap_xy(feature):
"""Swap x/y coordinates; use after transform()"""
feature["geometry"]["coordinates"] = [
[(y, x, *z) for x, y, *z in ring] for ring in coordinates(feature)
return feature
def geojson(features):
"""Build a GeoJSON object"""
return json.dumps(dict(type="FeatureCollection", features=features))
if __name__ == "__main__":
waterloo_districts = []
with fiona.Env():
with as features:
for feature in features:
# filter for local districts
if district_id(feature) in waterloo_district_ids:
waterloo_feature = reduce(reducer, (transform, swap_xy), feature)
# create a local copy
with open("waterloo_districts.geojson", "w") as f:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.