-
-
Notifications
You must be signed in to change notification settings - Fork 87
feat(geocoder): add native Photon provider #1242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TurtIeSocks
wants to merge
4
commits into
main
Choose a base branch
from
feat/photon-geocoder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e0700d2
feat(geocoder): add native Photon provider
TurtIeSocks b46654a
fix(geocoder): drop Photon features whose coordinates are not finite
TurtIeSocks f33185d
fix(geocoder): carry geocoderProvider onto the PoracleAPI instance
TurtIeSocks 38654d1
fix(geocoder): fall back to Photon's locality layer for the settlement
TurtIeSocks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| // @ts-check | ||
|
|
||
| const { fetchJson } = require('../utils/fetchJson') | ||
|
|
||
| /** | ||
| * Photon (https://github.com/komoot/photon) speaks GeoJSON rather than | ||
| * Nominatim's JSON, so it cannot be driven through node-geocoder's | ||
| * `openstreetmap` provider. This module talks to it directly and returns | ||
| * entries in the same shape that provider produces, so everything downstream | ||
| * (`formatter`, the webhook resolvers) is unaware of which backend answered. | ||
| */ | ||
|
|
||
| /** | ||
| * The Photon properties this module reads. Photon returns more; anything not | ||
| * listed here has no consumer in ReactMap. | ||
| * @typedef {object} PhotonProperties | ||
| * @property {string} [name] The result's own label | ||
| * @property {string} [housenumber] | ||
| * @property {string} [street] | ||
| * @property {string} [postcode] | ||
| * @property {string} [city] | ||
| * @property {string} [locality] A settlement below Photon's city layer, such | ||
| * as the hamlet a rural address sits in. Present when `city` is not. | ||
| * @property {string} [county] | ||
| * @property {string} [state] | ||
| * @property {string} [country] | ||
| * @property {string} [countrycode] Upper-case, e.g. "US" | ||
| * @property {string} [osm_key] OSM tag key, e.g. "place" or "highway" | ||
| * @property {string} [osm_value] OSM tag value, e.g. "city" | ||
| */ | ||
|
|
||
| /** | ||
| * @typedef {object} PhotonFeature | ||
| * @property {{ coordinates?: number[] }} [geometry] GeoJSON order: [lon, lat] | ||
| * @property {PhotonProperties} [properties] | ||
| */ | ||
|
|
||
| /** Results requested from Photon for a forward search. */ | ||
| const SEARCH_LIMIT = 10 | ||
|
|
||
| /** | ||
| * Photon reports a result's own label only in `properties.name`, and uses the | ||
| * hierarchy fields purely for what *contains* the result. Nominatim echoes the | ||
| * name into the matching address field, so searching "Denver" yields a city of | ||
| * Denver. These are the OSM classifications where that echo matters, because | ||
| * they are the ones `_formatResult` reads. | ||
| * @type {Record<string, 'city' | 'town' | 'village' | 'hamlet'>} | ||
| */ | ||
| const PLACE_SELF_REFERENCE = { | ||
| city: 'city', | ||
| town: 'town', | ||
| village: 'village', | ||
| hamlet: 'hamlet', | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} base | ||
| * @param {string} path | ||
| * @param {Record<string, string | number>} params | ||
| */ | ||
| function buildUrl(base, path, params) { | ||
| // Trailing slashes are tolerated here, unlike node-geocoder's own | ||
| // `osmServer + '/search'` concatenation. | ||
| const url = new URL(`${base.replace(/\/+$/, '')}${path}`) | ||
| Object.entries(params).forEach(([key, value]) => | ||
| url.searchParams.set(key, String(value)), | ||
| ) | ||
| return url.toString() | ||
| } | ||
|
|
||
| /** | ||
| * Joins address components the way Nominatim renders `display_name`: most | ||
| * specific first, comma separated, skipping absent parts and never repeating a | ||
| * component. A postcode search puts the same value in both the result name and | ||
| * the postcode field, and Nominatim shows it once. | ||
| * @param {(string | undefined)[]} parts | ||
| */ | ||
| function joinComponents(parts) { | ||
| const seen = new Set() | ||
| return parts | ||
| .map((part) => (part || '').trim()) | ||
| .filter((part) => { | ||
| if (!part || seen.has(part)) return false | ||
| seen.add(part) | ||
| return true | ||
| }) | ||
| .join(', ') | ||
| } | ||
|
|
||
| /** | ||
| * Blanks any component that reappears later in the hierarchy, keeping the | ||
| * broader of the two. A city sharing its state's name (the Statue of Liberty | ||
| * sits in city "New York", state "New York") should keep the state: dropping it | ||
| * would strip the state out of a US address line entirely. | ||
| * @param {(string | undefined)[]} parts | ||
| */ | ||
| function preferBroader(parts) { | ||
| return parts.map((part, i) => | ||
| part && parts.slice(i + 1).includes(part) ? undefined : part, | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * @param {PhotonProperties} properties | ||
| * @param {string} settlement The resolved city, town, village, hamlet or locality | ||
| */ | ||
| function buildFormattedAddress(properties, settlement) { | ||
| const street = joinComponents([properties.housenumber, properties.street]) | ||
|
|
||
| // The result's own name leads. When it was already echoed into a hierarchy | ||
| // field, joinComponents drops the repeat rather than printing it twice. | ||
| return joinComponents([ | ||
| properties.name, | ||
| ...preferBroader([ | ||
| street, | ||
| settlement, | ||
| properties.county, | ||
| properties.state, | ||
| properties.postcode, | ||
| properties.country, | ||
| ]), | ||
| ]) | ||
| } | ||
|
|
||
| /** | ||
| * Maps one Photon feature onto the entry shape node-geocoder's `openstreetmap` | ||
| * provider produces, including the three fields ReactMap patches on top of it. | ||
| * | ||
| * Returns null for a feature without usable coordinates: `parseFloat` of an | ||
| * absent value is NaN, and a NaN marker is worse than a missing result. | ||
| * | ||
| * `suburb` and `neighbourhood` are always empty. Photon's nearest field is | ||
| * `district`, which is a different OSM concept, and equating them would be an | ||
| * invention rather than a translation. | ||
| * @param {PhotonFeature} feature | ||
| */ | ||
| function formatPhotonFeature(feature) { | ||
| const coordinates = feature?.geometry?.coordinates | ||
| if (!Array.isArray(coordinates) || coordinates.length < 2) return null | ||
|
|
||
| // GeoJSON is [longitude, latitude]. Never the other way around. | ||
| const [longitude, latitude] = coordinates | ||
| // A well behaved Photon sends two numbers, but the length check alone would | ||
| // pass [null, null] straight through to an entry with a null latitude. Check | ||
| // the values, not just the shape. | ||
| if (!Number.isFinite(longitude) || !Number.isFinite(latitude)) return null | ||
| const properties = feature.properties || {} | ||
|
|
||
| const selfReferenced = PLACE_SELF_REFERENCE[properties.osm_value] | ||
| const isPlace = properties.osm_key === 'place' && !!selfReferenced | ||
| const named = (/** @type {string} */ field) => | ||
| isPlace && selfReferenced === field ? properties.name : undefined | ||
|
|
||
| const city = properties.city || named('city') | ||
| const town = named('town') | ||
| const village = named('village') | ||
| // Photon's hierarchy runs city > district > locality > street, so an address | ||
| // whose containing settlement sits below the city layer carries that name in | ||
| // `locality` and has no `city` at all. Without it a rural address loses its | ||
| // settlement from both `city` and formattedAddress. Named `settlement` rather | ||
| // than `locality` so it is not confused with the Photon field it falls back | ||
| // to. | ||
| const settlement = | ||
| city || town || village || named('hamlet') || properties.locality || '' | ||
|
|
||
| return { | ||
| latitude, | ||
| longitude, | ||
| formattedAddress: buildFormattedAddress(properties, settlement), | ||
| country: properties.country, | ||
| // Mirrors _formatResult's own city/town/village/hamlet fallback, with | ||
| // Photon's locality layer appended to it. | ||
| city: settlement || undefined, | ||
| state: properties.state, | ||
| zipcode: properties.postcode, | ||
| streetName: | ||
| properties.street || | ||
| (properties.osm_key === 'highway' ? properties.name : undefined), | ||
| streetNumber: properties.housenumber, | ||
| // Photon already sends this upper-case, which is what node-geocoder | ||
| // produces after upper-casing Nominatim's lower-case value. | ||
| countryCode: properties.countrycode, | ||
| neighbourhood: '', | ||
| suburb: '', | ||
| town: town || '', | ||
| village: village || '', | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} photonUrl | ||
| * @param {string | { lat: number, lon: number }} search | ||
| * @param {boolean} isReverse | ||
| */ | ||
| async function photonGeocoder(photonUrl, search, isReverse) { | ||
| const url = | ||
| isReverse && typeof search === 'object' | ||
| ? buildUrl(photonUrl, '/reverse', { | ||
| lat: search.lat, | ||
| lon: search.lon, | ||
| limit: 1, | ||
| }) | ||
| : buildUrl(photonUrl, '/api', { | ||
| q: String(search), | ||
| limit: SEARCH_LIMIT, | ||
| }) | ||
|
|
||
| const response = await fetchJson(url) | ||
| // fetchJson answers a failed request with the Response rather than throwing, | ||
| // so an absent features array covers both a network failure and an empty | ||
| // result set. | ||
| const features = Array.isArray(response?.features) ? response.features : [] | ||
|
|
||
| return features.map(formatPhotonFeature).filter(Boolean) | ||
| } | ||
|
|
||
| module.exports = { | ||
| photonGeocoder, | ||
| // Exported for the tests, which check the mapping without a Photon instance. | ||
| formatPhotonFeature, | ||
| joinComponents, | ||
| preferBroader, | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.