-
Notifications
You must be signed in to change notification settings - Fork 182
RTFDB-4842: Improve user input handling in "mapbox-gl-geocoder" #559
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
pahuta
wants to merge
2
commits into
RTFDB-4842-improve-user-input-handling-3
from
RTFDB-4842-improve-user-input-handling-4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,192 @@ | ||
| 'use strict'; | ||
|
|
||
| var utils = require('./utils'); | ||
|
|
||
| // An integer or a decimal with at least one fractional digit, optionally signed. | ||
| const NUMBER = '-?\\d+(?:\\.\\d+)?'; | ||
|
|
||
| const COMMA_SEPARATED_LNG_LAT_ZOOM_RGX = new RegExp('^(' + NUMBER + '),(' + NUMBER + '),(' + NUMBER + ')$'); | ||
| const SLASH_SEPARATED_ZOOM_LAT_LNG_RGX = new RegExp('^(' + NUMBER + ')\\/(' + NUMBER + ')\\/(' + NUMBER + ')$'); | ||
| const TILE_RGX = /^(\d+)\/(\d+)\/(\d+)$/; | ||
|
|
||
| function isValidLng(value) { | ||
| return value >= -180 && value <= 180; | ||
| } | ||
|
|
||
| function isValidLat(value) { | ||
| return value >= -90 && value <= 90; | ||
| } | ||
|
|
||
| function isValidZoom(zoom) { | ||
| return zoom >= 0 && zoom <= utils.MAX_SPATIAL_ZOOM; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the synthetic feature for a recognized spatial format. `center` and | ||
| * `geometry.coordinates` are both set so the feature needs no special handling in | ||
| * `_fly`, `_handleMarker` or `options.getItemValue`. | ||
| * @private | ||
| * @param {Object} params | ||
| * @param {number} params.lng | ||
| * @param {number} params.lat | ||
| * @param {number} params.zoom | ||
| * @param {string} params.placeName | ||
| * @param {string} params.searchQuery | ||
| * @param {Object} params.properties | ||
| * @returns {Object} a GeoJSON Feature | ||
| */ | ||
| function createFeature(params) { | ||
| return { | ||
| type: 'Feature', | ||
| place_name: params.placeName, | ||
| place_type: ['coordinate'], | ||
| center: [params.lng, params.lat], | ||
| geometry: { | ||
| type: 'Point', | ||
| coordinates: [params.lng, params.lat] | ||
| }, | ||
| properties: params.properties, | ||
| _zoom: params.zoom, | ||
| _searchQuery: params.searchQuery | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Recognizes input of the form `lng,lat,zoom` with no spaces, e.g. `6.925882,51.110352,11.31`. | ||
| * @param {String} searchInput search input | ||
| * @returns {Object|null} a GeoJSON Feature, or `null` if the input doesn't match | ||
| */ | ||
| function parseCommaSeparatedLngLatZoom(searchInput) { | ||
| const match = searchInput.match(COMMA_SEPARATED_LNG_LAT_ZOOM_RGX); | ||
| if (!match) { | ||
| return null; | ||
| } | ||
|
|
||
| const lngStr = match[1]; | ||
| const latStr = match[2]; | ||
| const zoomStr = match[3]; | ||
|
|
||
| const lng = Number(lngStr); | ||
| const lat = Number(latStr); | ||
| const zoom = Number(zoomStr); | ||
|
|
||
| if (!isValidLng(lng) || !isValidLat(lat) || !isValidZoom(zoom)) { | ||
| return null; | ||
| } | ||
|
|
||
| return createFeature({ | ||
| lng: lng, | ||
| lat: lat, | ||
| zoom: zoom, | ||
| placeName: 'Point,lng=' + lngStr + ' lat=' + latStr + ' zoom=' + zoomStr, | ||
| searchQuery: searchInput, | ||
| properties: { | ||
| spatialFormat: 'commaSeparatedLngLatZoom' | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Recognizes input of the form `zoom/lat/lng` with no spaces, e.g. `11.31/51.110352/6.925882`. | ||
| * @param {String} searchInput search input | ||
| * @returns {Object|null} a GeoJSON Feature, or `null` if the input doesn't match | ||
| */ | ||
| function parseSlashSeparatedZoomLatLng(searchInput) { | ||
| const match = searchInput.match(SLASH_SEPARATED_ZOOM_LAT_LNG_RGX); | ||
| if (!match) { | ||
| return null; | ||
| } | ||
|
|
||
| const zoomStr = match[1]; | ||
| const latStr = match[2]; | ||
| const lngStr = match[3]; | ||
|
|
||
| const zoom = Number(zoomStr); | ||
| const lat = Number(latStr); | ||
| const lng = Number(lngStr); | ||
|
|
||
| if (!isValidZoom(zoom) || !isValidLat(lat) || !isValidLng(lng)) { | ||
| return null; | ||
| } | ||
|
|
||
| return createFeature({ | ||
| lng: lng, | ||
| lat: lat, | ||
| zoom: zoom, | ||
| placeName: 'Point,lng=' + lngStr + ' lat=' + latStr + ' zoom=' + zoomStr, | ||
| searchQuery: searchInput, | ||
| properties: { | ||
| spatialFormat: 'slashSeparatedZoomLatLng' | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Recognizes XYZ tile coordinates of the form `z/x/y`, e.g. `14/8507/5477`, and resolves them | ||
| * to the center of the tile. | ||
| * @param {String} searchInput search input | ||
| * @returns {Object|null} a GeoJSON Feature, or `null` if the input doesn't match | ||
| */ | ||
| function parseTile(searchInput) { | ||
| const match = searchInput.match(TILE_RGX); | ||
| if (!match) { | ||
| return null; | ||
| } | ||
|
|
||
| const z = Number(match[1]); | ||
| const x = Number(match[2]); | ||
| const y = Number(match[3]); | ||
|
|
||
| if (!utils.isValidTile(z, x, y)) { | ||
| return null; | ||
| } | ||
|
|
||
| const center = utils.tileToLngLat(z, x, y); | ||
|
|
||
| return createFeature({ | ||
| lng: center[0], | ||
| lat: center[1], | ||
| zoom: z, | ||
| placeName: 'Tile,x=' + x + ' y=' + y + ' z=' + z, | ||
| searchQuery: searchInput, | ||
| properties: { | ||
| spatialFormat: 'tile', | ||
| tile: { z: z, x: x, y: y } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Recognizes a quadkey, e.g. `12020332200123`, and resolves it to the center of the quadkey. | ||
| * @param {String} searchInput search input | ||
| * @returns {Object|null} a GeoJSON Feature, or `null` if the input doesn't match | ||
| */ | ||
| function parseQuadkey(searchInput) { | ||
| if (!utils.isValidQuadkey(searchInput)) { | ||
| return null; | ||
| } | ||
|
|
||
| const qk = searchInput; | ||
| const parsedTile = utils.quadkeyToTile(qk); | ||
| const center = utils.tileToLngLat(parsedTile.z, parsedTile.x, parsedTile.y); | ||
|
|
||
| return createFeature({ | ||
| lng: center[0], | ||
| lat: center[1], | ||
| zoom: parsedTile.z, | ||
| placeName: 'Quadkey,' + qk, | ||
| searchQuery: searchInput, | ||
| properties: { | ||
| spatialFormat: 'quadkey', | ||
| quadkey: qk | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
| parseCommaSeparatedLngLatZoom: parseCommaSeparatedLngLatZoom, | ||
| parseSlashSeparatedZoomLatLng: parseSlashSeparatedZoomLatLng, | ||
| parseTile: parseTile, | ||
| parseQuadkey: parseQuadkey, | ||
| }; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't that break the
externalGeocoder?