Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): offer a searched place once, not once per pass

Elian Doran0aa390efa549
diff --git a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts b/apps/client/src/widgets/collections/geomap/nominatim.spec.ts
index e86240f028..c15611066b 100644
--- a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts
+++ b/apps/client/src/widgets/collections/geomap/nominatim.spec.ts
@@ -92,7 +92,8 @@ describe("Nominatim geocoding", () => {
expect(await search("anywhere")).toEqual([
{
- id: "240109189", label: "Berlin, Germany", name: "Berlin", lat: 52.5170365, lng: 13.3888599,
+ // What OSM calls it, not what Nominatim numbered it.
+ id: "R62422", label: "Berlin, Germany", name: "Berlin", lat: 52.5170365, lng: 13.3888599,
// Written south-north-west-east by Nominatim, and read as MapLibre frames one.
bounds: [ [ 13.088345, 52.3382448 ], [ 13.7611609, 52.6755087 ] ],
outline: expect.any(Function)
@@ -100,7 +101,7 @@ describe("Nominatim geocoding", () => {
// Named by the leading part of its label, Nominatim having named it nothing itself, and
// framed by nothing: its extent is unreadable, and a node has no boundary to fetch.
{
- id: "4", label: "Paris, France", name: "Paris", lat: 48.8566, lng: 2.3522,
+ id: "N17807753", label: "Paris, France", name: "Paris", lat: 48.8566, lng: 2.3522,
bounds: undefined, outline: undefined
}
]);
@@ -116,8 +117,8 @@ describe("Nominatim geocoding", () => {
});
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 inSibiu = place({ osm_type: "node", osm_id: 1, name: "Jumbo", display_name: "Jumbo, Sibiu, Romania" });
+ const inTheUnitedStates = place({ osm_type: "node", osm_id: 2, name: "Jumbo", display_name: "Jumbo, Ohio, USA" });
const fetchMock = respondInTurn([ [ inSibiu ], [ inTheUnitedStates, inSibiu ] ]);
const results = await search("jumbo", { viewport: [ [ 23, 45 ], [ 25, 47 ] ] });
@@ -161,6 +162,25 @@ describe("Nominatim geocoding", () => {
expect(east - west).toBeGreaterThan(north - south);
});
+ it("offers a place once, however many ways the two passes name it", async () => {
+ const shop = { osm_type: "node", osm_id: 5, name: "Jumbo", display_name: "Jumbo, Sibiu, Romania" };
+ const fetchMock = respondInTurn([
+ // Nominatim's own numbering differs between the instances the public service runs behind,
+ // so the same shop comes back under two place ids.
+ [ place({ ...shop, place_id: 111 }) ],
+ [
+ place({ ...shop, place_id: 222 }),
+ // The building around the shop: another OSM object under the same name and address.
+ place({ ...shop, place_id: 333, osm_type: "way", osm_id: 6 })
+ ]
+ ]);
+
+ const results = await search("jumbo", { viewport: [ [ 23, 45 ], [ 25, 47 ] ] });
+
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(results.map((result) => result.id)).toEqual([ "N5" ]);
+ });
+
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, [] ]);
diff --git a/apps/client/src/widgets/collections/geomap/nominatim.ts b/apps/client/src/widgets/collections/geomap/nominatim.ts
index e8bbe7ff9b..2250e3ae75 100644
--- a/apps/client/src/widgets/collections/geomap/nominatim.ts
+++ b/apps/client/src/widgets/collections/geomap/nominatim.ts
@@ -10,11 +10,11 @@ const NOMINATIM_LOOKUP_URL = "https://nominatim.openstreetmap.org/lookup";
*/
const POLYGON_THRESHOLD = 0.001;
-/**
- * How a lookup names an OSM object, from how a search result names one. Nodes are left out: a place
- * that is a single point has no boundary, and its pin already says where it is.
- */
-const OSM_TYPE_PREFIX: Record<string, string> = { relation: "R", way: "W" };
+/** How a lookup names an OSM object, from how a search result names one. */
+const OSM_TYPE_PREFIX: Record<string, string> = { node: "N", way: "W", relation: "R" };
+
+/** The kinds of OSM object that can enclose ground. A node is a single point and has no boundary. */
+const OSM_TYPES_WITH_A_BOUNDARY = new Set([ "way", "relation" ]);
/**
* The least ground a search treats as here, as a half-span in metres.
@@ -74,9 +74,30 @@ async function searchNominatim(query: string, { viewport }: GeoSearchOptions = {
// 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);
+ return dedupe([ ...nearby, ...elsewhere ]).slice(0, MAX_RESULTS);
+}
+
+/**
+ * Drops a place already offered, keeping the first of them — which is the nearest, the passes running
+ * that way round.
+ *
+ * By what it says as well as by which place it is. A shop and the building around it are two OSM
+ * objects under one name and one address, and two rows a reader cannot tell apart are one answer
+ * given twice.
+ */
+function dedupe(places: GeoSearchResult[]): GeoSearchResult[] {
+ const seen = new Set<string>();
+
+ return places.filter((place) => {
+ if (seen.has(place.id) || seen.has(place.label)) {
+ return false;
+ }
+
+ seen.add(place.id);
+ seen.add(place.label);
+ return true;
+ });
}
/** One request to Nominatim's search, asked in the language the app runs in. */
@@ -128,9 +149,13 @@ function toSearchResult(place: NominatimPlace): GeoSearchResult | null {
const prefix = OSM_TYPE_PREFIX[place.osm_type ?? ""];
const osmId = prefix && place.osm_id ? `${prefix}${place.osm_id}` : null;
+ const hasBoundary = OSM_TYPES_WITH_A_BOUNDARY.has(place.osm_type ?? "");
return {
- id: String(place.place_id ?? `${lat},${lng}`),
+ // What OSM calls the place, where it says: `place_id` is Nominatim's own numbering, and the
+ // public service runs several instances that number independently, so the same place comes
+ // back under two of them and is offered twice.
+ id: osmId ?? String(place.place_id ?? `${lat},${lng}`),
label: place.display_name,
// Nominatim leaves `name` empty for a result that is an address rather than a named place,
// where the leading part of the label is what names it.
@@ -138,7 +163,7 @@ function toSearchResult(place: NominatimPlace): GeoSearchResult | null {
lat,
lng,
bounds: toBounds(place.boundingbox),
- outline: osmId ? () => fetchOutline(osmId) : undefined
+ outline: osmId && hasBoundary ? () => fetchOutline(osmId) : undefined
};
}