INK, GREEN, GREY = "#003A40", "#3E9A4B", "#7C8B8D"
# Where a name goes if not to the right of its dot: shift in points, and alignment
LABEL = {1: (-11, 0, "right"), 3: (4, -14, "right"), 4: (3, -14, "left"), 7: (3, 13, "left"),
9: (-11, 0, "right"), 12: (10, -14, "right"), 14: (-11, 0, "right")}
# Metres to move a kecamatan name that would lie under a cafe
MOVE = {"Coblong": (-100, -900), "Gedebage": (-200, -800), "Sumur Bandung": (900, 100), "Lengkong": (150, 700),
"Kiaracondong": (0, 900), "Sukajadi": (200, -700), "Bandung Wetan": (-500, 250), "Batununggal": (0, -1000),
"Bandung Kulon": (1500, -50), "Sukasari": (350, -400), "Ujungberung": (50, -600), "Cicendo": (-500, 0), "Cibeunying Kaler": (350, -350)}
HALO = [withStroke(linewidth=2.5, foreground="white")]
borders = query("""
SELECT name, regency, ST_AsBinary(geom) AS shape,
ST_X(ST_PointOnSurface(geom)) AS x, ST_Y(ST_PointOnSurface(geom)) AS y
FROM kecamatan""")
borders["shape"] = shapely.from_wkb(borders["shape"].map(bytes))
city = shapely.union_all(borders["shape"][borders.regency == "Kota Bandung"])
def base_map(title):
"""The stops as numbered dots, the start in green, over the kecamatan borders, with a 2 km scale bar."""
fig, ax = plt.subplots(figsize=(9, 6.9))
ax.set_facecolor("#F1F3F3") # outside the city
for part in shapely.get_parts(city):
ax.fill(*part.exterior.xy, color="white", zorder=0)
for shape in shapely.get_parts(borders["shape"]):
ax.plot(*shape.exterior.xy, color="#CCD4D5", lw=0.8, zorder=1)
for part in shapely.get_parts(city):
ax.plot(*part.exterior.xy, color=GREY, lw=1.8, zorder=1)
for k in borders.itertuples():
if k.regency == "Kota Bandung":
dx, dy = MOVE.get(k.name, (0, 0))
ax.text(k.x + dx, k.y + dy, k.name, color="#93A2A4", fontsize=6.5, ha="center", va="center", zorder=1)
for s in stops[::-1].itertuples(): # stop 0 last, so nothing covers it
ax.plot(s.x_m, s.y_m, "o", ms=13, color=GREEN if s.id == 0 else INK, mec="white", mew=1, zorder=3)
ax.text(s.x_m, s.y_m, s.id, color="white", ha="center", va="center_baseline", fontsize=7.5, weight="bold", zorder=3)
dx, dy, ha = LABEL.get(s.id, (11, 0, "left"))
ax.annotate(s.name + (" (start)" if s.id == 0 else ""), (s.x_m, s.y_m), xytext=(dx, dy), zorder=4,
textcoords="offset points", va="center", ha=ha, fontsize=8.5, color=INK, path_effects=HALO)
west, south, east, north = city.bounds
ax.set_xlim(west - 900, east + 600)
ax.set_ylim(south - 900, north + 300)
for x, y, name in [(0.05, 0.66, "KOTA CIMAHI"), (0.80, 0.86, "KABUPATEN BANDUNG"), (0.12, 0.93, "KABUPATEN BANDUNG BARAT"),
(0.45, 0.05, "KABUPATEN BANDUNG")]:
ax.text(x, y, name, transform=ax.transAxes, color=GREY, fontsize=7.5, ha="center", va="center", zorder=1)
x0, y0 = ax.get_xlim()[0] + 300, ax.get_ylim()[0] + 300
ax.plot([x0, x0 + 2000], [y0, y0], color=INK, lw=2, zorder=4)
ax.text(x0 + 1000, y0 + 110, "2 km", ha="center", fontsize=8.5, color=INK, zorder=4, path_effects=HALO)
ax.set_title(title, loc="left", fontsize=11.5, color=INK)
ax.set_aspect("equal")
ax.set(xticks=[], yticks=[])
ax.spines[:].set_visible(False)
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.04, top=0.95)
fig.text(0.99, 0.012, "Cafes © OpenStreetMap contributors. Borders: Badan Informasi Geospasial", ha="right", fontsize=7.5, color=GREY)
return ax
base_map("Fifteen coffee stops in Kota Bandung")
plt.show()