mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +00:00
maps: lead with the place a name stands for, and leave Nominatim to installs without the services
The suggestions and the full search now share one merge. Photon's hits are ranked with places that have a Wikidata link before their unlinked namesakes, and its context bias is narrower (zoom 13). The list opens on a lead where Photon's answer has one: a country or region always; a town, island or sight when it is called what was typed (or a near typo of it), lies within 1000 km of where the traveller is looking and no place of that name ranks above it. Rows of the lead's name inside it, the same element under two ids and rows that read the same are shown once. A three-letter code in capitals is asked for as the airport of the bundled list. Where Photon is the geocoder it answers alone: an empty answer and a miss are the answer for search and imports, the reverse lookup asks again within 50 km, a Google place is matched inside a box through Photon, and an opened OpenStreetMap place is placed by Overpass and named by Photon. Nominatim stays the search of installations without the TREK services or with an operator's own Nominatim. Measured on 5000 places from Wikidata: the right place first while typing 48 % before, 65 % now; full search 53 % to 68 %; no repeated rows.
This commit is contained in:
@@ -116,7 +116,7 @@ function settings(rows: Record<string, string>): AppSettingsRepository {
|
||||
function withPhoton(rows: Suggestion[] | null, settingRows: Record<string, string> = {}) {
|
||||
trekPlaces.on = true;
|
||||
const parts = buildMapsParts({} as PlacePhotoCacheService, settings(settingRows), noUsers, {} as never, {} as never);
|
||||
const photon = vi.spyOn(parts.osm, 'autocompletePhoton').mockResolvedValue(rows);
|
||||
const photon = vi.spyOn(parts.osm, 'suggestPhoton').mockResolvedValue(rows && { rows, lead: null });
|
||||
return { svc: parts.svc, photon };
|
||||
}
|
||||
|
||||
@@ -540,7 +540,8 @@ describe('MapsService.autocompletePlaces', () => {
|
||||
mockSearch.mockResolvedValue([]);
|
||||
const { svc, photon } = withPhoton(null);
|
||||
photon.mockImplementation(
|
||||
() => new Promise((resolve) => setTimeout(() => resolve([photonRow()]), PHOTON_GRACE_MS + 100)),
|
||||
() =>
|
||||
new Promise((resolve) => setTimeout(() => resolve({ rows: [photonRow()], lead: null }), PHOTON_GRACE_MS + 100)),
|
||||
);
|
||||
|
||||
const result = await svc.autocompletePlaces(1, INPUT);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* MAPS-MERGE-001..008 — showing the index's answer and OpenStreetMap's together.
|
||||
* MAPS-MERGE-001..014 — showing the index's answer and OpenStreetMap's together.
|
||||
*
|
||||
* Why this exists, in numbers. 128 places out of a real trip to Japan, all
|
||||
* saved through the old search, replayed against the index: 51.6 percent came
|
||||
@@ -9,6 +9,7 @@
|
||||
* temple was one query away in OpenStreetMap.
|
||||
*/
|
||||
import { mergeSearchResults } from '../../../src/nest/maps/maps.helpers';
|
||||
import type { PhotonLead } from '../../../src/nest/maps/providers/photon.mapping';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
@@ -120,4 +121,37 @@ describe('mergeSearchResults', () => {
|
||||
const osm = [p('長谷寺', 35.3125, 139.5335, 'openstreetmap')];
|
||||
expect(mergeSearchResults(index, osm)).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('MAPS-MERGE-013: opens on the lead, as the suggestions do, and drops the rows of its name inside it', () => {
|
||||
const tokyo: PhotonLead = {
|
||||
placeId: 'relation:1543125',
|
||||
name: 'Tokyo',
|
||||
reach: 'everywhere',
|
||||
kind: 'province',
|
||||
lat: 35.68,
|
||||
lng: 139.76,
|
||||
extent: [135.85, 35.9, 154.2, 20.21],
|
||||
};
|
||||
const index = [
|
||||
{ ...p('Tokyo Tokyo', 14.5, 121, 'trek-places'), address: 'Philippines' },
|
||||
{ ...p('Tokyo', 35.69, 139.7, 'trek-places'), address: 'Shinjuku' },
|
||||
];
|
||||
const osm = [{ ...p('Tokyo', 35.68, 139.76, 'openstreetmap'), osm_id: 'relation:1543125' }];
|
||||
|
||||
expect(mergeSearchResults(index, osm, 10, { lead: tokyo }).map((r) => r.osm_id)).toEqual([
|
||||
'relation:1543125',
|
||||
'trek-places:Tokyo Tokyo',
|
||||
]);
|
||||
});
|
||||
|
||||
it('MAPS-MERGE-014: one row for the same element under both ids, and one for rows that read the same', () => {
|
||||
const index = [
|
||||
{ ...p('Kröpeliner Tor', 54.0893, 12.1325, 'openstreetmap'), osm_id: 'way:1' },
|
||||
{ ...p('Lidl', 54.1, 12.1, 'trek-places'), address: 'Rostock' },
|
||||
{ ...p('Lidl', 54.2, 12.2, 'trek-places'), address: 'Rostock', osm_id: 'gers:2' },
|
||||
];
|
||||
const osm = [{ ...p('Kröpeliner-Tor', 54.0894, 12.1326, 'openstreetmap'), osm_id: 'way:1' }];
|
||||
|
||||
expect(mergeSearchResults(index, osm).map((r) => r.osm_id)).toEqual(['way:1', 'trek-places:Lidl']);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -319,7 +319,13 @@ describe('MapsService.searchPlaces — the merged path', () => {
|
||||
|
||||
await make().searchPlaces(1, "L'Osteria", 'de', { lat: 54.0879, lng: 12.1408, radius: 5000 });
|
||||
|
||||
expect(mockSearch).toHaveBeenCalledWith("L'Osteria", { lat: 54.0879, lng: 12.1408, limit: 10, lang: 'de' });
|
||||
expect(mockSearch).toHaveBeenCalledWith("L'Osteria", {
|
||||
lat: 54.0879,
|
||||
lng: 12.1408,
|
||||
limit: 10,
|
||||
sources: 'index,osm',
|
||||
lang: 'de',
|
||||
});
|
||||
// Without the viewbox, "Hase-dera" answers with the temple in Nara rather
|
||||
// than the one the user is standing beside in Kamakura.
|
||||
const params = mockNominatim.mock.calls[0][1] as URLSearchParams;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* MAPS-SUGMERGE-001..004: the index's suggestions and Photon's in one list (#2877).
|
||||
* MAPS-SUGMERGE-001..010: the index's suggestions and Photon's in one list (#2877).
|
||||
*
|
||||
* The keystroke path asks the index with its OpenStreetMap layer and Photon at
|
||||
* once. Both carry OpenStreetMap places, so the same element can come back from
|
||||
@@ -7,6 +7,8 @@
|
||||
* Photon found.
|
||||
*/
|
||||
import { mergeSuggestions } from '../../../src/nest/maps/maps-index.helpers';
|
||||
import { LEAD_TOWN_REACH_M } from '../../../src/nest/maps/maps.helpers';
|
||||
import type { PhotonLead } from '../../../src/nest/maps/providers/photon.mapping';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
@@ -58,4 +60,87 @@ describe('mergeSuggestions', () => {
|
||||
expect(mergeSuggestions([], photon)).toHaveLength(8);
|
||||
expect(mergeSuggestions([], photon, 3).map((s) => s.placeId)).toEqual(['node:0', 'node:1', 'node:2']);
|
||||
});
|
||||
|
||||
// Measured on "Berlin": the index's first row is a business filed under the
|
||||
// city's name, Photon's first row is the city.
|
||||
const berlin: PhotonLead = {
|
||||
placeId: 'relation:62422',
|
||||
name: 'Berlin',
|
||||
reach: 'nearby',
|
||||
kind: 'city',
|
||||
lat: 52.5173885,
|
||||
lng: 13.3951309,
|
||||
extent: [13.088345, 52.6755087, 13.7611609, 52.3382448],
|
||||
};
|
||||
const berlinIndex = [
|
||||
row('gers:office', 'Berlin', 52.512, 13.32, 'trek-places'),
|
||||
row('node:484323', 'Berlin', 52.415, 13.197, 'openstreetmap'),
|
||||
row('node:21302157', 'Berlin-Tiergarten', 52.514, 13.336, 'openstreetmap'),
|
||||
];
|
||||
const berlinPhoton = [
|
||||
row('relation:62422', 'Berlin', 52.5173885, 13.3951309, 'openstreetmap'),
|
||||
row('relation:170184', 'Berlin', 44.4697, -71.1811, 'openstreetmap'),
|
||||
];
|
||||
|
||||
it('MAPS-SUGMERGE-005: opens on the lead and drops the rows of its name inside it', () => {
|
||||
const list = mergeSuggestions(berlinIndex, berlinPhoton, 8, { lead: berlin });
|
||||
|
||||
// The "Berlin" in Kleinmachnow lies outside the city, so it stays.
|
||||
expect(list.map((s) => s.placeId)).toEqual(['relation:62422', 'node:484323', 'node:21302157', 'relation:170184']);
|
||||
});
|
||||
|
||||
it('MAPS-SUGMERGE-006: a town leads only near where the traveller looks', () => {
|
||||
const tokyo = { lat: 35.68, lng: 139.76 };
|
||||
expect(mergeSuggestions(berlinIndex, berlinPhoton, 8, { lead: berlin, bias: tokyo })[0].placeId).toBe(
|
||||
'gers:office',
|
||||
);
|
||||
const potsdam = { lat: 52.39, lng: 13.06 };
|
||||
expect(mergeSuggestions(berlinIndex, berlinPhoton, 8, { lead: berlin, bias: potsdam })[0].placeId).toBe(
|
||||
'relation:62422',
|
||||
);
|
||||
expect(LEAD_TOWN_REACH_M).toBe(1_000_000);
|
||||
});
|
||||
|
||||
it('MAPS-SUGMERGE-007: a country leads from anywhere', () => {
|
||||
const mauritania: PhotonLead = {
|
||||
placeId: 'relation:192763',
|
||||
name: 'Mauritania',
|
||||
reach: 'everywhere',
|
||||
kind: 'country',
|
||||
lat: 20.25,
|
||||
lng: -10.33,
|
||||
extent: [-17.2380959, 27.314942, -4.8333344, 14.7209909],
|
||||
};
|
||||
const index = [row('gers:embassy', 'Islamische Republik Mauretanien', 52.51, 13.4, 'trek-places')];
|
||||
const photon = [row('relation:192763', 'Mauritania', 20.25, -10.33, 'openstreetmap')];
|
||||
|
||||
const list = mergeSuggestions(index, photon, 8, { lead: mauritania, bias: { lat: 52.5, lng: 13.4 } });
|
||||
expect(list.map((s) => s.placeId)).toEqual(['relation:192763', 'gers:embassy']);
|
||||
});
|
||||
|
||||
it("MAPS-SUGMERGE-008: leads with the layer's copy where Photon's was dropped as the same element", () => {
|
||||
const index = [row('gers:shop', 'Berlin Döner', 52.52, 13.4, 'trek-places'), berlinPhoton[0]];
|
||||
|
||||
const list = mergeSuggestions(index, berlinPhoton, 8, { lead: berlin });
|
||||
expect(list.map((s) => s.placeId)).toEqual(['relation:62422', 'gers:shop', 'relation:170184']);
|
||||
});
|
||||
|
||||
it('MAPS-SUGMERGE-009: shows rows that read the same once, and fills the list up behind them', () => {
|
||||
const branch = (id: string, lat: number) => ({
|
||||
...row(id, 'Tokyo Tokyo', lat, 121, 'trek-places'),
|
||||
secondaryText: 'Philippines',
|
||||
});
|
||||
const index = [branch('gers:1', 14.5), branch('gers:2', 14.6), branch('gers:3', 14.7)];
|
||||
const photon = [row('relation:1543125', 'Tokyo', 35.68, 139.76, 'openstreetmap')];
|
||||
|
||||
expect(mergeSuggestions(index, photon, 2).map((s) => s.placeId)).toEqual(['gers:1', 'relation:1543125']);
|
||||
});
|
||||
|
||||
it('MAPS-SUGMERGE-010: with nothing to say where the traveller is, a municipality does not lead', () => {
|
||||
const municipality: PhotonLead = { ...berlin, kind: 'municipality' };
|
||||
expect(mergeSuggestions(berlinIndex, berlinPhoton, 8, { lead: municipality })[0].placeId).toBe('gers:office');
|
||||
expect(
|
||||
mergeSuggestions(berlinIndex, berlinPhoton, 8, { lead: municipality, bias: { lat: 52.4, lng: 13.1 } })[0].placeId,
|
||||
).toBe('relation:62422');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* OSM-PHOTON-001..014: the OpenStreetMap search and reverse lookup ask Photon
|
||||
* first where the installation holds a key, answer in the shape Nominatim's
|
||||
* answers always had, and fall back to Nominatim whenever Photon gives none.
|
||||
* Lookups by id stay on Nominatim. The network is a stubbed global fetch
|
||||
* routed by host.
|
||||
* OSM-PHOTON-001..020: the OpenStreetMap search and reverse lookup are Photon's
|
||||
* where the installation holds a key, answer in the shape Nominatim's answers
|
||||
* always had, and leave Nominatim to installations without the TREK services.
|
||||
* A lookup by id is Overpass and Photon, Nominatim only where Overpass cannot
|
||||
* place the element. The network is a stubbed global fetch routed by host.
|
||||
*/
|
||||
import { deriveServices } from '../../../src/app-config/boot-derive';
|
||||
import { OsmClient } from '../../../src/nest/maps/providers/osm.client';
|
||||
@@ -33,6 +33,7 @@ const NOMINATIM_REVERSE = {
|
||||
|
||||
let identity: FakeServiceIdentity;
|
||||
let photonReply: () => Response;
|
||||
let overpassReply: () => Response;
|
||||
let osm: OsmClient;
|
||||
const hosts: string[] = [];
|
||||
|
||||
@@ -40,6 +41,20 @@ beforeEach(() => {
|
||||
identity = new FakeServiceIdentity();
|
||||
serviceIdentitySlot.install(identity);
|
||||
photonReply = () => photonAnswer([gateFeature()]);
|
||||
overpassReply = () =>
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
elements: [
|
||||
{
|
||||
type: 'way',
|
||||
id: 518071791,
|
||||
center: { lat: 52.5163, lon: 13.3777 },
|
||||
tags: { name: 'Brandenburger Tor', 'name:en': 'Brandenburg Gate', website: 'https://example.org' },
|
||||
},
|
||||
],
|
||||
}),
|
||||
{ status: 200, headers: { 'content-type': 'application/json' } },
|
||||
);
|
||||
hosts.length = 0;
|
||||
vi.stubEnv('NOMINATIM_URL', '');
|
||||
vi.stubGlobal(
|
||||
@@ -48,6 +63,7 @@ beforeEach(() => {
|
||||
const url = new URL(String(input));
|
||||
hosts.push(url.hostname);
|
||||
if (url.hostname === 'photon.liketrek.com') return photonReply();
|
||||
if (url.hostname === 'overpass-api.de') return overpassReply();
|
||||
const body = url.pathname.endsWith('/reverse') ? NOMINATIM_REVERSE : [NOMINATIM_GATE];
|
||||
return new Response(JSON.stringify(body), { status: 200, headers: { 'content-type': 'application/json' } });
|
||||
}),
|
||||
@@ -82,10 +98,15 @@ describe('search', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-002: asks Nominatim when Photon fails, with the bias it would have had', async () => {
|
||||
it('OSM-PHOTON-002: a Photon that fails is an empty answer where Photon is the geocoder; Nominatim only without the services', async () => {
|
||||
photonReply = () => photonAnswer([], 503);
|
||||
expect(await osm.searchNominatim('Brandenburger Tor', 'de', 'interactive', { lat: 52.5, lng: 13.4 })).toEqual([]);
|
||||
expect(hosts).toEqual(['photon.liketrek.com']);
|
||||
|
||||
hosts.length = 0;
|
||||
identity.bearer = null;
|
||||
const places = await osm.searchNominatim('Brandenburger Tor', 'de', 'interactive', { lat: 52.5, lng: 13.4 });
|
||||
expect(hosts).toEqual(['photon.liketrek.com', 'nominatim.openstreetmap.org']);
|
||||
expect(hosts).toEqual(['nominatim.openstreetmap.org']);
|
||||
expect(places[0]).toMatchObject({ osm_id: 'way:518071791' });
|
||||
});
|
||||
|
||||
@@ -95,26 +116,20 @@ describe('search', () => {
|
||||
expect(places.map((p) => p.osm_id)).toEqual(['node:7']);
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-010: asks Nominatim when Photon finds nothing usable, without holding it against Photon', async () => {
|
||||
it('OSM-PHOTON-010: an empty answer from Photon is the answer; Nominatim is not asked beside it', async () => {
|
||||
photonReply = () => photonAnswer([]);
|
||||
expect((await osm.searchNominatim('Brandenburger Tor', 'de'))[0]).toMatchObject({ osm_id: 'way:518071791' });
|
||||
expect(await osm.searchNominatim('Brandenburger Tor', 'de')).toEqual([]);
|
||||
photonReply = () => photonAnswer([gateFeature({ osm_type: undefined })]);
|
||||
expect((await osm.searchNominatim('Brandenburger Tor', 'de'))[0]).toMatchObject({ wikidata: 'Q82425' });
|
||||
photonReply = () => photonAnswer([gateFeature()]);
|
||||
await osm.searchNominatim('Brandenburger Tor', 'de');
|
||||
expect(hosts).toEqual([
|
||||
'photon.liketrek.com',
|
||||
'nominatim.openstreetmap.org',
|
||||
'photon.liketrek.com',
|
||||
'nominatim.openstreetmap.org',
|
||||
'photon.liketrek.com',
|
||||
]);
|
||||
expect(await osm.searchNominatim('Brandenburger Tor', 'de')).toEqual([]);
|
||||
expect(hosts).toEqual(['photon.liketrek.com', 'photon.liketrek.com']);
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-011: a bulk import on the background lane asks Nominatim alone', async () => {
|
||||
await osm.searchNominatim('Brandenburger Tor', 'de', 'background');
|
||||
it('OSM-PHOTON-011: a bulk import on the background lane asks Photon too, for its search and its reverse lookup', async () => {
|
||||
expect((await osm.searchNominatim('Brandenburger Tor', 'de', 'background'))[0]).toMatchObject({
|
||||
osm_id: 'way:518071791',
|
||||
});
|
||||
await osm.reverse('52.5', '13.4', 'de', { lane: 'background' });
|
||||
expect(hosts).toEqual(['nominatim.openstreetmap.org', 'nominatim.openstreetmap.org']);
|
||||
expect(hosts).toEqual(['photon.liketrek.com', 'photon.liketrek.com']);
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-012: carries the wiki tags of a Photon hit into the record', async () => {
|
||||
@@ -145,7 +160,7 @@ describe('search', () => {
|
||||
{
|
||||
placeId: 'way:518071791',
|
||||
mainText: 'Brandenburger Tor',
|
||||
secondaryText: 'Pariser Platz, Mitte, Berlin, 10117, Deutschland',
|
||||
secondaryText: 'Pariser Platz, Mitte, Berlin, Deutschland',
|
||||
source: 'openstreetmap',
|
||||
lat: 52.5163,
|
||||
lng: 13.3777,
|
||||
@@ -153,6 +168,67 @@ describe('search', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("OSM-PHOTON-015: hands the suggestion list Photon's first hit as its lead where it names a town", async () => {
|
||||
const town = gateFeature({
|
||||
osm_type: 'R',
|
||||
osm_id: 62422,
|
||||
osm_key: 'place',
|
||||
osm_value: 'city',
|
||||
type: 'city',
|
||||
name: 'Berlin',
|
||||
extent: [13.088345, 52.6755087, 13.7611609, 52.3382448],
|
||||
});
|
||||
photonReply = () => photonAnswer([town, gateFeature()]);
|
||||
const answer = await osm.suggestPhoton('Berl', 'de');
|
||||
expect(answer?.rows.map((r) => r.placeId)).toEqual(['relation:62422', 'way:518071791']);
|
||||
expect(answer?.lead).toMatchObject({ placeId: 'relation:62422', reach: 'nearby' });
|
||||
|
||||
// A sight with a Wikidata link leads too, when it is called what was typed.
|
||||
photonReply = () => photonAnswer([gateFeature(), town]);
|
||||
expect((await osm.suggestPhoton('Bran', 'de'))?.lead).toMatchObject({ placeId: 'way:518071791', kind: 'landmark' });
|
||||
expect((await osm.suggestPhoton('Pariser', 'de'))?.lead).toBeNull();
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-016: the search hands its records ranked and its lead, and null where Photon does not answer', async () => {
|
||||
const street = gateFeature({ osm_id: 1, osm_key: 'highway', osm_value: 'pedestrian', extra: undefined });
|
||||
photonReply = () => photonAnswer([street, gateFeature()]);
|
||||
const answer = await osm.searchPhoton('Brandenburger Tor', 'de');
|
||||
// The street of the same name steps back behind the sight with a Wikidata link.
|
||||
expect(answer?.records.map((r) => r.osm_id)).toEqual(['way:518071791', 'way:1']);
|
||||
expect(answer?.lead).toMatchObject({ placeId: 'way:518071791' });
|
||||
|
||||
identity.bearer = null;
|
||||
expect(await osm.searchPhoton('Brandenburger Tor', 'de')).toBeNull();
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-020: an airport code in capitals is asked as the airport at its place, and leads with the aerodrome', async () => {
|
||||
const asked: URLSearchParams[] = [];
|
||||
const aerodrome = gateFeature({
|
||||
osm_type: 'W',
|
||||
osm_id: 12345,
|
||||
osm_key: 'aeroway',
|
||||
osm_value: 'aerodrome',
|
||||
name: 'John F. Kennedy International Airport',
|
||||
});
|
||||
(aerodrome.geometry as { coordinates: number[] }).coordinates = [-73.7781, 40.6413];
|
||||
photonReply = () => photonAnswer([aerodrome]);
|
||||
const fetchStub = globalThis.fetch as unknown as ReturnType<typeof vi.fn>;
|
||||
const inner = fetchStub.getMockImplementation()!;
|
||||
fetchStub.mockImplementation(async (input: string | URL) => {
|
||||
asked.push(new URL(String(input)).searchParams);
|
||||
return inner(input);
|
||||
});
|
||||
|
||||
const answer = await osm.suggestPhoton('JFK', 'en');
|
||||
expect(asked[0].get('q')).toBe('John F. Kennedy International Airport');
|
||||
expect(asked[0].get('lat')).toBe('40.639447');
|
||||
expect(answer?.lead).toMatchObject({ placeId: 'way:12345', reach: 'everywhere', kind: 'airport' });
|
||||
|
||||
// Lower case is a word being typed, not a code.
|
||||
await osm.suggestPhoton('jfk', 'en');
|
||||
expect(asked[1].get('q')).toBe('jfk');
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-014: answers null without Photon rather than asking Nominatim, whose policy rules out autocomplete', async () => {
|
||||
identity.bearer = null;
|
||||
expect(await osm.autocompletePhoton('Brandenb', 'de')).toBeNull();
|
||||
@@ -176,30 +252,73 @@ describe('reverse', () => {
|
||||
expect((await osm.reverse('52.5163', '13.3777', 'de', { locality: true })).name).toBe('Berlin');
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-007: asks Nominatim when Photon fails or finds nothing there', async () => {
|
||||
photonReply = () => photonAnswer([], 429);
|
||||
expect(await osm.reverse('52.5', '13.4', 'de')).toEqual({
|
||||
name: 'Unter den Linden',
|
||||
address: 'Unter den Linden 77, Mitte, Berlin, Deutschland',
|
||||
});
|
||||
it('OSM-PHOTON-007: asks again within 50 km when nothing is next to the point, and never Nominatim beside Photon', async () => {
|
||||
const asked: string[] = [];
|
||||
photonReply = () => photonAnswer([]);
|
||||
expect((await osm.reverse('52.5', '13.4', 'de')).name).toBe('Unter den Linden');
|
||||
expect(hosts).toEqual([
|
||||
'photon.liketrek.com',
|
||||
'nominatim.openstreetmap.org',
|
||||
'photon.liketrek.com',
|
||||
'nominatim.openstreetmap.org',
|
||||
]);
|
||||
const fetchStub = globalThis.fetch as unknown as ReturnType<typeof vi.fn>;
|
||||
const inner = fetchStub.getMockImplementation()!;
|
||||
fetchStub.mockImplementation(async (input: string | URL) => {
|
||||
asked.push(new URL(String(input)).searchParams.get('radius') ?? '');
|
||||
return inner(input);
|
||||
});
|
||||
expect(await osm.reverse('46.0', '104.0', 'de')).toEqual({ name: null, address: null });
|
||||
expect(asked).toEqual(['', '50']);
|
||||
photonReply = () => photonAnswer([], 429);
|
||||
expect(await osm.reverse('52.5', '13.4', 'de')).toEqual({ name: null, address: null });
|
||||
expect(hosts).not.toContain('nominatim.openstreetmap.org');
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-008: the raw answer for a pasted link comes from Photon, else from Nominatim', async () => {
|
||||
it('OSM-PHOTON-008: the raw answer for a pasted link comes from Photon; a miss is an empty answer, not Nominatim', async () => {
|
||||
expect((await osm.reverseRaw(52.5163, 13.3777)).address?.tourism).toBe('Brandenburger Tor');
|
||||
photonReply = () => photonAnswer([], 500);
|
||||
expect(await osm.reverseRaw(52.5, 13.4)).toEqual(NOMINATIM_REVERSE);
|
||||
expect(await osm.reverseRaw(52.5, 13.4)).toEqual({});
|
||||
expect(hosts).not.toContain('nominatim.openstreetmap.org');
|
||||
});
|
||||
|
||||
it("OSM-PHOTON-017: without the TREK services the reverse lookup is Nominatim's, as it always was", async () => {
|
||||
identity.bearer = null;
|
||||
expect((await osm.reverse('52.5', '13.4', 'de')).name).toBe('Unter den Linden');
|
||||
expect(hosts).toEqual(['nominatim.openstreetmap.org']);
|
||||
});
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-009: a lookup by id stays on Nominatim', async () => {
|
||||
await osm.lookupNominatim('way', '518071791', 'de');
|
||||
expect(hosts).toEqual(['nominatim.openstreetmap.org']);
|
||||
it('OSM-PHOTON-009: a lookup by id is Overpass for the tags and the place, Photon for the name and address', async () => {
|
||||
const details = await osm.elementDetails('way:518071791', 'en');
|
||||
expect(details).toMatchObject({
|
||||
name: 'Brandenburg Gate',
|
||||
address: 'Brandenburger Tor, Pariser Platz, Mitte, Berlin, 10117, Deutschland',
|
||||
lat: 52.5163,
|
||||
lng: 13.3777,
|
||||
osm_id: 'way:518071791',
|
||||
website: 'https://example.org',
|
||||
});
|
||||
expect(hosts).toEqual(['overpass-api.de', 'photon.liketrek.com']);
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-018: Nominatim names the element only when Overpass cannot place it, or without the TREK services', async () => {
|
||||
overpassReply = () => new Response('busy', { status: 504 });
|
||||
await osm.elementDetails('way:518071791', 'de');
|
||||
expect(hosts.filter((h) => h === 'nominatim.openstreetmap.org')).toHaveLength(1);
|
||||
hosts.length = 0;
|
||||
identity.bearer = null;
|
||||
await osm.elementDetails('way:518071791', 'de');
|
||||
expect(hosts).toContain('nominatim.openstreetmap.org');
|
||||
expect(hosts).not.toContain('photon.liketrek.com');
|
||||
});
|
||||
|
||||
it('OSM-PHOTON-019: the identity of a place comes from Photon inside the box, the sight before the station of its name', async () => {
|
||||
const station = gateFeature({
|
||||
osm_type: 'N',
|
||||
osm_id: 3862767512,
|
||||
osm_key: 'railway',
|
||||
osm_value: 'station',
|
||||
extra: { wikidata: 'Q477185' },
|
||||
});
|
||||
photonReply = () => photonAnswer([station, gateFeature()]);
|
||||
const identityAnswer = await osm.resolveOsmIdentity('Brandenburger Tor', 52.5163, 13.3777, { lang: 'de' });
|
||||
expect(identityAnswer).toMatchObject({
|
||||
osmUrl: 'https://www.openstreetmap.org/way/518071791',
|
||||
tags: { wikidata: 'Q82425' },
|
||||
});
|
||||
expect(hosts).toEqual(['photon.liketrek.com']);
|
||||
});
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
/**
|
||||
* PHOTON-MAP-001..016: Photon's answers read into the shape Nominatim gives,
|
||||
* the rows of the suggestion list, and the language Photon is asked in.
|
||||
* PHOTON-MAP-001..029: Photon's answers read into the shape Nominatim gives,
|
||||
* the rows of the suggestion list, the lead the list opens on, and the
|
||||
* language Photon is asked in.
|
||||
*/
|
||||
import {
|
||||
asLead,
|
||||
editDistance,
|
||||
nameLike,
|
||||
pickLead,
|
||||
rankFeatures,
|
||||
asNominatimHit,
|
||||
asNominatimReverse,
|
||||
asSuggestion,
|
||||
@@ -128,7 +134,7 @@ describe('asSuggestion', () => {
|
||||
expect(asSuggestion(feature())).toEqual({
|
||||
placeId: 'way:518071791',
|
||||
mainText: 'Brandenburger Tor',
|
||||
secondaryText: 'Pariser Platz, Mitte, Berlin, 10117, Deutschland',
|
||||
secondaryText: 'Pariser Platz, Mitte, Berlin, Deutschland',
|
||||
source: 'openstreetmap',
|
||||
lat: 52.5163,
|
||||
lng: 13.3777,
|
||||
@@ -138,7 +144,7 @@ describe('asSuggestion', () => {
|
||||
it('PHOTON-MAP-015: keeps a name that holds a comma whole, and titles a house without one by its number', () => {
|
||||
const bar = asSuggestion(feature({ name: 'Bar, Café Einstein' }));
|
||||
expect(bar?.mainText).toBe('Bar, Café Einstein');
|
||||
expect(bar?.secondaryText).toBe('Pariser Platz, Mitte, Berlin, 10117, Deutschland');
|
||||
expect(bar?.secondaryText).toBe('Pariser Platz, Mitte, Berlin, Deutschland');
|
||||
|
||||
const house = asSuggestion(
|
||||
feature({ name: undefined, housenumber: '77', street: 'Unter den Linden', postcode: undefined }),
|
||||
@@ -155,3 +161,194 @@ describe('asSuggestion', () => {
|
||||
expect(asSuggestion(bare.features[0])).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('asSuggestion, the line below the name', () => {
|
||||
it('PHOTON-MAP-017: leaves out postcode and county, and names the region only where there is no town', () => {
|
||||
const village = asSuggestion(
|
||||
feature({
|
||||
osm_key: 'place',
|
||||
osm_value: 'village',
|
||||
name: 'Berlin',
|
||||
street: undefined,
|
||||
postcode: '457130',
|
||||
district: undefined,
|
||||
city: undefined,
|
||||
county: 'Троицкий муниципальный округ',
|
||||
state: 'Chelyabinsk Oblast',
|
||||
country: 'Russia',
|
||||
}),
|
||||
);
|
||||
expect(village?.secondaryText).toBe('Chelyabinsk Oblast, Russia');
|
||||
expect(asSuggestion(feature({ county: 'Berlin', postcode: '10117' }))?.secondaryText).toBe(
|
||||
'Pariser Platz, Mitte, Berlin, Deutschland',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('asLead', () => {
|
||||
const city = (properties: Record<string, unknown> = {}) =>
|
||||
feature({
|
||||
osm_type: 'R',
|
||||
osm_id: 62422,
|
||||
osm_key: 'place',
|
||||
osm_value: 'city',
|
||||
type: 'city',
|
||||
name: 'Berlin',
|
||||
street: undefined,
|
||||
postcode: undefined,
|
||||
district: undefined,
|
||||
city: undefined,
|
||||
extent: [13.088345, 52.6755087, 13.7611609, 52.3382448],
|
||||
...properties,
|
||||
});
|
||||
|
||||
it('PHOTON-MAP-018: reads a town as a lead from nearby, with its extent', () => {
|
||||
expect(asLead(city())).toEqual({
|
||||
placeId: 'relation:62422',
|
||||
name: 'Berlin',
|
||||
reach: 'nearby',
|
||||
kind: 'city',
|
||||
lat: 52.5163,
|
||||
lng: 13.3777,
|
||||
extent: [13.088345, 52.6755087, 13.7611609, 52.3382448],
|
||||
});
|
||||
});
|
||||
|
||||
it('PHOTON-MAP-019: reads a country or a region as a lead from everywhere, extent or none', () => {
|
||||
expect(asLead(city({ osm_value: 'country', type: 'country', name: 'Mauritania' }))?.reach).toBe('everywhere');
|
||||
// Tokyo is a province Photon files on its town layer.
|
||||
expect(asLead(city({ osm_value: 'province', name: 'Tokyo' }))?.reach).toBe('everywhere');
|
||||
expect(asLead(city({ extent: undefined }))?.extent).toBeNull();
|
||||
});
|
||||
|
||||
it('PHOTON-MAP-020: has no lead for a sight, a street or a quarter', () => {
|
||||
expect(asLead(feature())).toBeNull();
|
||||
expect(asLead(city({ osm_key: 'highway', osm_value: 'primary', type: 'street' }))).toBeNull();
|
||||
expect(asLead(city({ osm_value: 'suburb', type: 'district' }))).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('rankFeatures', () => {
|
||||
it('PHOTON-MAP-021: puts a place with a Wikidata link before its unlinked namesakes, Photon order otherwise', () => {
|
||||
const art = feature({
|
||||
osm_id: 1,
|
||||
osm_key: 'tourism',
|
||||
osm_value: 'artwork',
|
||||
extra: undefined,
|
||||
name: 'Neuschwanstein',
|
||||
});
|
||||
const castle = feature({ osm_id: 2, osm_key: 'tourism', osm_value: 'attraction', name: 'Schloss Neuschwanstein' });
|
||||
const street = feature({
|
||||
osm_id: 3,
|
||||
osm_key: 'highway',
|
||||
osm_value: 'residential',
|
||||
extra: undefined,
|
||||
name: 'Neuschwansteinstraße',
|
||||
});
|
||||
const shop = feature({
|
||||
osm_id: 4,
|
||||
osm_key: 'shop',
|
||||
osm_value: 'gift',
|
||||
extra: undefined,
|
||||
name: 'Neuschwanstein Souvenirs',
|
||||
});
|
||||
expect(rankFeatures([art, street, castle, shop]).map((f) => f.properties.osm_id)).toEqual([2, 1, 4, 3]);
|
||||
expect(rankFeatures([castle, art]).map((f) => f.properties.osm_id)).toEqual([2, 1]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pickLead', () => {
|
||||
const town = (properties: Record<string, unknown>) =>
|
||||
feature({
|
||||
osm_key: 'place',
|
||||
osm_value: 'city',
|
||||
type: 'city',
|
||||
street: undefined,
|
||||
district: undefined,
|
||||
city: undefined,
|
||||
...properties,
|
||||
});
|
||||
|
||||
it('PHOTON-MAP-022: a country or region first leads under any name, so an exonym keeps it', () => {
|
||||
const lead = pickLead(
|
||||
[town({ osm_type: 'R', osm_id: 192763, osm_value: 'country', type: 'country', name: 'Mauritania' })],
|
||||
'Mauretanien',
|
||||
);
|
||||
expect(lead).toMatchObject({ placeId: 'relation:192763', reach: 'everywhere' });
|
||||
});
|
||||
|
||||
it('PHOTON-MAP-023: anything else leads only when called what was typed, or a typo of it', () => {
|
||||
const centrum = town({
|
||||
osm_type: 'R',
|
||||
osm_id: 1,
|
||||
osm_key: 'boundary',
|
||||
osm_value: 'administrative',
|
||||
type: 'county',
|
||||
name: 'Centrum',
|
||||
});
|
||||
const paramaribo = town({ osm_type: 'N', osm_id: 2, name: 'Paramaribo' });
|
||||
expect(pickLead([centrum, paramaribo], 'Paramraibo')).toMatchObject({ placeId: 'node:2' });
|
||||
expect(pickLead([centrum], 'Paramraibo')).toBeNull();
|
||||
});
|
||||
|
||||
it('PHOTON-MAP-024: no lead for an airport code, nor for a municipality with nothing to say where the traveller is', () => {
|
||||
expect(pickLead([town({ name: 'FRA' })], 'FRA')).toBeNull();
|
||||
const municipality = town({ osm_value: 'municipality', name: 'Sagrada Família' });
|
||||
expect(pickLead([municipality], 'Sagrada Familia')).toBeNull();
|
||||
expect(pickLead([municipality], 'Sagrada Familia', { lat: -20, lng: -44 })).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('pickLead, namesakes', () => {
|
||||
const place = (properties: Record<string, unknown>) =>
|
||||
feature({ osm_key: 'place', street: undefined, district: undefined, city: undefined, ...properties });
|
||||
|
||||
it('PHOTON-MAP-027: a namesake ranked higher that may not lead leaves the list without a lead', () => {
|
||||
const london = place({ osm_type: 'R', osm_id: 1, osm_value: 'town', type: 'district', name: 'Greenwich' });
|
||||
const connecticut = place({ osm_type: 'R', osm_id: 2, osm_value: 'town', type: 'city', name: 'Greenwich' });
|
||||
expect(pickLead([london, connecticut], 'Greenwich')).toBeNull();
|
||||
expect(pickLead([connecticut, london], 'Greenwich')).toMatchObject({ placeId: 'relation:2' });
|
||||
});
|
||||
|
||||
it('PHOTON-MAP-029: a street of the name ranked higher does not block, and an island leads', () => {
|
||||
const street = place({
|
||||
osm_type: 'W',
|
||||
osm_id: 4,
|
||||
osm_key: 'highway',
|
||||
osm_value: 'primary',
|
||||
type: 'street',
|
||||
name: 'Spitzbergen',
|
||||
});
|
||||
const island = place({ osm_type: 'R', osm_id: 5, osm_value: 'island', type: 'other', name: 'Spitzbergen' });
|
||||
expect(pickLead([street, island], 'Spitzbergen')).toMatchObject({ placeId: 'relation:5', kind: 'island' });
|
||||
});
|
||||
|
||||
it('PHOTON-MAP-028: a town filed as an administrative boundary leads as a town', () => {
|
||||
const merida = place({
|
||||
osm_type: 'R',
|
||||
osm_id: 3,
|
||||
osm_key: 'boundary',
|
||||
osm_value: 'administrative',
|
||||
type: 'city',
|
||||
name: 'Mérida',
|
||||
});
|
||||
expect(pickLead([merida], 'Mérida')).toMatchObject({ placeId: 'relation:3', kind: 'city' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('nameLike and editDistance', () => {
|
||||
it('PHOTON-MAP-025: counts a swap of neighbours as one edit', () => {
|
||||
expect(editDistance('paramraibo', 'paramaribo')).toBe(2);
|
||||
expect(editDistance('tokio', 'tokyo')).toBe(1);
|
||||
expect(editDistance('ab', 'ba')).toBe(1);
|
||||
expect(editDistance('berlin', 'berlin')).toBe(0);
|
||||
});
|
||||
|
||||
it('PHOTON-MAP-026: takes the same name, its start, a word of it or a near typo, and nothing shorter than three letters', () => {
|
||||
expect(nameLike('Berl', 'Berlin')).toBe(true);
|
||||
expect(nameLike('Tor', 'Brandenburger Tor')).toBe(true);
|
||||
expect(nameLike('Tokio', 'Tokyo')).toBe(true);
|
||||
expect(nameLike('Mailand', 'Milan')).toBe(false);
|
||||
expect(nameLike('Be', 'Berlin')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user