INK, GREEN, GREY, AMBER = "#003A40", "#3E9A4B", "#7C8B8D", "#C4622D"
HALO = [withStroke(linewidth=2.5, foreground="white")]
def shapes(text, **params):
"""Geometri dari kueri yang mengembalikan ST_AsBinary(geom) AS wkb."""
return shapely.from_wkb(query(text, **params).wkb.map(bytes))
BORDERS = shapes("SELECT ST_AsBinary(geom) AS wkb FROM kecamatan WHERE regency = 'Kota Bandung'")
CITY = shapely.union_all(BORDERS)
def city_map(title, streets=False):
"""Kota Bandung dengan batas kecamatannya, atau dengan jalan-jalannya, dan skala 2 km."""
fig, ax = plt.subplots(figsize=(9, 6.3))
if streets:
roads = shapes("SELECT ST_AsBinary(geom) AS wkb FROM road")
ax.add_collection(LineCollection([shapely.get_coordinates(r) for r in roads], colors="#CBD3D4",
linewidths=0.4, zorder=1, rasterized=True))
else:
for part in shapely.get_parts(BORDERS):
ax.plot(*part.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.5, zorder=1)
west, south, east, north = CITY.bounds
ax.set_xlim(west - 300, east + 300)
ax.set_ylim(south - 900, north + 200)
x0, y0 = west, south - 600
ax.plot([x0, x0 + 2000], [y0, y0], color=INK, lw=2, zorder=6)
ax.text(x0 + 1000, y0 + 110, "2 km", ha="center", fontsize=8.5, color=INK, zorder=6, path_effects=HALO)
ax.set_title(title, loc="left", fontsize=11.5, color=INK)
ax.set_aspect("equal")
ax.axis("off")
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.03, top=0.95)
fig.text(0.99, 0.012, "Data © OpenStreetMap contributors. Borders: Badan Informasi Geospasial", ha="right", fontsize=7.5, color=GREY)
return ax
def draw_stops(ax, numbers):
"""Satu titik untuk setiap perhentian perintah kerja, dengan nomor di dalamnya."""
for s, number in list(zip(stops.itertuples(), numbers))[::-1]:
ax.plot(s.x_m, s.y_m, "o", ms=14, color=GREEN if s.stop == 0 else INK, mec="white", mew=1, zorder=5)
ax.text(s.x_m, s.y_m, number, color="white", ha="center", va="center_baseline", fontsize=7.5, weight="bold", zorder=5)
ax = city_map("Work order 1: twenty puskesmas in Kota Bandung")
others = query("SELECT ST_X(geom) AS x, ST_Y(geom) AS y FROM puskesmas WHERE node <> ALL(%(nodes)s)", nodes=stops.node.tolist())
ax.plot(others.x, others.y, "o", ms=6, color="#9FB0B2", zorder=3)
draw_stops(ax, stops.stop)
plt.show()