In this notebook, we'll look at two ways that areas in Chicago are collected into "regions" and "sides", and then save this collated data as shapefiles for use elsewhere.
In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import geopandas as gpd
import os
The file Chicago_Areas.geojson is downloaded from https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Community-Areas-current-/cauq-8yn6
In [11]:
filename = os.path.join("..", "..", "..", "..", "..", "Data", "Chicago_Areas.geojson")
frame = gpd.read_file(filename)
frame.crs = {"init": "epsg:4326"}
frame.head()
Out[11]:
In [12]:
fig, ax = plt.subplots(figsize=(10,14))
_ = frame.plot(ax=ax, column="community")
In [13]:
regions_mapping = {
"North" : [1,2,3,4,5,6,7,77],
"Northwest" : list(range(9,23)) + [76],
"West" : list(range(23,32)),
"Central" : [8, 32, 33],
"South" : [35, 36] + list(range(38, 49)) + [69],
"Southwest" : [34, 37] + list(range(56, 69)) + [70],
"FarSouth" : list(range(49, 56)) + list(range(71, 76))
}
In [14]:
def get_region(x):
return next( key for key, item in regions_mapping.items() if int(x) in item )
frame["region"] = frame.area_numbe.map(get_region)
In [15]:
_ = frame.plot(column="region", legend=True)
In [16]:
frame = frame.to_crs({"init":"epsg:3857"})
In [17]:
import tilemapbase
In [18]:
extent = tilemapbase.extent_from_frame(frame, 1000, 5)
fig, ax = plt.subplots(figsize=(14,15))
extent.plot(ax, tilemapbase.tiles.OSM, alpha=1)
frame.plot(ax=ax, column="region", alpha=0.5)
for _, row in frame.iterrows():
x, y = row.geometry.centroid.coords[0]
ax.text(x, y, row.area_numbe, horizontalalignment='center', verticalalignment='center')
ax.set_title("Chicago regions")
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
ax.set_aspect(1)
None
https://en.wikipedia.org/wiki/Community_areas_in_Chicago
Fortunately seems to have the same numbering!
In [19]:
side_mapping = {
"Far North" : [1,2,3,4,9,10,11,12,13,14,76,77],
"Northwest" : [15,16,17,18,19,20],
"North" : [5,6,7,21,22],
"West" : list(range(23, 32)),
"Central" : [8,32,33],
"South" : list(range(34,44)) + [60, 69],
"Southwest" : [56,57,58,59] + list(range(61,69)),
"Far Southwest" : list(range(70,76)),
"Far Southeast" : list(range(44,56))
}
In [20]:
frame["side"] = frame.area_numbe.map(lambda x : next(key
for key, item in side_mapping.items() if int(x) in item) )
In [21]:
fig, ax = plt.subplots(figsize=(14,15))
extent.plot(ax, tilemapbase.tiles.OSM, alpha=1)
frame.plot(ax=ax, column="side", alpha=0.5)
for _, row in frame.iterrows():
x, y = row.geometry.centroid.coords[0]
ax.text(x, y, row.area_numbe, horizontalalignment='center', verticalalignment='center')
ax.set_title("Chicago sides")
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
ax.set_aspect(1)
None