feat(maps): show a chain's own logo on its map pin

A corridor full of petrol stations was a row of identical coloured dots, and the one
thing that tells them apart is the brand. OSM carries brand:wikidata on most of them —
88% of the fuel stations in a Berlin box — Wikidata carries the logo as property P154,
and Commons serves it.

The bytes are proxied and cached rather than linked: one self-hosted instance asking
Wikimedia for a handful of logos is a very different egress profile from every
visitor's browser announcing which petrol stations they are looking at. The cache is in
memory because a GET must not write to the database, and there are only so many fuel
brands; a brand with no logo is remembered too, or every pan would ask again.

A logo-less or unreachable brand answers 204 and the pin keeps its category icon, so
nothing depends on Wikidata being reachable. Both renderers show it, since both read
the same category table.
This commit is contained in:
Maurice
2026-08-28 23:09:59 +02:00
committed by Maurice
parent 74258bd5d9
commit 37179cb608
7 changed files with 253 additions and 9 deletions
+22
View File
@@ -246,6 +246,28 @@ export class MapsController {
}
}
/**
* A brand's logo, by Wikidata id — what makes a corridor full of petrol stations
* readable at a glance.
*
* Proxied rather than linked: the browser never announces to Wikimedia which brands
* a user is looking at, and one instance asking for a handful of logos is a very
* different egress profile from every visitor doing it. An unknown or logo-less
* brand answers 204, like the photo route above and for the same reason — a marker
* per petrol station would otherwise be a burst of 404s from one address.
*/
@Get('brand-logo/:wikidataId')
async brandLogo(@Param('wikidataId') wikidataId: string, @Res() res: Response): Promise<void> {
const logo = await this.maps.brandLogo(wikidataId);
if (!logo) {
this.emptyPhoto(res);
return;
}
res.set('Cache-Control', 'public, max-age=2592000, immutable');
res.type(logo.contentType);
res.send(logo.bytes);
}
// 204 for "no bytes to serve". Overrides the immutable Cache-Control the hit
// path already set — a photo that reappears in the cache must not stay hidden
// behind a month-old empty response.
+3
View File
@@ -261,6 +261,9 @@ export interface OverpassPoi {
phone: string | null;
opening_hours: string | null;
cuisine: string | null;
/** Brand name and its Wikidata id, when OSM carries them — the logo is looked up from the id. */
brand: string | null;
brand_wikidata: string | null;
/**
* Which index the row came from. Overture is not OpenStreetMap: it carries
* OSM among other sources under other licences, so a row from the TREK index
+100
View File
@@ -379,6 +379,29 @@ function isGoogleMapsHost(hostname: string): boolean {
const WIKI_TIMEOUT_MS = 6000;
// ── Brand logos ──────────────────────────────────────────────────────────────
//
// A road trip corridor is mostly chains — Shell, Aral, JET — and the brand is the
// fastest thing to recognise on a map. OSM carries `brand:wikidata` on most of them,
// Wikidata carries the logo (P154), and Commons serves it.
//
// The bytes are proxied rather than linked so the browser never talks to Wikimedia:
// one self-hosted instance asking for a handful of logos is a very different egress
// profile from every visitor's browser announcing which petrol stations they are
// looking at. The cache is in memory on purpose — a GET must not write to the DB, and
// there are only so many fuel brands.
const BRAND_LOGO_CACHE_TTL_MS = 24 * 60 * 60 * 1000;
const BRAND_LOGO_CACHE_MAX = 300;
/** Well past any logo; a Commons original can be a multi-megabyte SVG or print-res PNG. */
const BRAND_LOGO_MAX_BYTES = 512 * 1024;
const BRAND_LOGO_WIDTH = 96;
const WIKIDATA_ID_RE = /^Q[1-9][0-9]{0,11}$/;
export interface BrandLogo {
bytes: Buffer;
contentType: string;
}
// Tighter than the wiki calls, because this one sits at the FRONT of a chain:
// identity, then sitelinks, then the extract. Nominatim answers a bounded
// search in 0.2-0.7s in practice, so anything past a couple of seconds is a bad
@@ -547,6 +570,10 @@ export class MapsService {
private readonly photoCache: PlacePhotoCacheService,
) {}
/** Brand id → logo bytes, or null for "asked, has none". Insertion-ordered, so the
* oldest entry is the one evicted when it fills up. */
private readonly brandLogoCache = new Map<string, { at: number; logo: BrandLogo | null }>();
private isSettingDisabled(key: string): boolean {
const row = this.database.get<{ value: string }>(
'SELECT value FROM app_settings WHERE key = ?',
@@ -641,6 +668,70 @@ export class MapsService {
return this.resolveGoogleMapsUrl(url) as Promise<MapsResolveUrlResult>;
}
/**
* The logo of a brand, by its Wikidata id, as bytes.
*
* Two hops: Wikidata says which Commons file is the logo (property P154), Commons
* serves a thumbnail of it. Both answers are cached, including "this brand has no
* logo" — otherwise every map pan would ask Wikidata about the same supermarket
* chain again. Returns null whenever anything is missing or unreadable; a marker
* without a logo falls back to its category icon, which is a fine outcome.
*/
async brandLogo(wikidataId: string): Promise<BrandLogo | null> {
if (!WIKIDATA_ID_RE.test(wikidataId)) return null;
const cached = this.brandLogoCache.get(wikidataId);
if (cached && Date.now() - cached.at < BRAND_LOGO_CACHE_TTL_MS) return cached.logo;
const remember = (logo: BrandLogo | null): BrandLogo | null => {
if (this.brandLogoCache.size >= BRAND_LOGO_CACHE_MAX) {
const oldest = this.brandLogoCache.keys().next().value;
if (oldest !== undefined) this.brandLogoCache.delete(oldest);
}
this.brandLogoCache.set(wikidataId, { at: Date.now(), logo });
return logo;
};
try {
const params = new URLSearchParams({
action: 'wbgetclaims',
entity: wikidataId,
property: 'P154',
format: 'json',
});
const claimRes = await fetch(`https://www.wikidata.org/w/api.php?${params}`, {
headers: { 'User-Agent': UA },
signal: AbortSignal.timeout(WIKI_TIMEOUT_MS),
});
if (!claimRes.ok) return remember(null);
const claims = (await claimRes.json()) as {
claims?: { P154?: { mainsnak?: { datavalue?: { value?: unknown } } }[] };
};
const file = claims.claims?.P154?.[0]?.mainsnak?.datavalue?.value;
if (typeof file !== 'string' || !file.trim()) return remember(null);
// Special:FilePath renders a thumbnail at the width asked for and redirects to
// the CDN, so each hop is re-checked by the guard rather than trusted.
const url = `https://commons.wikimedia.org/wiki/Special:FilePath/${encodeURIComponent(file)}?width=${BRAND_LOGO_WIDTH}`;
const imgRes = await safeFetchFollow(url, undefined, { bypassInternalIpAllowed: true });
if (!imgRes.ok) return remember(null);
const declared = Number(imgRes.headers.get('content-length') ?? '0');
if (declared > BRAND_LOGO_MAX_BYTES) return remember(null);
const bytes = Buffer.from(await imgRes.arrayBuffer());
// Checked again after reading: a chunked response has no length to check first.
if (bytes.byteLength === 0 || bytes.byteLength > BRAND_LOGO_MAX_BYTES) return remember(null);
const contentType = imgRes.headers.get('content-type') ?? '';
if (!contentType.startsWith('image/')) return remember(null);
return remember({ bytes, contentType });
} catch (err) {
if (err instanceof SsrfBlockedError) return remember(null);
return remember(null);
}
}
// POI search by category within a viewport. Never calls Google.
//
// The index answers first: it holds the same places, indexed in an R-Tree, and
@@ -695,6 +786,11 @@ export class MapsService {
website: p.contact?.website ?? null,
phone: p.contact?.phone ?? null,
opening_hours: p.hours?.osm ?? null,
// The index carries the chain and its Wikidata item, which is what
// the logo on the pin is looked up from — so a branch of a chain
// gets its own mark here exactly as it does on the Overpass path.
brand: p.brand?.name ?? null,
brand_wikidata: p.brand?.wikidata ?? null,
// The index has no cuisine field, so this null is the truth
// rather than a field being dropped on the way through.
cuisine: null,
@@ -1083,6 +1179,10 @@ export class MapsService {
phone: tags.phone || tags['contact:phone'] || null,
opening_hours: tags.opening_hours || null,
cuisine: tags.cuisine || null,
brand: tags.brand || tags.operator || null,
// Only the plain Q-id form is passed on; anything else would be a lookup we
// would have to guess at.
brand_wikidata: /^Q[0-9]+$/.test(tags['brand:wikidata'] || '') ? tags['brand:wikidata'] : null,
source: 'openstreetmap',
});
}