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
feat(geomap): draw a searched place by what kind of place it is
Elian Doran510e5f29d1bf
diff --git a/apps/client/src/widgets/collections/geomap/PlaceMarker.tsx b/apps/client/src/widgets/collections/geomap/PlaceMarker.tsxindex 2570a5ed82..8dceb2ef25 100644--- a/apps/client/src/widgets/collections/geomap/PlaceMarker.tsx+++ b/apps/client/src/widgets/collections/geomap/PlaceMarker.tsx@@ -6,7 +6,7 @@ import { buildMarkerImage, LABEL_LAYOUT, LABEL_PAINT, MARKER_SHADOW_PADDING, mar/** The colour the pin is drawn in, so a searched place is not read as one of the map's own notes. */export const PLACE_MARKER_COLOR = "#E8833A";-/** The icon inside that pin, naming where it came from. */+/** The icon inside that pin, for a place whose kind the geocoder does not say. */export const PLACE_MARKER_ICON = "bx bx-search";export const PLACE_SOURCE = "searched-place";@@ -26,6 +26,8 @@ interface PlaceMarkerProps {name: string;/** Whether the map's style is a dark one, which decides how the label is drawn (see Markers). */isDarkTheme: boolean;+ /** A boxicons class saying what kind of place it is, drawn inside the pin (see osm_icons). */+ icon?: string;/** The ground the place covers, for one that is an area rather than a point. */outline?: GeoJSON.Geometry;}@@ -42,7 +44,7 @@ interface PlaceMarkerProps {* A place that covers ground rather than standing at a point — a country, a county — also has that* ground drawn under the pin, in the pin's own colour so the two read as one answer.*/-export default function PlaceMarker({ center, name, isDarkTheme, outline }: PlaceMarkerProps) {+export default function PlaceMarker({ center, name, isDarkTheme, icon, outline }: PlaceMarkerProps) {const parentMap = useContext(ParentMap);const styleLoaded = useContext(MapStyleLoaded);@@ -50,7 +52,8 @@ export default function PlaceMarker({ center, name, isDarkTheme, outline }: Placif (!parentMap) return;// Aliased so the narrowing above carries into the nested function below.const map = parentMap;- const imageId = markerImageId(PLACE_MARKER_COLOR, PLACE_MARKER_ICON);+ const iconClass = icon ?? PLACE_MARKER_ICON;+ const imageId = markerImageId(PLACE_MARKER_COLOR, iconClass);let cancelled = false;let image: HTMLImageElement | null = null;@@ -106,7 +109,7 @@ export default function PlaceMarker({ center, name, isDarkTheme, outline }: Plac}}- buildMarkerImage(PLACE_MARKER_COLOR, PLACE_MARKER_ICON).then((built) => {+ buildMarkerImage(PLACE_MARKER_COLOR, iconClass).then((built) => {if (cancelled) return;image = built;if (styleLoaded) {@@ -134,7 +137,7 @@ export default function PlaceMarker({ center, name, isDarkTheme, outline }: Plac// The map may already have been removed.}};- }, [ parentMap, styleLoaded, center, name, isDarkTheme ]);+ }, [ parentMap, styleLoaded, center, name, isDarkTheme, icon ]);useEffect(() => {if (!parentMap || !outline) return;diff --git a/apps/client/src/widgets/collections/geomap/PlacePanel.tsx b/apps/client/src/widgets/collections/geomap/PlacePanel.tsxindex 2e21f43029..590cda4468 100644--- a/apps/client/src/widgets/collections/geomap/PlacePanel.tsx+++ b/apps/client/src/widgets/collections/geomap/PlacePanel.tsx@@ -30,7 +30,7 @@ export default function PlacePanel({ place, isReadOnly, onAddMarker, onClose }:return (<OverlayPanelclassName="geo-place-panel"- header={<OverlayPanelTitle icon={PLACE_MARKER_ICON} text={place.name} />}+ header={<OverlayPanelTitle icon={place.icon ?? PLACE_MARKER_ICON} text={place.name} />}close={{ text: t("geo-map.close-details"), onClick: onClose }}><OverlayPanelBody className="geo-place-panel-body">diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.tsxindex e3f1a9c6e7..35d1102192 100644--- a/apps/client/src/widgets/collections/geomap/SearchBox.tsx+++ b/apps/client/src/widgets/collections/geomap/SearchBox.tsx@@ -13,7 +13,7 @@ import { formatDistance } from "../../../utils/units";import FormAutocomplete from "../../react/FormAutocomplete";import Icon from "../../react/Icon";import OverlayToolbar, { OverlayToolbarButton } from "../../react/OverlayToolbar";-import { DEFAULT_GEOCODING_PROVIDER_NAME, type GeoBounds, GEOCODING_PROVIDERS, type GeoSearchResult, SEARCH_RADIUS_M } from "./geocoding";+import { DEFAULT_GEOCODING_PROVIDER_NAME, DEFAULT_PLACE_ICON, type GeoBounds, GEOCODING_PROVIDERS, type GeoSearchResult, SEARCH_RADIUS_M } from "./geocoding";import { ParentMap } from "./map";import { LOCATION_ATTRIBUTE, parseLocation } from "./Markers";@@ -403,7 +403,7 @@ function placeEntry(result: GeoSearchResult): SearchEntry {kind: "place",key: `place:${result.id}`,label: result.label,- icon: "bx bx-map-pin",+ icon: result.icon ?? DEFAULT_PLACE_ICON,center: [ result.lng, result.lat ],result};diff --git a/apps/client/src/widgets/collections/geomap/geocoding.ts b/apps/client/src/widgets/collections/geomap/geocoding.tsindex 1d3296302a..30e78cc000 100644--- a/apps/client/src/widgets/collections/geomap/geocoding.ts+++ b/apps/client/src/widgets/collections/geomap/geocoding.ts@@ -24,6 +24,9 @@ export interface GeoSearchOptions {viewport?: GeoBounds;}+/** The icon for a place whose kind the geocoder says nothing about. */+export const DEFAULT_PLACE_ICON = "bx bx-map-pin";+/** A single place returned by a geocoder. */export interface GeoSearchResult {/** Unique within one result set. */@@ -32,6 +35,11 @@ export interface GeoSearchResult {label: string;/** The place's own name, for labelling it on the map where the full label would be a paragraph. */name: string;+ /**+ * A boxicons class saying what kind of place it is, as `FNote.getIcon()` gives one — a cart for a+ * supermarket, a flag for a country. Absent where the provider says nothing about the kind.+ */+ icon?: string;lat: number;lng: number;/**diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsxindex 03bb0f6426..fac63bf6cf 100644--- a/apps/client/src/widgets/collections/geomap/index.tsx+++ b/apps/client/src/widgets/collections/geomap/index.tsx@@ -305,7 +305,7 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM<SearchBox notes={notes} onPickPlace={pickPlace} />{pickedPlace && placeCenter && <><PlaceMarker- center={placeCenter} name={pickedPlace.name}+ center={placeCenter} name={pickedPlace.name} icon={pickedPlace.icon}outline={placeOutline} isDarkTheme={layerData.isDarkTheme ?? false}/><PlacePaneldiff --git a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts b/apps/client/src/widgets/collections/geomap/nominatim.spec.tsindex aa39a0cf57..58ccccb866 100644--- a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts+++ b/apps/client/src/widgets/collections/geomap/nominatim.spec.ts@@ -96,17 +96,28 @@ describe("Nominatim geocoding", () => {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)+ icon: undefined, outline: expect.any(Function)},// 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: "N17807753", label: "Paris, France", name: "Paris", lat: 48.8566, lng: 2.3522,- bounds: undefined, outline: undefined+ bounds: undefined, icon: undefined, outline: undefined}]);});+ it("carries the kind of place through, for the icon it is drawn with", async () => {+ respondWith([+ place({ category: "shop", type: "supermarket", addresstype: "shop" }),+ place({ osm_id: 62423, display_name: "Berlin, Germany, the city", category: "boundary", type: "administrative", addresstype: "city" })+ ]);++ const [ shop, city ] = await search("berlin");+ expect(shop.icon).toBe("bx bx-cart");+ expect(city.icon).toBe("bx bx-buildings");+ });+it("reports a refused request rather than answering with no places", async () => {respondWith([], { ok: false, status: 429 });diff --git a/apps/client/src/widgets/collections/geomap/nominatim.ts b/apps/client/src/widgets/collections/geomap/nominatim.tsindex ba9a681601..31bc8ef7c6 100644--- a/apps/client/src/widgets/collections/geomap/nominatim.ts+++ b/apps/client/src/widgets/collections/geomap/nominatim.ts@@ -1,5 +1,6 @@import { getCurrentLanguage } from "../../../services/i18n";import { type GeoBounds, type GeocodingProvider, type GeoSearchOptions, type GeoSearchResult, SEARCH_RADIUS_M } from "./geocoding";+import { placeIcon } from "./osm_icons";const NOMINATIM_URL = "https://nominatim.openstreetmap.org/search";const NOMINATIM_LOOKUP_URL = "https://nominatim.openstreetmap.org/lookup";@@ -135,6 +136,12 @@ interface NominatimPlace {lon?: string;/** `[south, north, west, east]`, as strings, which is the order Nominatim writes it in. */boundingbox?: string[];+ /** The OSM key the place is filed under: `shop`, `amenity`, `boundary`. */+ category?: string;+ /** The OSM value under that key: `supermarket`, `restaurant`, `administrative`. */+ type?: string;+ /** What kind of address the place is: `city`, `country`, `road`, `shop`. */+ addresstype?: string;}/** A place as the app holds one, or `null` where the entry carries no readable position. */@@ -162,6 +169,7 @@ function toSearchResult(place: NominatimPlace): GeoSearchResult | null {lat,lng,bounds,+ icon: placeIcon({ category: place.category, type: place.type, addressType: place.addresstype }),outline: osmId && hasBoundary ? () => fetchOutline(osmId, bounds) : undefined};}diff --git a/apps/client/src/widgets/collections/geomap/osm_icons.spec.ts b/apps/client/src/widgets/collections/geomap/osm_icons.spec.tsnew file mode 100644index 0000000000..474b5dcb20--- /dev/null+++ b/apps/client/src/widgets/collections/geomap/osm_icons.spec.ts@@ -0,0 +1,35 @@+/** Which icon a place is drawn with, from how OpenStreetMap classifies it. */+import { describe, expect, it } from "vitest";++import { placeIcon } from "./osm_icons";++describe("Place icons", () => {+ it("draws a place by its own kind, where OSM names one", () => {+ expect(placeIcon({ category: "shop", type: "supermarket", addressType: "shop" })).toBe("bx bx-cart");+ expect(placeIcon({ category: "amenity", type: "cafe", addressType: "amenity" })).toBe("bx bx-coffee");+ expect(placeIcon({ category: "amenity", type: "parking", addressType: "amenity" })).toBe("bx bxs-parking");+ expect(placeIcon({ category: "natural", type: "volcano", addressType: "volcano" })).toBe("bx bx-landscape");+ expect(placeIcon({ category: "aeroway", type: "aerodrome", addressType: "aeroway" })).toBe("bx bx-paper-plane");+ });++ it("tells administrative areas apart by their address type, which is all that distinguishes them", () => {+ // Every one of these is filed under the same category and type.+ const boundary = { category: "boundary", type: "administrative" };++ expect(placeIcon({ ...boundary, addressType: "country" })).toBe("bx bx-flag");+ expect(placeIcon({ ...boundary, addressType: "county" })).toBe("bx bx-map-alt");+ expect(placeIcon({ ...boundary, addressType: "city" })).toBe("bx bx-buildings");+ expect(placeIcon({ ...boundary, addressType: "village" })).toBe("bx bx-home-alt");+ expect(placeIcon({ ...boundary, addressType: "suburb" })).toBe("bx bx-building-house");+ });++ it("falls back on what a place broadly is, then on nothing at all", () => {+ // A trade no table names is still a shop, and a road is still a road.+ expect(placeIcon({ category: "shop", type: "locksmith", addressType: "shop" })).toBe("bx bx-store");+ expect(placeIcon({ category: "highway", type: "secondary", addressType: "road" })).toBe("bx bx-directions");++ // Nothing here claims these, so the caller falls back on DEFAULT_PLACE_ICON in their stead.+ expect(placeIcon({ category: "sport", type: "curling" })).toBeUndefined();+ expect(placeIcon({})).toBeUndefined();+ });+});diff --git a/apps/client/src/widgets/collections/geomap/osm_icons.ts b/apps/client/src/widgets/collections/geomap/osm_icons.tsnew file mode 100644index 0000000000..9d0356f990--- /dev/null+++ b/apps/client/src/widgets/collections/geomap/osm_icons.ts@@ -0,0 +1,173 @@+/**+ * The icon a place found by searching wears, taken from how OpenStreetMap classifies it.+ *+ * The tables here speak OSM's vocabulary, which is what geocoders built on OSM data report — see+ * nominatim.ts, the only caller. A geocoder classifying places its own way, by Maki icon name or by+ * a list of Google place types, maps that vocabulary itself and fills in `GeoSearchResult.icon`.+ *+ * OSM describes a place with a key and a value — `shop`=`supermarket`, `natural`=`volcano` — which a+ * geocoder passes on as a category and a type. Every administrative area from a hamlet to a country+ * shares one pair, `boundary`=`administrative`, so what tells those apart is the address type the+ * geocoder works out alongside it.+ *+ * A pair the tables below do not name is still drawn by its category, a shop as a shop; what not even+ * that claims is left without an icon, for the caller to fall back on DEFAULT_PLACE_ICON.+ */++/** How a geocoder classifies a place, in the terms OpenStreetMap uses. */+export interface OsmClassification {+ /** The OSM key: `shop`, `amenity`, `natural`, `boundary`. */+ category?: string;+ /** The OSM value: `supermarket`, `restaurant`, `volcano`, `administrative`. */+ type?: string;+ /**+ * What kind of address the place is: `city`, `country`, `road`, `shop`. Worked out by the+ * geocoder rather than tagged in OSM, so one that reports no such thing leaves it out.+ */+ addressType?: string;+}++/**+ * The boxicons class for a place, from the most specific of its classifications that is known, or+ * nothing where none of them says what kind of place it is.+ *+ * The address type is read before the bare category so that a city is drawn as a city rather than as+ * the boundary it is filed under.+ */+export function placeIcon({ category, type, addressType }: OsmClassification): string | undefined {+ const icon = BY_CATEGORY_AND_TYPE[`${category}/${type}`]+ ?? BY_ADDRESS_TYPE[addressType ?? ""]+ ?? BY_CATEGORY[category ?? ""];++ return icon ? `bx ${icon}` : undefined;+}++/** What a place is, where its own kind is worth drawing: `<key>/<value>`, as OSM tags it. */+const BY_CATEGORY_AND_TYPE: Record<string, string> = {+ "shop/supermarket": "bx-cart",+ "shop/convenience": "bx-basket",+ "shop/bakery": "bx-cake",+ "shop/mall": "bx-shopping-bag",+ "shop/department_store": "bx-shopping-bag",++ "amenity/restaurant": "bx-restaurant",+ "amenity/fast_food": "bx-bowl-hot",+ "amenity/cafe": "bx-coffee",+ "amenity/bar": "bx-beer",+ "amenity/pub": "bx-beer",+ "amenity/biergarten": "bx-beer",+ "amenity/bank": "bx-money",+ "amenity/atm": "bx-credit-card",+ "amenity/pharmacy": "bx-plus-medical",+ "amenity/hospital": "bx-clinic",+ "amenity/clinic": "bx-clinic",+ "amenity/doctors": "bx-clinic",+ "amenity/school": "bx-book",+ "amenity/kindergarten": "bx-book",+ "amenity/college": "bx-book",+ "amenity/university": "bx-book",+ "amenity/library": "bx-library",+ "amenity/police": "bx-shield",+ "amenity/fire_station": "bx-first-aid",+ "amenity/post_office": "bx-envelope",+ "amenity/fuel": "bx-gas-pump",+ "amenity/parking": "bxs-parking",+ "amenity/place_of_worship": "bx-church",+ "amenity/cinema": "bx-movie",+ "amenity/theatre": "bx-movie",+ "amenity/bus_station": "bx-bus",++ "tourism/hotel": "bx-hotel",+ "tourism/motel": "bx-hotel",+ "tourism/hostel": "bx-hotel",+ "tourism/guest_house": "bx-hotel",+ "tourism/apartment": "bx-hotel",+ "tourism/museum": "bx-library",+ "tourism/gallery": "bx-palette",+ "tourism/artwork": "bx-palette",+ "tourism/camp_site": "bx-leaf",++ "leisure/park": "bx-leaf",+ "leisure/garden": "bx-leaf",+ "leisure/nature_reserve": "bx-leaf",+ "leisure/pitch": "bx-football",+ "leisure/stadium": "bx-football",+ "leisure/sports_centre": "bx-football",+ "leisure/swimming_pool": "bx-swim",+ "leisure/fitness_centre": "bx-dumbbell",++ "natural/peak": "bx-landscape",+ "natural/volcano": "bx-landscape",+ "natural/water": "bx-water",+ "natural/bay": "bx-water",+ "natural/spring": "bx-water",+ "natural/wood": "bx-leaf",+ "natural/tree": "bx-leaf",+ "natural/beach": "bx-sun",++ "railway/station": "bx-train",+ "railway/halt": "bx-train",+ "railway/tram_stop": "bx-train",+ "railway/subway_entrance": "bx-train",++ "highway/bus_stop": "bx-bus",+ "highway/footway": "bx-walk",+ "highway/path": "bx-walk",+ "highway/pedestrian": "bx-walk",+ "highway/cycleway": "bx-cycling",++ "aeroway/aerodrome": "bx-paper-plane",+ "aeroway/terminal": "bx-paper-plane"+};++/** How big a place is, for the areas OSM files under one category and type. */+const BY_ADDRESS_TYPE: Record<string, string> = {+ "continent": "bx-world",+ "country": "bx-flag",+ "state": "bx-map-alt",+ "province": "bx-map-alt",+ "region": "bx-map-alt",+ "county": "bx-map-alt",+ "district": "bx-map-alt",+ "municipality": "bx-map-alt",+ "island": "bx-landscape",+ "islet": "bx-landscape",+ "city": "bx-buildings",+ "town": "bx-buildings",+ "village": "bx-home-alt",+ "hamlet": "bx-home-alt",+ "isolated_dwelling": "bx-home-alt",+ "borough": "bx-building-house",+ "city_district": "bx-building-house",+ "suburb": "bx-building-house",+ "neighbourhood": "bx-building-house",+ "quarter": "bx-building-house",+ "road": "bx-directions",+ "house": "bx-home",+ "building": "bx-home",+ "residential": "bx-home",+ "postcode": "bx-envelope"+};++/** What a place broadly is, for a kind too rare to name above. */+const BY_CATEGORY: Record<string, string> = {+ "shop": "bx-store",+ "amenity": "bx-building",+ "tourism": "bx-camera",+ "leisure": "bx-football",+ "natural": "bx-leaf",+ "waterway": "bx-water",+ "highway": "bx-directions",+ "railway": "bx-train",+ "aeroway": "bx-paper-plane",+ "building": "bx-building",+ "historic": "bx-library",+ "office": "bx-briefcase",+ "craft": "bx-wrench",+ "man_made": "bx-wrench",+ "healthcare": "bx-plus-medical",+ "military": "bx-shield",+ "emergency": "bx-first-aid",+ "landuse": "bx-map-alt",+ "boundary": "bx-map-alt"+};