Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(geomap): search what the map is showing before the wider world
Elian Doran39bd24b2e2f1
diff --git a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts b/apps/client/src/widgets/collections/geomap/nominatim.spec.tsindex df4667a345..8cedabff4d 100644--- a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts+++ b/apps/client/src/widgets/collections/geomap/nominatim.spec.ts@@ -34,6 +34,19 @@ function respondWith(body: unknown, init: { ok?: boolean; status?: number } = {}return fetchMock;}+/** Answers each request in turn, for a search that makes more than one (see the two passes). */+function respondInTurn(bodies: unknown[]) {+ let call = 0;+ const fetchMock = vi.fn(async (_url: string) => ({+ ok: true,+ status: 200,+ statusText: "",+ json: async () => bodies[Math.min(call++, bodies.length - 1)]+ }));+ vi.stubGlobal("fetch", fetchMock);+ return fetchMock;+}+/** Runs a search, letting the rate limiter's wait elapse rather than sitting through it. */async function search(query: string, options?: Parameters<typeof provider.search>[1]) {const results = provider.search(query, options);@@ -41,8 +54,8 @@ async function search(query: string, options?: Parameters<typeof provider.searchreturn results;}-function requestedUrl(fetchMock: ReturnType<typeof respondWith>) {- return new URL(fetchMock.mock.calls[0][0]);+function requestedUrl(fetchMock: ReturnType<typeof respondWith>, call = 0) {+ return new URL(fetchMock.mock.calls[call][0]);}beforeEach(() => vi.useFakeTimers());@@ -102,27 +115,53 @@ describe("Nominatim geocoding", () => {await refused;});- it("prefers what the map is showing, without refusing what lies outside it", async () => {- const fetchMock = respondWith([]);+ it("asks what the map is showing first, then the wider world", async () => {+ const inSibiu = place({ place_id: 1, name: "Jumbo", display_name: "Jumbo, Sibiu, Romania" });+ const inTheUnitedStates = place({ place_id: 2, name: "Jumbo", display_name: "Jumbo, Ohio, USA" });+ const fetchMock = respondInTurn([ [ inSibiu ], [ inTheUnitedStates, inSibiu ] ]);- await search("jumbo", { viewport: [ [ 19.8, 39.5 ], [ 20.1, 39.8 ] ] });+ const results = await search("jumbo", { viewport: [ [ 24.0, 45.7 ], [ 24.3, 45.9 ] ] });+ // Restricted to the view first: a viewbox alone is only a nudge next to how well known a+ // place is, so a shop in Sibiu would otherwise stay under every Jumbo in the United States.+ const nearby = requestedUrl(fetchMock, 0).searchParams;// Two opposite corners, longitude first, as Nominatim reads a preferred area.- expect(requestedUrl(fetchMock).searchParams.get("viewbox")).toBe("19.8,39.5,20.1,39.8");- // A preference, not a restriction: `bounded` would answer nothing where the only match is- // somewhere else entirely.- expect(requestedUrl(fetchMock).searchParams.get("bounded")).toBeNull();+ expect(nearby.get("viewbox")).toBe("24,45.7,24.3,45.9");+ expect(nearby.get("bounded")).toBe("1");++ // Then unrestricted, so a place nowhere near the map is still found.+ const elsewhere = requestedUrl(fetchMock, 1).searchParams;+ expect(elsewhere.get("viewbox")).toBe("24,45.7,24.3,45.9");+ expect(elsewhere.get("bounded")).toBeNull();++ // What is at hand comes first, and is not offered twice for being in both answers.+ expect(results.map((result) => result.label)).toEqual([+ "Jumbo, Sibiu, Romania",+ "Jumbo, Ohio, USA"+ ]);+ });++ it("does not go looking further afield where the view already fills the list", async () => {+ const local = Array.from({ length: 8 }, (_, index) => place({ place_id: index, display_name: `Jumbo ${index}` }));+ const fetchMock = respondInTurn([ local, [] ]);++ const results = await search("jumbo", { viewport: [ [ 24.0, 45.7 ], [ 24.3, 45.9 ] ] });++ expect(fetchMock).toHaveBeenCalledOnce();+ expect(results).toHaveLength(8);});- it("asks from nowhere in particular where the view says nothing usable", async () => {+ it("asks once, from nowhere in particular, where the view says nothing usable", async () => {const fromNowhere = respondWith([]);await search("jumbo");+ expect(fromNowhere).toHaveBeenCalledOnce();expect(requestedUrl(fromNowhere).searchParams.get("viewbox")).toBeNull();// Panned across the antimeridian, where the view runs the other way round and no pair of// corners describes it.const wrapped = respondWith([]);await search("jumbo", { viewport: [ [ 170, 39.5 ], [ -170, 39.8 ] ] });+ expect(wrapped).toHaveBeenCalledOnce();expect(requestedUrl(wrapped).searchParams.get("viewbox")).toBeNull();});diff --git a/apps/client/src/widgets/collections/geomap/nominatim.ts b/apps/client/src/widgets/collections/geomap/nominatim.tsindex c583b74331..88f985cc81 100644--- a/apps/client/src/widgets/collections/geomap/nominatim.ts+++ b/apps/client/src/widgets/collections/geomap/nominatim.ts@@ -38,6 +38,9 @@ let nextRequestAt = 0;** The same policy rules out searching as the user types, which is why a search runs only when the* user asks for one (see SearchBox).+ *+ * A search is answered in two passes where the map says what it is showing, so that what is at hand+ * comes first and what is far off still comes at all (see searchNominatim).*/export const nominatim: GeocodingProvider = {name: "Nominatim (OpenStreetMap)",@@ -45,10 +48,31 @@ export const nominatim: GeocodingProvider = {};async function searchNominatim(query: string, { viewport }: GeoSearchOptions = {}): Promise<GeoSearchResult[]> {+ const viewbox = toViewbox(viewport);++ // What the map is showing, and nothing else. A viewbox on its own is only a nudge next to how+ // well known a place is, so a supermarket in the town on screen stays buried under the towns of+ // that name across the world — restricting the search to the view is what brings it up.+ const nearby = viewbox ? await searchPlaces(query, { viewbox, bounded: "1" }) : [];+ if (nearby.length >= MAX_RESULTS) {+ return nearby;+ }++ // Then the wider world, so that a place nowhere near the map is still found, and a view holding+ // one match is not the whole answer. What is at hand stands above it either way.+ const elsewhere = await searchPlaces(query, viewbox ? { viewbox } : {});+ const nearbyIds = new Set(nearby.map((place) => place.id));++ return [ ...nearby, ...elsewhere.filter((place) => !nearbyIds.has(place.id)) ].slice(0, MAX_RESULTS);+}++/** One request to Nominatim's search, asked in the language the app runs in. */+async function searchPlaces(query: string, extraParams: Record<string, string>): Promise<GeoSearchResult[]> {const params = new URLSearchParams({q: query,format: "jsonv2",- limit: String(MAX_RESULTS)+ limit: String(MAX_RESULTS),+ ...extraParams});// Place names in the language the app runs in, where Nominatim holds one. Trilium writes some@@ -58,11 +82,6 @@ async function searchNominatim(query: string, { viewport }: GeoSearchOptions = {params.set("accept-language", language.replace("_", "-"));}- const viewbox = toViewbox(viewport);- if (viewbox) {- params.set("viewbox", viewbox);- }-await waitForRequestSlot();const response = await fetch(`${NOMINATIM_URL}?${params}`);if (!response.ok) {