Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/nominatimGeocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const ADDRESS_TYPE_MAP = Object.freeze({
region: ['administrative_area_level_1', 'political'],
county: ['administrative_area_level_2', 'political'],
district: ['administrative_area_level_2', 'political'],
subdivision: ['administrative_area_level_2', 'political'],
municipality: ['locality', 'political'],
city: ['locality', 'political'],
town: ['locality', 'political'],
Expand All @@ -24,6 +25,7 @@ const ADDRESS_TYPE_MAP = Object.freeze({
suburb: ['neighborhood', 'political'],
neighbourhood: ['neighborhood', 'political'],
neighborhood: ['neighborhood', 'political'],
borough: ['sublocality', 'sublocality_level_1', 'political'],
quarter: ['neighborhood', 'political'],
postcode: ['postal_code'],
road: ['route'],
Expand All @@ -39,6 +41,17 @@ const ADDRESS_TYPE_MAP = Object.freeze({
university: ['university'],
stadium: ['stadium'],
island: ['natural_feature'],
harbour: ['natural_feature'],
bay: ['natural_feature'],
river: ['natural_feature'],
lake: ['natural_feature'],
sea: ['natural_feature'],
ocean: ['natural_feature'],
volcano: ['natural_feature'],
hotel: ['lodging', 'point_of_interest', 'establishment'],
motel: ['lodging', 'point_of_interest', 'establishment'],
museum: ['museum', 'point_of_interest', 'establishment'],
hospital: ['hospital', 'health', 'point_of_interest', 'establishment'],
});

/**
Expand Down Expand Up @@ -80,6 +93,8 @@ export function typesFromNominatim(hit) {
ADDRESS_TYPE_MAP[addresstype] ||
ADDRESS_TYPE_MAP[osmType] ||
(osmClass === 'highway' ? ['route'] : null) ||
(osmClass === 'railway' ? ['transit_station'] : null) ||
(osmClass === 'boundary' ? ['political'] : null) ||
(osmClass === 'leisure' ? ['park'] : null) ||
(osmClass === 'natural' ? ['natural_feature'] : null) ||
(osmClass === 'aeroway' ? ['airport'] : null) ||
Expand Down Expand Up @@ -147,9 +162,8 @@ export function nominatimToGeocodeResult(hit) {
* Translate a `swLat,swLng|neLat,neLng` bias into Nominatim's
* `viewbox=west,north,east,south`, or null when the string is unusable.
*
* Both edges are range-checked. A viewbox naming a longitude of 999 is not a
* hint the upstream can use, and sending it is a malformed request rather than
* a soft one.
* Both edges are range-checked. Tolerates whitespace around delimiter commas
* and pipe symbols.
*
* @param {string} bounds
* @returns {string|null}
Expand All @@ -158,7 +172,7 @@ export function nominatimViewboxFromBounds(bounds) {
const match = String(bounds || '')
.trim()
.match(
/^(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)\|(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)$/,
/^(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*\|\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)$/,
);
if (!match) return null;
const swLat = nominatimLatitude(match[1]);
Expand Down
17 changes: 17 additions & 0 deletions src/nominatimGeocode.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ test('types fall back to a place rather than guessing a category', () => {
assert.deepEqual(typesFromNominatim({ class: 'highway', type: 'primary' }), [
'route',
]);
assert.deepEqual(typesFromNominatim({ addresstype: 'borough' }), [
'sublocality',
'sublocality_level_1',
'political',
]);
assert.deepEqual(typesFromNominatim({ addresstype: 'museum' }), [
'museum',
'point_of_interest',
'establishment',
]);
assert.deepEqual(typesFromNominatim({ class: 'railway', type: 'station' }), [
'transit_station',
]);
assert.deepEqual(typesFromNominatim({}), [
'point_of_interest',
'establishment',
Expand All @@ -151,6 +164,10 @@ test('a viewport hint is sent only when every edge is a real coordinate', () =>
nominatimViewboxFromBounds('30.1,-97.95|30.52,-97.55'),
'-97.95,30.52,-97.55,30.1',
);
assert.equal(
nominatimViewboxFromBounds(' 30.1 , -97.95 | 30.52 , -97.55 '),
'-97.95,30.52,-97.55,30.1',
);

for (const bounds of [
null,
Expand Down
Loading