INK, GREEN, GREY, AMBER = "#003A40", "#3E9A4B", "#7C8B8D", "#C4622D"
def lines(text, **params):
"""The lines of a query that returns ST_AsBinary(geom) AS wkb, ready for matplotlib."""
return [shapely.get_coordinates(g) for g in shapely.from_wkb(query(text, **params).wkb.map(bytes))]
STREETS = lines("SELECT ST_AsBinary(geom) AS wkb FROM road")
def street_map(ax, title, ids=None, also=(), margin=700):
"""Streets in light grey, the chosen stops as numbered dots (the start in green), and a 2 km scale bar.
The map frames the stops and the lines in `also`."""
chosen = stops if ids is None else stops[stops.id.isin(ids)]
frame = np.vstack([chosen[["x_m", "y_m"]], *also])
ax.add_collection(LineCollection(STREETS, colors="#C3CCCE", linewidths=0.45, zorder=1, rasterized=True))
for s in chosen[::-1].itertuples():
ax.plot(s.x_m, s.y_m, "o", ms=13, color=GREEN if s.id == 0 else INK, mec="white", mew=1, zorder=5)
ax.text(s.x_m, s.y_m, s.id, color="white", ha="center", va="center_baseline", fontsize=7.5, weight="bold", zorder=5)
ax.set_xlim(frame[:, 0].min() - margin, frame[:, 0].max() + margin)
ax.set_ylim(frame[:, 1].min() - margin, frame[:, 1].max() + margin)
x0, y0 = ax.get_xlim()[0] + 250, ax.get_ylim()[0] + 250
ax.plot([x0, x0 + 2000], [y0, y0], color=INK, lw=2, zorder=6)
ax.text(x0 + 1000, y0 + 90, "2 km", ha="center", fontsize=8.5, color=INK, zorder=6,
bbox={"facecolor": "white", "edgecolor": "none", "alpha": 0.8, "pad": 0.5})
ax.set_title(title, loc="left", fontsize=11, color=INK)
ax.set_aspect("equal")
ax.axis("off")
def finish(fig):
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.04, top=0.95, wspace=0.04)
fig.text(0.99, 0.01, "Data © OpenStreetMap contributors", ha="right", fontsize=7.5, color=GREY)
fig, ax = plt.subplots(figsize=(9, 7))
street_map(ax, "The street network, with one-way streets in green")
ax.add_collection(LineCollection(lines("SELECT ST_AsBinary(geom) AS wkb FROM road WHERE one_way"),
colors=GREEN, linewidths=1.1, zorder=2))
finish(fig)
plt.show()