Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Bug] fixed geocoder crash and added ability to pass coordinates (#1342)

Shan He32d801826641
diff --git a/src/components/geocoder/geocoder.js b/src/components/geocoder/geocoder.js
index e7fca553b..ae2760715 100644
--- a/src/components/geocoder/geocoder.js
+++ b/src/components/geocoder/geocoder.js
@@ -28,8 +28,27 @@ import KeyEvent from 'constants/keyevent';
import {Input} from 'components/common/styled-components';
import {Search, Delete} from 'components/common/icons';
+// matches only valid coordinates
+const COORDINATE_REGEX_STRING =
+ '^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?),\\s*[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)';
+const COORDINATE_REGEX = RegExp(COORDINATE_REGEX_STRING);
+
+const PLACEHOLDER = 'Enter an address or coordinates, ex 37.79,-122.40';
+
let debounceTimeout = null;
+export const testForCoordinates = query => {
+ const isValid = COORDINATE_REGEX.test(query.trim());
+
+ if (!isValid) {
+ return [isValid, query];
+ }
+
+ const tokens = query.trim().split(',');
+
+ return [isValid, Number(tokens[0]), Number(tokens[1])];
+};
+
const StyledContainer = styled.div`
position: relative;
color: ${props => props.theme.textColor};
@@ -51,6 +70,7 @@ const StyledContainer = styled.div`
input {
padding: 4px 36px;
height: ${props => props.theme.geocoderInputHeight}px;
+ caret-color: unset;
}
}
@@ -86,12 +106,9 @@ const StyledContainer = styled.div`
}
`;
-const PLACEHOLDER = 'Enter an Address';
-
const GeoCoder = ({
mapboxApiAccessToken,
className = '',
- initialInputValue = '',
limit = 5,
timeout = 300,
formatItem = item => item.place_name,
@@ -103,7 +120,7 @@ const GeoCoder = ({
width,
intl
}) => {
- const [inputValue, setInputValue] = useState(initialInputValue);
+ const [inputValue, setInputValue] = useState(null);
const [showResults, setShowResults] = useState(false);
const [showDelete, setShowDelete] = useState(false);
const [results, setResults] = useState([]);
@@ -115,15 +132,27 @@ const GeoCoder = ({
event => {
const queryString = event.target.value;
setInputValue(queryString);
- clearTimeout(debounceTimeout);
-
- debounceTimeout = setTimeout(async () => {
- if (limit > 0 && Boolean(queryString)) {
- const response = await client.geocodeForward(queryString, {limit});
- setShowResults(true);
- setResults(response.entity.features);
- }
- }, timeout);
+ const [hasValidCoordinates, longitude, latitude] = testForCoordinates(queryString);
+ if (hasValidCoordinates) {
+ setResults([{center: [latitude, longitude], place_name: queryString}]);
+ } else {
+ clearTimeout(debounceTimeout);
+ debounceTimeout = setTimeout(async () => {
+ if (limit > 0 && Boolean(queryString)) {
+ try {
+ const response = await client.geocodeForward(queryString, {limit});
+ if (response.entity.features) {
+ setShowResults(true);
+ setResults(response.entity.features);
+ }
+ } catch (e) {
+ // TODO: show geocode error
+ // eslint-disable-next-line no-console
+ console.log(e);
+ }
+ }
+ }, timeout);
+ }
},
[client, limit, timeout, setResults, setShowResults]
);
@@ -171,6 +200,9 @@ const GeoCoder = ({
const onKeyDown = useCallback(
e => {
+ if (!results || results.length === 0) {
+ return;
+ }
switch (e.keyCode) {
case KeyEvent.DOM_VK_UP:
setSelectedIndex(selectedIndex > 0 ? selectedIndex - 1 : selectedIndex);
@@ -180,7 +212,9 @@ const GeoCoder = ({
break;
case KeyEvent.DOM_VK_ENTER:
case KeyEvent.DOM_VK_RETURN:
- onItemSelected(results[selectedIndex]);
+ if (results[selectedIndex]) {
+ onItemSelected(results[selectedIndex]);
+ }
break;
default:
break;
diff --git a/src/localization/en.js b/src/localization/en.js
index da9119fe6..8b0f4a289 100644
--- a/src/localization/en.js
+++ b/src/localization/en.js
@@ -442,7 +442,7 @@ export default {
or: 'or'
},
geocoder: {
- title: 'Enter an Address'
+ title: 'Enter an address or coordinates, ex 37.79,-122.40'
},
fieldSelector: {
clearAll: 'Clear All',
diff --git a/test/browser/components/geocoder-panel-test.js b/test/browser/components/geocoder-panel-test.js
index 32c64c450..d5bbabc91 100644
--- a/test/browser/components/geocoder-panel-test.js
+++ b/test/browser/components/geocoder-panel-test.js
@@ -25,6 +25,7 @@ import test from 'tape';
import {IntlWrapper, mountWithTheme} from 'test/helpers/component-utils';
import GeocoderPanelFactory from 'components/geocoder-panel';
import {appInjector} from 'components/container';
+import {testForCoordinates} from 'components/geocoder/geocoder';
const GeocoderPanel = appInjector.get(GeocoderPanelFactory);
const MAPBOX_TOKEN = process.env.MapboxAccessToken;
@@ -192,3 +193,30 @@ test('GeocoderPanel - render', t => {
t.end();
});
+
+test('Geocoder -> testForCoordinates', t => {
+ t.deepEqual(
+ testForCoordinates('21.22,-138.0'),
+ [true, 21.22, -138.0],
+ 'should recognize valid coordinates'
+ );
+
+ t.deepEqual(
+ testForCoordinates('san francisco'),
+ [false, 'san francisco'],
+ 'should recognize invalid coordinates'
+ );
+
+ t.deepEqual(
+ testForCoordinates('91,123'),
+ [false, '91,123'],
+ 'should recognize invalid coordinates'
+ );
+
+ t.deepEqual(
+ testForCoordinates('-21.122, -123.4321'),
+ [true, -21.122, -123.4321],
+ 'should recognize valid coordinates'
+ );
+ t.end();
+});