mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
Ask the sources that know the place before the ones that know the area
This commit is contained in:
@@ -405,3 +405,121 @@ export const OSM_PLACE_ID = /^(?:node|way|relation):/i;
|
||||
export function isGooglePlaceId(placeId: string): boolean {
|
||||
return !NON_GOOGLE_PLACE_ID.test(placeId);
|
||||
}
|
||||
|
||||
// ── Ranking Commons candidates ───────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* A candidate as the ranker needs to see it. Structurally the subset of
|
||||
* `CommonsCandidate` it reads, kept local so the pure function does not have to
|
||||
* import from the service that imports this file.
|
||||
*/
|
||||
export interface RankableCommonsCandidate {
|
||||
pageId: number | null;
|
||||
title: string | null;
|
||||
attribution: string | null;
|
||||
width: number | null;
|
||||
height: number | null;
|
||||
descriptors: string | null;
|
||||
photoUrl: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Commons files that are near a place but are not a picture of it.
|
||||
*
|
||||
* Orthophotos are the worst offender by volume: German states upload their
|
||||
* aerial survey tiles to Commons with coordinates, so a geosearch around any
|
||||
* airport returns the runway as seen from a plane at 3000m — four of them for
|
||||
* Hamburg, one per survey year. The rest are documents that happen to live in a
|
||||
* place's category: noise maps, floor plans, terminal layouts, logos, coats of
|
||||
* arms.
|
||||
*/
|
||||
const NOT_A_PHOTO_OF_THE_PLACE =
|
||||
/orthophoto|orthofoto|sommerbefliegung|luftbildkarte|dop\d+|l[äa]rmkarte|noise map|floor ?plan|grundriss|lageplan|layout|diagram|schematic|blueprint|coat of arms|wappen|\blogo\b|flag of/i;
|
||||
|
||||
/**
|
||||
* Strips what makes two frames of the same burst look like different files, so
|
||||
* they collapse onto one stem: trailing counters, camera dumps, dates, and the
|
||||
* `- 17` / `(2)` suffixes press sets use.
|
||||
*/
|
||||
function seriesStem(title: string): string {
|
||||
return title
|
||||
.replace(/^File:/i, '')
|
||||
.replace(/\.[a-z0-9]+$/i, '')
|
||||
.toLowerCase()
|
||||
// 20260614 100717648 HDR — a camera dump, all from the same minute
|
||||
.replace(/\b\d{8}[ _-]\d{6,9}\b/g, ' ')
|
||||
.replace(/\b(19|20)\d{2}\b/g, ' ')
|
||||
.replace(/[ _-]+\(?\d{1,4}\)?$/g, '')
|
||||
.replace(/[^a-z]+/g, ' ')
|
||||
.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks the pictures worth showing out of whatever the providers returned.
|
||||
*
|
||||
* Everything here was written against a real failing case. Deduplication is by
|
||||
* page id because the same file reaches us under different thumbnail URLs. The
|
||||
* series rule exists because Commons categories are full of bursts — the four
|
||||
* `Hauptbahnhof Berlin interior 0807..0810` shots that filled the picker were
|
||||
* one person walking across a concourse. The author cap is the same problem
|
||||
* seen from the other side: one contributor who documented a station thoroughly
|
||||
* should not supply the whole strip.
|
||||
*
|
||||
* `perAuthor` is the one knob: coordinate search gets 1, because there being no
|
||||
* evidence the pictures are even of the right subject makes variety the only
|
||||
* defence. Curated sources get 2 — someone already vouched that these depict
|
||||
* the place.
|
||||
*/
|
||||
export function rankCommonsCandidates<T extends RankableCommonsCandidate>(
|
||||
candidates: T[],
|
||||
limit: number,
|
||||
opts: { perAuthor?: number } = {},
|
||||
): T[] {
|
||||
const perAuthor = opts.perAuthor ?? 2;
|
||||
const seenPages = new Set<number>();
|
||||
const seenUrls = new Set<string>();
|
||||
const seenStems = new Set<string>();
|
||||
const authorCounts = new Map<string, number>();
|
||||
const out: T[] = [];
|
||||
|
||||
for (const candidate of candidates) {
|
||||
if (out.length >= limit) break;
|
||||
|
||||
if (candidate.pageId != null) {
|
||||
if (seenPages.has(candidate.pageId)) continue;
|
||||
} else if (seenUrls.has(candidate.photoUrl)) {
|
||||
// No page id (an older payload, or a provider that does not report one):
|
||||
// the URL is a weaker key but better than letting an exact repeat through.
|
||||
continue;
|
||||
}
|
||||
|
||||
const haystack = `${candidate.title ?? ''} ${candidate.descriptors ?? ''}`;
|
||||
if (NOT_A_PHOTO_OF_THE_PLACE.test(haystack)) continue;
|
||||
|
||||
const { width, height } = candidate;
|
||||
if (width && height) {
|
||||
// Survey tiles are square and enormous; nothing photographed by hand is.
|
||||
if (width === height && width >= 3000) continue;
|
||||
const ratio = Math.max(width / height, height / width);
|
||||
// Panorama strips and tall banners crop to nothing in a square tile.
|
||||
if (ratio > 4) continue;
|
||||
}
|
||||
|
||||
const stem = candidate.title ? seriesStem(candidate.title) : '';
|
||||
if (stem && seenStems.has(stem)) continue;
|
||||
|
||||
const author = (candidate.attribution ?? '').trim().toLowerCase();
|
||||
if (author) {
|
||||
const used = authorCounts.get(author) ?? 0;
|
||||
if (used >= perAuthor) continue;
|
||||
authorCounts.set(author, used + 1);
|
||||
}
|
||||
|
||||
if (candidate.pageId != null) seenPages.add(candidate.pageId);
|
||||
seenUrls.add(candidate.photoUrl);
|
||||
if (stem) seenStems.add(stem);
|
||||
out.push(candidate);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -54,6 +54,124 @@ interface NominatimResult {
|
||||
display_name?: string;
|
||||
lat: string;
|
||||
lon: string;
|
||||
extratags?: Record<string, string> | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys that say which encyclopaedia entry, Wikidata item and Commons
|
||||
* category describe a place. Everything the enrichment column shows beyond
|
||||
* coordinates hangs off one of these.
|
||||
*
|
||||
* Bare keys only. OSM also carries `brand:wikidata` / `brand:wikipedia`, and
|
||||
* following those means a branch of a chain gets the chain's article and the
|
||||
* chain's logo — for "L'Osteria Rostock" you would confidently describe
|
||||
* L'Osteria the company. That is the exact failure the tag-only rule was
|
||||
* written to avoid, so do not "improve" this by falling back to brand:*.
|
||||
*/
|
||||
const WIKI_IDENTITY_TAGS = ['wikipedia', 'wikidata', 'wikimedia_commons'] as const;
|
||||
|
||||
export interface WikiIdentity {
|
||||
wikipedia: string | null;
|
||||
wikidata: string | null;
|
||||
wikimedia_commons: string | null;
|
||||
}
|
||||
|
||||
interface WikidataSnak {
|
||||
mainsnak?: { datavalue?: { value?: string } };
|
||||
rank?: 'preferred' | 'normal' | 'deprecated';
|
||||
}
|
||||
type WikidataClaims = Record<string, WikidataSnak[] | undefined>;
|
||||
|
||||
/**
|
||||
* Wikidata properties that name a picture of a place, in the order we want them.
|
||||
*
|
||||
* P18 is the representative image. The rest exist because a station or a
|
||||
* monument is not one view: asking for the interior, the night shot, the
|
||||
* panorama and the aerial gives a picker four genuinely different pictures
|
||||
* instead of four frames of the same façade.
|
||||
*/
|
||||
const WIKIDATA_IMAGE_PROPERTIES = [
|
||||
'P18', // image
|
||||
'P5775', // interior view
|
||||
'P3451', // night view
|
||||
'P8592', // aerial view
|
||||
'P4291', // panoramic view
|
||||
'P5252', // winter view
|
||||
'P948', // Wikivoyage banner
|
||||
] as const;
|
||||
|
||||
/** First non-empty string value of a claim list. */
|
||||
function claimValue(snaks: WikidataSnak[] | undefined): string | null {
|
||||
for (const snak of snaks ?? []) {
|
||||
const value = snak.mainsnak?.datavalue?.value;
|
||||
if (typeof value === 'string' && value.trim()) return value.trim();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* File names from an item's picture properties, best first.
|
||||
*
|
||||
* Within P18 the statement rank decides: an item with several images marks one
|
||||
* `preferred`, and that is the one an editor considers representative. The API
|
||||
* returns statements in edit order, not rank order, so taking `[0]` picks
|
||||
* whichever was added first — for the Brandenburg Gate that is a coin toss
|
||||
* between the morning shot and a wide overview.
|
||||
*/
|
||||
function wikidataImageClaims(claims: WikidataClaims, limit: number): string[] {
|
||||
const rankOrder = { preferred: 0, normal: 1, deprecated: 2 } as const;
|
||||
const names: string[] = [];
|
||||
const seen = new Set<string>();
|
||||
|
||||
for (const property of WIKIDATA_IMAGE_PROPERTIES) {
|
||||
const snaks = [...(claims[property] ?? [])]
|
||||
.filter((snak) => snak.rank !== 'deprecated')
|
||||
.sort((a, b) => (rankOrder[a.rank ?? 'normal'] ?? 1) - (rankOrder[b.rank ?? 'normal'] ?? 1));
|
||||
for (const snak of snaks) {
|
||||
const value = snak.mainsnak?.datavalue?.value;
|
||||
if (typeof value !== 'string' || !value.trim()) continue;
|
||||
const key = normalizeFileTitle(value);
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
names.push(value.trim());
|
||||
if (names.length >= limit) return names;
|
||||
}
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
/** `File:` prefix off, underscores and case normalised — Commons treats these as one title. */
|
||||
function normalizeFileTitle(title: string): string {
|
||||
return title.replace(/^File:/i, '').replace(/_/g, ' ').trim().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* A bare Commons category name out of whatever an OSM `wikimedia_commons` tag
|
||||
* holds.
|
||||
*
|
||||
* The tag is free text and mappers put three different things in it: a bare
|
||||
* name, a prefixed `Category:…`, or — against the wiki's own advice — a single
|
||||
* `File:…`. Prefixing blindly turned the last one into `Category:File:X.jpg`,
|
||||
* which matches nothing and fell through to the coordinate search without a
|
||||
* word. Localised prefixes (`Kategorie:`) appear too.
|
||||
*/
|
||||
function normalizeCategoryName(raw: string): string | null {
|
||||
const value = raw.trim();
|
||||
if (!value) return null;
|
||||
// A file name is not a category, and there is nothing sensible to derive.
|
||||
if (/^(file|datei|image|bild)\s*:/i.test(value)) return null;
|
||||
return value.replace(/^(category|kategorie|categorie|categoría|categoria)\s*:/i, '').trim() || null;
|
||||
}
|
||||
|
||||
/** Picks the three identity tags out of a Nominatim `extratags` blob. */
|
||||
export function readWikiIdentity(extratags: Record<string, string> | null | undefined): WikiIdentity {
|
||||
const out: WikiIdentity = { wikipedia: null, wikidata: null, wikimedia_commons: null };
|
||||
if (!extratags) return out;
|
||||
for (const tag of WIKI_IDENTITY_TAGS) {
|
||||
const value = extratags[tag];
|
||||
if (typeof value === 'string' && value.trim()) out[tag] = value.trim();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
interface OverpassElement {
|
||||
@@ -61,10 +179,14 @@ interface OverpassElement {
|
||||
}
|
||||
|
||||
interface WikiCommonsPage {
|
||||
pageid?: number;
|
||||
title?: string;
|
||||
imageinfo?: {
|
||||
url?: string;
|
||||
thumburl?: string;
|
||||
mime?: string;
|
||||
width?: number;
|
||||
height?: number;
|
||||
/** The file description page — where the full licence terms live. */
|
||||
descriptionurl?: string;
|
||||
extmetadata?: {
|
||||
@@ -72,6 +194,10 @@ interface WikiCommonsPage {
|
||||
LicenseShortName?: { value?: string };
|
||||
LicenseUrl?: { value?: string };
|
||||
UsageTerms?: { value?: string };
|
||||
/** Used to spot survey imagery and diagrams, which are not pictures of a place. */
|
||||
Categories?: { value?: string };
|
||||
ObjectName?: { value?: string };
|
||||
ImageDescription?: { value?: string };
|
||||
};
|
||||
}[];
|
||||
}
|
||||
@@ -88,6 +214,20 @@ export interface CommonsCandidate {
|
||||
license: string | null;
|
||||
licenseUrl: string | null;
|
||||
sourceUrl: string | null;
|
||||
/**
|
||||
* Commons page id — the only stable identity a file has across the four ways
|
||||
* we reach it. The thumbnail URL is not: the same file comes back from
|
||||
* commons.wikimedia.org and from a language Wikipedia with different query
|
||||
* strings, so deduplicating on the URL silently lets the same picture through
|
||||
* twice. It is also what keys the cached bytes, so it must survive.
|
||||
*/
|
||||
pageId: number | null;
|
||||
/** File page title, e.g. `File:Brandenburger Tor morgens.jpg`. */
|
||||
title: string | null;
|
||||
width: number | null;
|
||||
height: number | null;
|
||||
/** Free text used to reject survey imagery, floor plans and logos. */
|
||||
descriptors: string | null;
|
||||
}
|
||||
|
||||
interface GooglePlaceResult {
|
||||
@@ -372,6 +512,11 @@ export class MapsService {
|
||||
q: query,
|
||||
format: 'json',
|
||||
addressdetails: '1',
|
||||
// Free, same request: this is where a place's wikidata/wikipedia/commons
|
||||
// tags live. Without them the enrichment column can only fall back to
|
||||
// "photos taken within 300m", which around a city centre is passers-by
|
||||
// and the neighbouring building.
|
||||
extratags: '1',
|
||||
limit: '10',
|
||||
'accept-language': toApiLang(lang),
|
||||
});
|
||||
@@ -402,6 +547,7 @@ export class MapsService {
|
||||
website: null,
|
||||
phone: null,
|
||||
source: 'openstreetmap',
|
||||
...readWikiIdentity(item.extratags),
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -417,11 +563,15 @@ export class MapsService {
|
||||
address: string;
|
||||
lat: number | null;
|
||||
lng: number | null;
|
||||
extratags: Record<string, string> | null;
|
||||
} | null> {
|
||||
const typePrefix = osmType.charAt(0).toUpperCase(); // N, W, R
|
||||
const params = new URLSearchParams({
|
||||
osm_ids: `${typePrefix}${osmId}`,
|
||||
format: 'json',
|
||||
// Overpass is the richer source but it is also the one that times out;
|
||||
// whatever Nominatim already knows costs nothing extra here.
|
||||
extratags: '1',
|
||||
'accept-language': toApiLang(lang),
|
||||
});
|
||||
try {
|
||||
@@ -439,6 +589,7 @@ export class MapsService {
|
||||
address: item.display_name || '',
|
||||
lat: Number.isFinite(lat) ? lat : null,
|
||||
lng: Number.isFinite(lng) ? lng : null,
|
||||
extratags: item.extratags ?? null,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
@@ -623,9 +774,13 @@ export class MapsService {
|
||||
ggsnamespace: '6',
|
||||
ggsradius: '300',
|
||||
ggscoord: `${lat}|${lng}`,
|
||||
ggslimit: String(Math.max(1, Math.min(limit, 20))),
|
||||
// Deliberately more than the caller asked for. Around anything worth
|
||||
// visiting the first few hits are survey tiles, passers-by and the
|
||||
// building next door; the ranker needs a pool to reject from, and
|
||||
// geosearch charges the same for one result as for twenty.
|
||||
ggslimit: String(Math.max(1, Math.min(Math.max(limit * 4, 8), 20))),
|
||||
prop: 'imageinfo',
|
||||
iiprop: 'url|extmetadata|mime',
|
||||
iiprop: 'url|extmetadata|mime|size',
|
||||
iiurlwidth: '400',
|
||||
});
|
||||
try {
|
||||
@@ -637,25 +792,30 @@ export class MapsService {
|
||||
});
|
||||
if (!res.ok) return [];
|
||||
const data = (await res.json()) as { query?: { pages?: Record<string, WikiCommonsPage> } };
|
||||
return this.toCommonsCandidates(data.query?.pages, limit);
|
||||
// Hand back the whole pool. fetchWikimediaPhoto still takes [0] and gets
|
||||
// what it always got; the enrichment column ranks before it cuts.
|
||||
return this.toCommonsCandidates(data.query?.pages, Number(params.get('ggslimit')));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** Shared shaping for both Commons queries (by coordinate and by category). */
|
||||
/** Shared shaping for every Commons query (coordinate, category, Wikidata, batch). */
|
||||
private toCommonsCandidates(
|
||||
pages: Record<string, WikiCommonsPage> | undefined,
|
||||
limit: number,
|
||||
): CommonsCandidate[] {
|
||||
if (!pages) return [];
|
||||
const out: CommonsCandidate[] = [];
|
||||
for (const page of Object.values(pages)) {
|
||||
// entries(), not values(): the map key is the page id, and for the queries
|
||||
// that reach a file by title it is the only place the id appears.
|
||||
for (const [key, page] of Object.entries(pages)) {
|
||||
const info = page.imageinfo?.[0];
|
||||
// Only use actual photos (JPEG/PNG), skip SVGs and PDFs
|
||||
const mime = info?.mime || '';
|
||||
if (!info?.url || !(mime.startsWith('image/jpeg') || mime.startsWith('image/png'))) continue;
|
||||
const meta = info.extmetadata;
|
||||
const pageId = page.pageid ?? (Number.isInteger(Number(key)) ? Number(key) : null);
|
||||
out.push({
|
||||
// iiurlwidth=400 makes Commons also return a scaled thumburl. Prefer it —
|
||||
// info.url is the full-resolution original (multi-megapixel camera exports).
|
||||
@@ -664,6 +824,17 @@ export class MapsService {
|
||||
license: stripWikiMarkup(meta?.LicenseShortName?.value) ?? stripWikiMarkup(meta?.UsageTerms?.value),
|
||||
licenseUrl: meta?.LicenseUrl?.value?.trim() || null,
|
||||
sourceUrl: info.descriptionurl || null,
|
||||
pageId: pageId && pageId > 0 ? pageId : null,
|
||||
title: page.title ?? null,
|
||||
width: info.width ?? null,
|
||||
height: info.height ?? null,
|
||||
descriptors: [
|
||||
stripWikiMarkup(meta?.ObjectName?.value),
|
||||
stripWikiMarkup(meta?.ImageDescription?.value),
|
||||
stripWikiMarkup(meta?.Categories?.value),
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' | ') || null,
|
||||
});
|
||||
if (out.length >= limit) break;
|
||||
}
|
||||
@@ -730,50 +901,150 @@ export class MapsService {
|
||||
}
|
||||
|
||||
/**
|
||||
* The one picture Wikidata records for a place (property P18).
|
||||
* The pictures Wikidata records for a place, best first.
|
||||
*
|
||||
* By far the most accurate source there is: someone chose this image to
|
||||
* By far the most accurate source there is: a person chose each of these to
|
||||
* represent this exact object, where a coordinate search only knows what was
|
||||
* photographed nearby. Restaurants and small venues carry a `wikidata` tag far
|
||||
* more often than a Commons category, so this is also the source that reaches
|
||||
* the places the others miss.
|
||||
* photographed nearby. Wikidata also keeps them apart by what they show, so
|
||||
* asking for more than P18 buys genuine variety rather than another frame of
|
||||
* the same burst — Berlin Hauptbahnhof has an exterior, two interiors, a
|
||||
* night shot, a panorama and a winter view, all curated.
|
||||
*
|
||||
* Two calls total whatever the item holds: one for the claims, one batch for
|
||||
* the file metadata.
|
||||
*/
|
||||
async fetchWikidataImage(wikidataId: string): Promise<CommonsCandidate | null> {
|
||||
if (!/^Q\d+$/.test(wikidataId.trim())) return null;
|
||||
async fetchWikidataCandidates(
|
||||
wikidataId: string,
|
||||
limit = 5,
|
||||
): Promise<{ candidates: CommonsCandidate[]; commonsCategory: string | null }> {
|
||||
const empty = { candidates: [], commonsCategory: null };
|
||||
const qid = wikidataId.trim();
|
||||
if (!/^Q\d+$/.test(qid)) return empty;
|
||||
try {
|
||||
const claims = await fetch(
|
||||
`https://www.wikidata.org/w/api.php?action=wbgetclaims&entity=${wikidataId.trim()}&property=P18&format=json`,
|
||||
const res = await fetch(
|
||||
`https://www.wikidata.org/w/api.php?action=wbgetentities&props=claims&ids=${qid}&format=json`,
|
||||
{ headers: { 'User-Agent': UA }, signal: AbortSignal.timeout(WIKI_TIMEOUT_MS) },
|
||||
);
|
||||
if (!claims.ok) return null;
|
||||
const data = (await claims.json()) as {
|
||||
claims?: { P18?: { mainsnak?: { datavalue?: { value?: string } } }[] };
|
||||
};
|
||||
const fileName = data.claims?.P18?.[0]?.mainsnak?.datavalue?.value;
|
||||
if (!fileName) return null;
|
||||
if (!res.ok) return empty;
|
||||
const data = (await res.json()) as { entities?: Record<string, { claims?: WikidataClaims }> };
|
||||
const claims = data.entities?.[qid]?.claims;
|
||||
if (!claims) return empty;
|
||||
|
||||
// Second hop for the URL and, more importantly, the licence — the claim
|
||||
// itself carries only a file name.
|
||||
const params = new URLSearchParams({
|
||||
action: 'query',
|
||||
format: 'json',
|
||||
titles: `File:${fileName}`,
|
||||
prop: 'imageinfo',
|
||||
iiprop: 'url|extmetadata|mime',
|
||||
iiurlwidth: '400',
|
||||
});
|
||||
const file = await fetch(`https://commons.wikimedia.org/w/api.php?${params}`, {
|
||||
const fileNames = wikidataImageClaims(claims, limit);
|
||||
const commonsCategory = claimValue(claims.P373) ?? null;
|
||||
if (fileNames.length === 0) return { candidates: [], commonsCategory };
|
||||
|
||||
const byTitle = await this.fetchCommonsFilesByName(fileNames);
|
||||
// Back into the order Wikidata implied, which the batch response loses.
|
||||
const candidates = fileNames.map((name) => byTitle.get(normalizeFileTitle(name))).filter((c): c is CommonsCandidate => !!c);
|
||||
return { candidates, commonsCategory };
|
||||
} catch {
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Metadata for a list of Commons files, in one request.
|
||||
*
|
||||
* `redirects=1` matters more than it looks: a Wikidata claim or a Wikipedia
|
||||
* lead image often names a file that has since been renamed, and without it
|
||||
* the API answers with a `missing` page and the picture disappears silently.
|
||||
* Keyed by normalised title so callers can restore their own ordering.
|
||||
*/
|
||||
async fetchCommonsFilesByName(fileNames: string[]): Promise<Map<string, CommonsCandidate>> {
|
||||
const out = new Map<string, CommonsCandidate>();
|
||||
const titles = fileNames.map((name) => (/^File:/i.test(name) ? name : `File:${name}`));
|
||||
if (titles.length === 0) return out;
|
||||
|
||||
const params = new URLSearchParams({
|
||||
action: 'query',
|
||||
format: 'json',
|
||||
titles: titles.join('|'),
|
||||
redirects: '1',
|
||||
prop: 'imageinfo',
|
||||
iiprop: 'url|extmetadata|mime|size',
|
||||
iiurlwidth: '400',
|
||||
});
|
||||
try {
|
||||
const res = await fetch(`https://commons.wikimedia.org/w/api.php?${params}`, {
|
||||
headers: { 'User-Agent': UA },
|
||||
signal: AbortSignal.timeout(WIKI_TIMEOUT_MS),
|
||||
});
|
||||
if (!file.ok) return null;
|
||||
const fileData = (await file.json()) as { query?: { pages?: Record<string, WikiCommonsPage> } };
|
||||
return this.toCommonsCandidates(fileData.query?.pages, 1)[0] ?? null;
|
||||
if (!res.ok) return out;
|
||||
const data = (await res.json()) as {
|
||||
query?: {
|
||||
pages?: Record<string, WikiCommonsPage>;
|
||||
normalized?: { from: string; to: string }[];
|
||||
redirects?: { from: string; to: string }[];
|
||||
};
|
||||
};
|
||||
// The API renames titles twice on the way in (normalisation, then
|
||||
// redirects), so walk the chain back to what the caller asked for.
|
||||
const aliases = new Map<string, string>();
|
||||
for (const hop of [...(data.query?.normalized ?? []), ...(data.query?.redirects ?? [])]) {
|
||||
aliases.set(normalizeFileTitle(hop.to), normalizeFileTitle(hop.from));
|
||||
}
|
||||
const resolveOriginal = (title: string): string => {
|
||||
let key = normalizeFileTitle(title);
|
||||
for (let hop = 0; hop < 4; hop++) {
|
||||
const previous = aliases.get(key);
|
||||
if (!previous || previous === key) break;
|
||||
key = previous;
|
||||
}
|
||||
return key;
|
||||
};
|
||||
|
||||
for (const candidate of this.toCommonsCandidates(data.query?.pages, titles.length)) {
|
||||
if (!candidate.title) continue;
|
||||
out.set(resolveOriginal(candidate.title), candidate);
|
||||
// Also reachable under its own name, for callers that already resolved.
|
||||
out.set(normalizeFileTitle(candidate.title), candidate);
|
||||
}
|
||||
return out;
|
||||
} catch {
|
||||
return null;
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The lead image a wiki article picked for a place.
|
||||
*
|
||||
* Only the file NAME is taken from here; the bytes and the licence come from
|
||||
* the same Commons batch as everything else. The thumbnail URL the API offers
|
||||
* alongside it carries no attribution, and a picture we cannot credit is a
|
||||
* picture we cannot show.
|
||||
*/
|
||||
async fetchWikiLeadImageName(wikipediaTag: string | null | undefined): Promise<string | null> {
|
||||
const parsed = parseWikipediaTag(wikipediaTag);
|
||||
if (!parsed) return null;
|
||||
const params = new URLSearchParams({
|
||||
action: 'query',
|
||||
format: 'json',
|
||||
titles: parsed.title,
|
||||
prop: 'pageimages',
|
||||
piprop: 'name',
|
||||
redirects: '1',
|
||||
});
|
||||
for (const host of ['wikivoyage', 'wikipedia'] as const) {
|
||||
try {
|
||||
const res = await fetch(`https://${parsed.lang}.${host}.org/w/api.php?${params}`, {
|
||||
headers: { 'User-Agent': UA },
|
||||
signal: AbortSignal.timeout(WIKI_TIMEOUT_MS),
|
||||
});
|
||||
if (!res.ok) continue;
|
||||
const data = (await res.json()) as {
|
||||
query?: { pages?: Record<string, { pageimage?: string }> };
|
||||
};
|
||||
for (const page of Object.values(data.query?.pages ?? {})) {
|
||||
if (page.pageimage) return page.pageimage;
|
||||
}
|
||||
} catch {
|
||||
/* try the next wiki */
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Commons images from a category, which is the set of pictures OF a place.
|
||||
*
|
||||
@@ -782,29 +1053,56 @@ export class MapsService {
|
||||
* passers-by, while the category of a restaurant returns the restaurant.
|
||||
*/
|
||||
async fetchCommonsCategoryCandidates(category: string, limit = 5): Promise<CommonsCandidate[]> {
|
||||
const title = category.startsWith('Category:') ? category : `Category:${category}`;
|
||||
const params = new URLSearchParams({
|
||||
const name = normalizeCategoryName(category);
|
||||
if (!name) return [];
|
||||
// Overfetch: the ranker throws away survey imagery, diagrams and repeats,
|
||||
// and it can only do that from a pool bigger than the strip.
|
||||
const poolSize = String(Math.max(1, Math.min(limit * 3, 20)));
|
||||
|
||||
// `generator=search` first. `categorymembers` orders by sort key, i.e.
|
||||
// alphabetically by file name, which is not a quality signal in any
|
||||
// direction: "Category:Brandenburg Gate" opens with an .ogg pronunciation,
|
||||
// a marathon photo and six near-identical press shots, and
|
||||
// "Category:Hamburg Airport" with a noise map and a terminal layout. The
|
||||
// search index at least ranks by how well a file matches its category.
|
||||
const search = new URLSearchParams({
|
||||
action: 'query',
|
||||
format: 'json',
|
||||
generator: 'search',
|
||||
gsrsearch: `incategory:"${name}" filetype:bitmap`,
|
||||
gsrnamespace: '6',
|
||||
gsrlimit: poolSize,
|
||||
prop: 'imageinfo',
|
||||
iiprop: 'url|extmetadata|mime|size',
|
||||
iiurlwidth: '400',
|
||||
});
|
||||
const members = new URLSearchParams({
|
||||
action: 'query',
|
||||
format: 'json',
|
||||
generator: 'categorymembers',
|
||||
gcmtitle: title,
|
||||
gcmtitle: `Category:${name}`,
|
||||
gcmtype: 'file',
|
||||
gcmlimit: String(Math.max(1, Math.min(limit * 2, 20))),
|
||||
gcmlimit: poolSize,
|
||||
prop: 'imageinfo',
|
||||
iiprop: 'url|extmetadata|mime',
|
||||
iiprop: 'url|extmetadata|mime|size',
|
||||
iiurlwidth: '400',
|
||||
});
|
||||
try {
|
||||
const res = await fetch(`https://commons.wikimedia.org/w/api.php?${params}`, {
|
||||
headers: { 'User-Agent': UA },
|
||||
signal: AbortSignal.timeout(WIKI_TIMEOUT_MS),
|
||||
});
|
||||
if (!res.ok) return [];
|
||||
const data = (await res.json()) as { query?: { pages?: Record<string, WikiCommonsPage> } };
|
||||
return this.toCommonsCandidates(data.query?.pages, limit);
|
||||
} catch {
|
||||
return [];
|
||||
|
||||
for (const params of [search, members]) {
|
||||
try {
|
||||
const res = await fetch(`https://commons.wikimedia.org/w/api.php?${params}`, {
|
||||
headers: { 'User-Agent': UA },
|
||||
signal: AbortSignal.timeout(WIKI_TIMEOUT_MS),
|
||||
});
|
||||
if (!res.ok) continue;
|
||||
const data = (await res.json()) as { query?: { pages?: Record<string, WikiCommonsPage> } };
|
||||
const hits = this.toCommonsCandidates(data.query?.pages, Number(poolSize));
|
||||
if (hits.length) return hits;
|
||||
} catch {
|
||||
/* fall through to the second strategy */
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1033,12 +1331,22 @@ export class MapsService {
|
||||
// OSM details: placeId is "node:123456" or "way:123456" etc.
|
||||
if (placeId.includes(':')) {
|
||||
const [osmType, osmId] = placeId.split(':');
|
||||
const element = await this.fetchOverpassDetails(osmType, osmId);
|
||||
const details = buildOsmDetails(element?.tags || {}, osmType, osmId);
|
||||
|
||||
// buildOsmDetails never yields name/address/coordinates — Nominatim is
|
||||
// always the source for those (Overpass contributes the tag-derived rest).
|
||||
const nominatim = await this.lookupNominatim(osmType, osmId, lang);
|
||||
const [element, nominatim] = await Promise.all([
|
||||
this.fetchOverpassDetails(osmType, osmId),
|
||||
this.lookupNominatim(osmType, osmId, lang),
|
||||
]);
|
||||
// Overpass has the fuller tag set and wins where both answer, but it is
|
||||
// also the one that goes down — overpass-api.de is regularly overloaded.
|
||||
// Nominatim's extratags carry the wikidata/wikipedia/commons tags too, so
|
||||
// a place keeps its pictures and its description when Overpass times out
|
||||
// instead of falling back to "photographed within 300m".
|
||||
const details = buildOsmDetails(
|
||||
{ ...(nominatim?.extratags ?? {}), ...(element?.tags ?? {}) },
|
||||
osmType,
|
||||
osmId,
|
||||
);
|
||||
|
||||
return {
|
||||
place: {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import type {
|
||||
MapsPlaceEnrichmentRequest,
|
||||
@@ -8,8 +9,8 @@ import type {
|
||||
} from '@trek/shared';
|
||||
import { safeFetchFollow } from '../../utils/ssrfGuard';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { MapsService, withPhotoFetchSlot } from '../maps/maps.service';
|
||||
import { isGooglePlaceId } from '../maps/maps.helpers';
|
||||
import { MapsService, withPhotoFetchSlot, type CommonsCandidate } from '../maps/maps.service';
|
||||
import { isGooglePlaceId, rankCommonsCandidates } from '../maps/maps.helpers';
|
||||
import { PlacePhotoCacheService } from '../place-photos/place-photo-cache.service';
|
||||
|
||||
/**
|
||||
@@ -37,11 +38,40 @@ const CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
* from before the change — which is exactly what happened when the facts and
|
||||
* Wikivoyage landed.
|
||||
*/
|
||||
const CACHE_VERSION = 2;
|
||||
const CACHE_VERSION = 3;
|
||||
|
||||
/** Cache key for one candidate picture. Deliberately not a bare place id. */
|
||||
function candidateKey(placeId: string, index: number): string {
|
||||
return `${placeId}~p${index}`;
|
||||
/**
|
||||
* Cache key for one candidate picture. Deliberately not a bare place id.
|
||||
*
|
||||
* Keyed by which picture it is, not by where it sat in the strip. Position was
|
||||
* the obvious choice and it was wrong: the ladder below returns different
|
||||
* numbers of candidates depending on which providers answered, so a slot that
|
||||
* held a Wikidata photo one minute held a category photo the next — and the
|
||||
* cache hands back whatever bytes it stored for that slot while the response
|
||||
* carries the new candidate's author. Under CC BY that is not a stale
|
||||
* thumbnail, it is the wrong person credited for someone's work.
|
||||
*
|
||||
* Hashed because the identity can be a Google resource path with slashes, and
|
||||
* this ends up in a URL.
|
||||
*/
|
||||
export function candidateKey(placeId: string, identity: string): string {
|
||||
const digest = createHash('sha1').update(identity).digest('hex').slice(0, 12);
|
||||
return `${placeId}~p${digest}`;
|
||||
}
|
||||
|
||||
/** A ranked Commons pick, tagged with the rung of the ladder it came from. */
|
||||
type CommonsPick = CommonsCandidate & { rung: 'wikidata' | 'wikipedia' | 'category' | 'nearby' };
|
||||
|
||||
interface PendingCandidate {
|
||||
/** Stable identity of the underlying file, for the cache key. */
|
||||
identity: string;
|
||||
candidate: PlacePhotoCandidate;
|
||||
fetchBytes: () => Promise<Buffer | null>;
|
||||
}
|
||||
|
||||
/** Appends candidates to the pool, remembering which rung produced them. */
|
||||
function push(pool: CommonsPick[], candidates: CommonsCandidate[], rung: CommonsPick['rung']): void {
|
||||
for (const candidate of candidates) pool.push({ ...candidate, rung });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,53 +237,95 @@ export class PlaceEnrichmentService {
|
||||
const apiKey = this.maps.getMapsKey(userId);
|
||||
const wantsGoogle = !!apiKey && !this.maps.photosDisabled() && isGooglePlaceId(placeId);
|
||||
|
||||
// Ask both providers at once — one listing call each, and they know nothing
|
||||
// about one another.
|
||||
// Accuracy, best first. Wikidata's P18 is one picture somebody chose to
|
||||
// represent this exact object. A Commons category is pictures of it. The
|
||||
// coordinate search is only pictures NEAR it, which around a city centre
|
||||
// means statues and passers-by — it is the last resort, not the plan.
|
||||
const category = typeof details?.wikimedia_commons === 'string' ? details.wikimedia_commons : null;
|
||||
const wikidata = typeof details?.wikidata === 'string' ? details.wikidata : null;
|
||||
const tag = (key: string): string | null => (typeof details?.[key] === 'string' ? (details[key] as string) : null);
|
||||
const wikidata = tag('wikidata');
|
||||
const wikipedia = tag('wikipedia');
|
||||
|
||||
const [googleRefs, wikidataImage, commons] = await Promise.all([
|
||||
wantsGoogle ? this.maps.fetchGooglePhotoRefs(placeId, apiKey!, GOOGLE_CAP) : Promise.resolve([]),
|
||||
wikidata ? this.maps.fetchWikidataImage(wikidata) : Promise.resolve(null),
|
||||
category
|
||||
? this.maps
|
||||
.fetchCommonsCategoryCandidates(category, COMMONS_CAP)
|
||||
.then((hits) => (hits.length ? hits : this.maps.fetchCommonsCandidates(req.lat, req.lng, COMMONS_CAP)))
|
||||
: this.maps.fetchCommonsCandidates(req.lat, req.lng, COMMONS_CAP),
|
||||
]);
|
||||
// Google's listing is one call for the whole strip and knows nothing about
|
||||
// the free sources, so it runs alongside them rather than in the ladder.
|
||||
const googlePending = wantsGoogle
|
||||
? this.maps.fetchGooglePhotoRefs(placeId, apiKey!, GOOGLE_CAP)
|
||||
: Promise.resolve([] as { name: string; attribution: string | null }[]);
|
||||
|
||||
// The Wikidata picture leads, and is dropped from the rest of the list when
|
||||
// a category happens to contain it too.
|
||||
const commonsList = wikidataImage
|
||||
? [wikidataImage, ...commons.filter((c) => c.photoUrl !== wikidataImage.photoUrl)].slice(0, COMMONS_CAP)
|
||||
: commons;
|
||||
// The free ladder, in order of how much anyone vouched that the picture
|
||||
// shows THIS place:
|
||||
// Wikidata — a person attached this file to this exact object.
|
||||
// Wiki lead — the article about it opens with this picture.
|
||||
// Category — the set of pictures of it.
|
||||
// Nearby — anything photographed within 300m.
|
||||
// Only the last one has no claim on the subject at all, which is how an
|
||||
// airport ended up represented by aerial survey tiles of its runway and a
|
||||
// gate by the underground station beneath it. It is the fallback, and it
|
||||
// only runs when the ladder above came up short.
|
||||
const commonsPool: CommonsPick[] = [];
|
||||
let categoryName = tag('wikimedia_commons');
|
||||
|
||||
// Google first — its pictures are of the place itself, while Commons
|
||||
// geosearch finds whatever happens to have been photographed nearby.
|
||||
const pending: PlacePhotoCandidate[] = [
|
||||
if (wikidata) {
|
||||
const fromWikidata = await this.maps.fetchWikidataCandidates(wikidata, COMMONS_CAP);
|
||||
push(commonsPool, fromWikidata.candidates, 'wikidata');
|
||||
categoryName ??= fromWikidata.commonsCategory;
|
||||
}
|
||||
|
||||
if (commonsPool.length < COMMONS_CAP && wikipedia) {
|
||||
const leadName = await this.maps.fetchWikiLeadImageName(wikipedia);
|
||||
if (leadName) {
|
||||
const byName = await this.maps.fetchCommonsFilesByName([leadName]);
|
||||
push(commonsPool, [...byName.values()], 'wikipedia');
|
||||
}
|
||||
}
|
||||
|
||||
if (commonsPool.length < COMMONS_CAP && categoryName) {
|
||||
push(commonsPool, await this.maps.fetchCommonsCategoryCandidates(categoryName, COMMONS_CAP), 'category');
|
||||
}
|
||||
|
||||
// Two is the bar: one curated picture plus the nearby noise reads worse
|
||||
// than one curated picture on its own.
|
||||
const curated = commonsPool.length;
|
||||
// Started here, decided below. Waiting for Google's listing first would put
|
||||
// two round trips end to end, and that is precisely the sequencing that
|
||||
// used to push this endpoint past the client's timeout. Geosearch is free
|
||||
// and unmetered, so a speculative call that gets discarded costs nothing
|
||||
// anyone pays for.
|
||||
const nearbyPending =
|
||||
curated < 2 ? this.maps.fetchCommonsCandidates(req.lat, req.lng, COMMONS_CAP) : Promise.resolve([]);
|
||||
|
||||
const googleRefs = await googlePending;
|
||||
if (curated < 2 && googleRefs.length < GOOGLE_CAP) {
|
||||
push(commonsPool, await nearbyPending, 'nearby');
|
||||
}
|
||||
|
||||
// One ranking pass over everything: the same file reaches us from several
|
||||
// rungs and only the page id catches that.
|
||||
const ranked = rankCommonsCandidates(commonsPool, COMMONS_CAP, { perAuthor: curated > 0 ? 2 : 1 });
|
||||
// Google leads the strip — its pictures are of the business itself.
|
||||
const pending: PendingCandidate[] = [
|
||||
...googleRefs.map((ref) => ({
|
||||
key: '',
|
||||
url: '',
|
||||
attribution: ref.attribution,
|
||||
// Google grants no reusable licence for these; the author line it hands
|
||||
// back is all we may show, so the rest stays honestly empty.
|
||||
license: null,
|
||||
licenseUrl: null,
|
||||
sourceUrl: null,
|
||||
source: 'google' as const,
|
||||
identity: `google:${ref.name}`,
|
||||
candidate: {
|
||||
key: '',
|
||||
url: '',
|
||||
attribution: ref.attribution,
|
||||
// Google grants no reusable licence for these; the author line it
|
||||
// hands back is all we may show, so the rest stays honestly empty.
|
||||
license: null,
|
||||
licenseUrl: null,
|
||||
sourceUrl: null,
|
||||
source: 'google' as const,
|
||||
},
|
||||
fetchBytes: () => this.maps.fetchGooglePhotoBytes(ref.name, apiKey!),
|
||||
})),
|
||||
...commonsList.map((candidate) => ({
|
||||
key: '',
|
||||
url: '',
|
||||
attribution: candidate.attribution,
|
||||
license: candidate.license,
|
||||
licenseUrl: candidate.licenseUrl,
|
||||
sourceUrl: candidate.sourceUrl,
|
||||
source: 'wikimedia' as const,
|
||||
...ranked.map((pick) => ({
|
||||
identity: `commons:${pick.pageId ?? pick.photoUrl}`,
|
||||
candidate: {
|
||||
key: '',
|
||||
url: '',
|
||||
attribution: pick.attribution,
|
||||
license: pick.license,
|
||||
licenseUrl: pick.licenseUrl,
|
||||
sourceUrl: pick.sourceUrl,
|
||||
source: (pick.rung === 'wikipedia' ? 'wikipedia' : 'wikimedia') as 'wikipedia' | 'wikimedia',
|
||||
},
|
||||
fetchBytes: () => this.fetchRemoteBytes(pick.photoUrl),
|
||||
})),
|
||||
];
|
||||
|
||||
@@ -262,16 +334,9 @@ export class PlaceEnrichmentService {
|
||||
// which pushed the whole request past the client's request timeout. The
|
||||
// shared slot limiter still caps how many run at once.
|
||||
const results = await Promise.all(
|
||||
pending.map(async (candidate, index) => {
|
||||
const key = candidateKey(placeId, index);
|
||||
const fetchBytes =
|
||||
candidate.source === 'google'
|
||||
? () => this.maps.fetchGooglePhotoBytes(googleRefs[index].name, apiKey!)
|
||||
: () => this.fetchRemoteBytes(commonsList[index - googleRefs.length].photoUrl);
|
||||
pending.map(async ({ identity, candidate, fetchBytes }) => {
|
||||
const key = candidateKey(placeId, identity);
|
||||
const url = await this.storeCandidate(key, creditLine(candidate.attribution, candidate.license), fetchBytes);
|
||||
// The index is fixed before any download, so a picture that fails to
|
||||
// load leaves a gap rather than renumbering the ones after it — the key
|
||||
// has to keep pointing at the same file across requests.
|
||||
return url ? { ...candidate, key, url } : null;
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user