1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
.Dd September 5, 2026
.Dt TWIN-TOWNS 1
.Os
.Sh NAME
.Nm twin-towns
.Nd hoverable world map of sister cities, from Wikidata
.Sh SYNOPSIS
.Nm
.Cm ingest
.Fl -db Ar path
.Op Fl -endpoint Ar url
.Op Fl -timeout Ar duration
.Nm
.Cm serve
.Fl -db Ar path
.Op Fl -addr Ar host:port
.Sh DESCRIPTION
.Nm
draws every twin-town / sister-city relation recorded on Wikidata as a world
map.
Hovering a city highlights it, draws an arc to each of its twins and lists
them in a sidebar; clicking pins the selection so the pointer can be moved
away.
.Pp
Where Wikidata records when a twinning began
.Pq Dv P580
or ended
.Pq Dv P582 ,
the year is shown next to the twin and the map can be wound back through it:
a histogram of twinnings per year sits above a slider, and moving the slider
hides every town and link not yet twinned by that year.
Dissolved twinnings are drawn dashed and in their own colour rather than
being hidden, because a twinning that ended is a fact about the world and not
an absence of one.
.Pp
A search box finds towns by name, matching loosely enough that
.Ql brln
finds Berlin and
.Ql zurich
finds
.Dq Zürich ,
since names are compared with diacritics stripped.
Towns the year filter is currently hiding are still listed, greyed out and
ranked last: they can be selected anyway, which pins them and shows where
they are.
Hiding them outright would make the box appear broken, as searching a town
you know exists would answer
.Dq no match
with no hint that a slider is the reason.
.Pp
Following a twin from the sidebar selects it on the map rather than loading a
page: the address becomes
.Pa /?q=Q64
and Back walks the towns visited, so a chain of twinnings can be explored
without discarding the viewport, the year filter, or the dataset already in
memory.
The server-rendered
.Pa /place/...
page is still what a modified click, a crawler, or a browser without
JavaScript gets.
.Pp
The year window, the undated toggle, the zoom and pan, the pinned town and
the colour theme are remembered in
.Dv localStorage
between visits.
A
.Ar ?q=
in the URL overrides the remembered selection, so a shared link opens on the
place it names.
.Pp
The map is drawn dark or light according to the operating system's
preference, and a button beside the title cycles that through three states:
follow the system, light, dark.
Three and not two, because once a choice is pinned there would otherwise be
no value left meaning
.Dq follow the system ,
and that is the right answer for anyone whose desktop switches at sunset.
The light palette is not the dark one inverted \(em a dot that glows on
near-black is invisible on near-white \(em so the map colours are chosen
separately for each.
The choice applies to
.Pa /place/...
as well, though that page carries no button of its own: it is the
no-JavaScript path, and a control that does nothing without JavaScript does
not belong on it.
Without JavaScript both pages still follow the system preference; only the
override needs it.
.Pp
It is split into two subcommands: one that
.Cm ingest Ns s
the current state of Wikidata into SQLite, and one that
.Cm serve Ns s
that database over HTTP.
Ingest is the only part that touches the network, so the server can run
offline and is never slowed by an upstream outage.
.Ss Subcommands
.Bl -tag -width Ds
.It Cm ingest Fl -db Ar path Op Fl -endpoint Ar url Op Fl -timeout Ar duration
Query the Wikidata Query Service for every
.Dv P190
.Pq "twinned administrative body"
statement and the metadata of the entities involved, then replace the
contents of the database with the result.
The replacement happens in a single transaction, so an interrupted or failed
ingest leaves the previously served data intact.
.Pp
As a guard against publishing an empty map when the endpoint misbehaves,
a result with no places or no pairs is refused rather than written.
.It Cm serve Fl -db Ar path Op Fl -addr Ar host:port
Serve the map.
Defaults to
.Ar 127.0.0.1:8778 .
The dataset is read once at startup and held in memory, so requests never
touch SQLite except for the per-place pages.
.El
.Sh ROUTES
.Bl -tag -width "/static/style.css"
.It Pa /
The map.
Accepts
.Ar ?q=Q64
to open with that place already selected, which is what makes the sidebar
permalink round-trip back into the map.
.It Pa /data.json
The whole dataset in one payload: every plottable place and every edge
between two plottable places.
Edges are a flat array of four numbers each \(en
.Ar a , b , startYear , endYear
\(en where the two indices point into the places array and a year of
.Ar 0
means the date is not recorded.
Served gzipped from memory with a strong
.Dv ETag .
.It Pa /place/Q64
One place and all its twins, server-rendered.
This is the no-JavaScript path, and the only place where twins that have no
coordinates are visible at all.
.It Pa /static/map.js , Pa /static/style.css
Embedded assets.
Served
.Li no-cache
with a strong ETag, so a visit costs a conditional request and almost always
a 304.
They are only tens of kilobytes and change only on deploy, and the
alternative is worse: with no explicit directive a browser guesses a
freshness lifetime from
.Li Last-Modified ,
which for an embedded asset can only be the process start time, so the
window grows with uptime and a deploy reaches returning visitors hours or
days late \(em while
.Pa /data.json ,
which is
.Li no-cache ,
has already changed underneath the stale code.
.El
.Sh DATA MODEL
Two tables.
.Ar place
holds one row per entity appearing on either side of a twinning, with its
label, country and coordinates;
.Ar twin
holds one row per unordered pair, normalised so that
.Ar a No < Ar b
and enforced by a
.Dv CHECK
constraint.
.Pp
.Ar twin
also carries
.Ar started
and
.Ar ended ,
the
.Dv P580
and
.Dv P582
qualifiers of the underlying statement, as ISO dates.
Both are usually
.Dv NULL :
only about a third of twinnings record when they began and under one percent
when they ended.
A date whose month or day Wikidata gives as
.Ql 00
is stored as the bare year rather than being rounded to January 1st.
.Pp
The
.Ar place
coordinates are nullable and the
.Ar twin
table references places that may have none, which is deliberate \(en see
.Sx CAVEATS .
.Sh EXAMPLES
Populate a database and serve it:
.Bd -literal -offset indent
twin-towns ingest --db twin-towns.db
twin-towns serve --db twin-towns.db --addr 127.0.0.1:8778
.Ed
.Pp
Refreshing the data is always two steps, because
.Cm serve
reads the whole dataset once at startup and never looks at the file again:
.Bd -literal -offset indent
twin-towns ingest --db twin-towns.db
systemctl --user restart twin-towns
.Ed
.Pp
On the deployed host the same two steps are two units, and the restart is
separate for a reason: the ingest unit runs as a
.Xr systemd.exec 5
.Dv DynamicUser
and is refused permission to restart anything.
.Bd -literal -offset indent
systemctl start twin-towns-ingest
systemctl restart twin-towns
.Ed
.Pp
There is no timer.
Wikidata gains a handful of P190 statements a day, each ingest costs about a
minute of a free public SPARQL endpoint's time, and a refresh that changes
what the map claims is worth performing deliberately rather than silently.
.Sh SEE ALSO
.Lk https://www.wikidata.org/wiki/Property:P190 "Wikidata property P190"
.Pp
Wikidata content is available under CC0.
.Sh AUTHORS
.An Profpatsch
.Sh CAVEATS
.Ss P190 is not symmetric in practice
Wikidata documents
.Dv P190
as a symmetric property, and it is tempting to deduplicate the pairs in
SPARQL with
.Ql FILTER(STR(?a) < STR(?b)) .
That is wrong: 6537 statements exist in only one direction, and the filter
silently discards every pair whose single statement happens to point
.Dq backwards ,
yielding 25980 pairs where 29212 exist.
.Nm
therefore fetches both directions and normalises in code.
If you change the query, do not reintroduce that filter.
.Ss Not every twin can be drawn
About 110 of the 20141 twinned entities have no
.Dv P625
coordinate.
They are still ingested and still listed on
.Pa /place/... ,
but they cannot appear on the map, and an edge is only drawn when
.Em both
endpoints are plottable.
A city's twin count on the map can therefore be lower than on its page; the
page is the truthful one.
.Ss The map has no coastlines
Every coordinate rendered comes from Wikidata and nothing else.
At 20031 points the dots trace the continents by themselves, and a graticule
supplies the spatial reference that outlines otherwise would.
This is a deliberate refusal to acquire a second dataset: Wikidata stores
country outlines only as a
.Dv P3896
pointer to a Commons
.Pa .map
page, which would mean 205 separate fetches of roughly 30 MB of unsimplified
geometry, for a backdrop the data already draws.
.Ss Most twinnings are undated, so the timeline is a lower bound
Only about 10000 of the 29000 pairs record a start date, and only 7400 of the
20000 mapped towns have even one dated twinning.
The histogram and the year slider therefore describe the *dated* subset, not
the world: winding the slider back to 1970 shows the roughly 1300 towns known
to have twinned by then, not the number that actually had.
.Pp
This is why undated twinnings are shown by default and hidden only by an
explicit toggle, rather than being quietly dropped when the slider moves.
With the toggle on, a town stays on the map as long as any of its twinnings
is undated \(en which for two thirds of them is always.
.Ss Dates disagree with themselves
Both directions of a symmetric statement can carry a start date, and about
494 pairs give two different ones \(en usually adjacent years, sometimes
adjacent days.
Neither is authoritative, so ingest keeps the earliest start and the latest
end: the twinning demonstrably spanned at least that.
.Ss Some date values are not dates
The live data contains start dates that are blank-node URIs
.Pq Wikidata's Dq unknown value ,
a year 51983 and a year 199.
These are rejected at ingest rather than stored, and counted in the
.Dq unusable date values
line.
Anything outside the years 700 to next year is treated as a typo; the oldest
genuine date in the set is 1561, for Greater London and Moscow, which dates
the relationship to the Muscovy Company rather than to any modern twinning.
.Ss The two ingest queries are not one snapshot
Places and pairs are fetched by separate queries, so a statement added
between them can name an entity that was not fetched.
Such statements are dropped and counted in the
.Dq statements skipped
line rather than violating the foreign key.
The count is normally small; a large one means something changed upstream.
.Ss The whole dataset ships to the browser
.Pa /data.json
is roughly 1.5 MB of JSON.
This is the price of instant hovering over 20k dots: a round trip per hover
would be unusable, and the payload is smaller than a handful of map tiles.
It does mean the first paint waits for that fetch.
.Ss The themes need a 2024 browser
Every colour is defined once as
.Ic light-dark( light , dark ) ,
which needs Firefox 120, Chrome 123 or Safari 17.5.
Older browsers do not merely fall back to one theme: they fail to parse the
declaration and get no colour at all, which is unreadable rather than
merely wrong.
The alternative was writing the light palette out a second time inside a
.Ic @media
block and keeping the copies in step by hand, which is the more common bug.
.Pp
Note that
.Ic light-dark()
is defined for
.Aq color
only.
The dots' transparency therefore lives in the alpha channel of
.Li --dot
rather than in a
.Ic fill-opacity
of their own: a
.Li light-dark(0.75, 0.55)
is not a number, so the property would be invalid and silently fall back to
1 \(em in both themes at once, flattening the overlapping dots that are most
of what the map shows.
For the same reason
.Li --link
exists as an opaque copy of the same hue, for text and the histogram bars,
which set their own opacity and would otherwise apply it twice.