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 the town around a view of one neighbourhood
Elian Doranff6300de6279
diff --git a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts b/apps/client/src/widgets/collections/geomap/nominatim.spec.tsindex 8cedabff4d..e86240f028 100644--- a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts+++ b/apps/client/src/widgets/collections/geomap/nominatim.spec.ts@@ -120,18 +120,18 @@ describe("Nominatim geocoding", () => {const inTheUnitedStates = place({ place_id: 2, name: "Jumbo", display_name: "Jumbo, Ohio, USA" });const fetchMock = respondInTurn([ [ inSibiu ], [ inTheUnitedStates, inSibiu ] ]);- const results = await search("jumbo", { viewport: [ [ 24.0, 45.7 ], [ 24.3, 45.9 ] ] });+ const results = await search("jumbo", { viewport: [ [ 23, 45 ], [ 25, 47 ] ] });// 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(nearby.get("viewbox")).toBe("24,45.7,24.3,45.9");+ expect(nearby.get("viewbox")).toBe("23,45,25,47");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("viewbox")).toBe("23,45,25,47");expect(elsewhere.get("bounded")).toBeNull();// What is at hand comes first, and is not offered twice for being in both answers.@@ -141,11 +141,31 @@ describe("Nominatim geocoding", () => {]);});+ it("searches the town around a view of one neighbourhood, not the neighbourhood alone", async () => {+ const fetchMock = respondWith([]);++ // A few streets of Sibiu, some 500 m across.+ await search("jumbo", { viewport: [ [ 24.147, 45.796 ], [ 24.153, 45.800 ] ] });++ const [ west, south, east, north ] = (requestedUrl(fetchMock).searchParams.get("viewbox") ?? "")+ .split(",").map(Number);++ // Grown about the same middle rather than shifted off it.+ expect((west + east) / 2).toBeCloseTo(24.15, 4);+ expect((south + north) / 2).toBeCloseTo(45.798, 4);++ // Wide enough to hold the town: a degree of latitude is about 111 km, so 25 km either way of+ // the middle is a little over 0.2 degrees.+ expect(north - south).toBeGreaterThan(0.4);+ // Longitude is shorter this far north, so the box is wider in degrees than it is tall.+ expect(east - west).toBeGreaterThan(north - south);+ });+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 ] ] });+ const results = await search("jumbo", { viewport: [ [ 23, 45 ], [ 25, 47 ] ] });expect(fetchMock).toHaveBeenCalledOnce();expect(results).toHaveLength(8);diff --git a/apps/client/src/widgets/collections/geomap/nominatim.ts b/apps/client/src/widgets/collections/geomap/nominatim.tsindex 88f985cc81..e8bbe7ff9b 100644--- a/apps/client/src/widgets/collections/geomap/nominatim.ts+++ b/apps/client/src/widgets/collections/geomap/nominatim.ts@@ -16,6 +16,19 @@ const POLYGON_THRESHOLD = 0.001;*/const OSM_TYPE_PREFIX: Record<string, string> = { relation: "R", way: "W" };+/**+ * The least ground a search treats as here, as a half-span in metres.+ *+ * A map zoomed into a neighbourhood shows a few hundred metres across, and a search restricted to+ * exactly that answers nothing for a shop three streets away. What the reader means by searching from+ * where they are standing is the town they are looking at, not the block.+ */+const MIN_SEARCH_RADIUS_M = 25_000;++/** Metres to a degree of latitude, the same the world over. A degree of longitude is this shortened+ * by the cosine of the latitude. */+const METRES_PER_DEGREE = 111_320;+/** Caps how many results one search returns. */const MAX_RESULTS = 8;@@ -151,8 +164,11 @@ function toBounds(boundingbox: string[] | undefined): GeoSearchResult["bounds"]/*** What the map is showing, as Nominatim reads a preferred area: two opposite corners, longitude- * first. Sent without `bounded`, so a place is ranked up for being in view rather than a place out- * of view being refused.+ * first. Sent without `bounded` on the wider pass, so a place is ranked up for being in view rather+ * than a place out of view being refused.+ *+ * Grown to at least {@link MIN_SEARCH_RADIUS_M} about its middle, since a view of one neighbourhood+ * restricts a search to that neighbourhood and answers nothing for the next street over.** A view running the other way round has been panned across the antimeridian, which no pair of* corners describes; nothing is sent for one, and the search is answered as if from nowhere.@@ -167,7 +183,23 @@ function toViewbox(viewport: GeoBounds | undefined) {return null;}- return [ west, south, east, north ].join(",");+ const middleLat = (south + north) / 2;+ const middleLng = (west + east) / 2;+ const latitudePad = MIN_SEARCH_RADIUS_M / METRES_PER_DEGREE;+ // A degree of longitude shortens towards the poles, where it is short enough that padding by one+ // would reach around the world; the cosine is floored to keep the padding finite.+ const longitudePad = latitudePad / Math.max(Math.cos(middleLat * Math.PI / 180), 0.01);++ return [+ clamp(Math.min(west, middleLng - longitudePad), -180, 180),+ clamp(Math.min(south, middleLat - latitudePad), -90, 90),+ clamp(Math.max(east, middleLng + longitudePad), -180, 180),+ clamp(Math.max(north, middleLat + latitudePad), -90, 90)+ ].map((degrees) => Number(degrees.toFixed(5))).join(",");+}++function clamp(value: number, least: number, most: number) {+ return Math.min(Math.max(value, least), most);}/**