Skip to content

Commit 608e018

Browse files
author
Nathaniel Henry
committed
Draw the close_map() mark as crossing lines so it renders, and fix the reverse-scale docs.
1 parent 40c6489 commit 608e018

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

src/closecity/map.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ def close_map(
124124
``fill`` is the name of a numeric column to shade features by, on a
125125
continuous ColorBrewer scale with a legend (use it OR ``highlight``);
126126
``palette`` (default ``"YlGnBu"``) and ``reverse`` control that scale.
127-
``reverse=False`` puts the blue end at the high values; pass ``reverse=True``
128-
when high values mean *less* access, e.g. travel time.
127+
``reverse=False`` (default) puts blue at the low values, ``reverse=True`` at
128+
the high values — choose so blue marks the most-accessible end.
129129
130130
``boundary`` is a polygon GeoDataFrame drawn as a grey outline underneath
131131
(e.g. a city boundary from ``place_boundary``). ``background`` is one polygon
132132
GeoDataFrame, or a list of them, drawn as semi-transparent fills underneath
133133
(e.g. commute isochrones or a walkshed); ``background_color`` recycles across
134-
them. ``mark`` draws an "✕" on top at a ``(lon, lat)`` pair or a point
134+
them. ``mark`` draws an X on top at a ``(lon, lat)`` pair or a point
135135
GeoDataFrame (e.g. a starting point). Returns a plotly ``Figure``.
136136
"""
137137
try:
@@ -239,7 +239,8 @@ def close_map(
239239
traces.append(go.Choroplethmapbox(
240240
z=z, colorscale=colorscale, showscale=False, **common))
241241

242-
# A point marked with an "✕", drawn last so it sits on top of everything.
242+
# A point marked with an X (two crossing line segments — mapbox text glyphs
243+
# do not render on the raster basemap), drawn last so it sits on top.
243244
if mark is not None:
244245
import geopandas as gpd
245246

@@ -250,9 +251,17 @@ def close_map(
250251
else:
251252
mlon, mlat = [mark[0]], [mark[1]]
252253
bounds.append([min(mlon), min(mlat), max(mlon), max(mlat)])
254+
xs = [c for b in bounds for c in (b[0], b[2])]
255+
ys = [c for b in bounds for c in (b[1], b[3])]
256+
hs = max(max(xs) - min(xs), max(ys) - min(ys)) * 0.02
257+
lons, lats = [], []
258+
for lo, la in zip(mlon, mlat):
259+
dla = hs * math.cos(math.radians(la)) # square against the Mercator stretch
260+
lons += [lo - hs, lo + hs, None, lo - hs, lo + hs, None]
261+
lats += [la - dla, la + dla, None, la + dla, la - dla, None]
253262
traces.append(go.Scattermapbox(
254-
lon=mlon, lat=mlat, mode="text", text=["✕"] * len(mlon),
255-
textfont={"size": 22, "color": "#111111"},
263+
lon=lons, lat=lats, mode="lines",
264+
line={"color": "#111111", "width": 3},
256265
hoverinfo="skip", showlegend=False))
257266

258267
center, zoom = _center_zoom(bounds, buffer)

tests/test_map.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_close_map_mark():
5151
import plotly.graph_objects as go
5252
fig = close_map(_points(), mark = (-71.41, 41.82))
5353
assert isinstance(fig, go.Figure)
54-
assert fig.data[-1].mode == "text"
54+
assert fig.data[-1].mode == "lines"
5555

5656

5757
def test_close_map_boundary_and_background_layers():

0 commit comments

Comments
 (0)