Skip to content

Commit

Permalink
feat(query): attempt to geocode place name from query params
Browse files Browse the repository at this point in the history
fix #180
  • Loading branch information
landonreed committed Jun 26, 2020
1 parent 072139b commit 808a457
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
11 changes: 2 additions & 9 deletions lib/actions/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,8 @@ export function parseUrlQueryString (params = getUrlParams()) {
})
const searchId = params.ui_activeSearch || randId()
// Convert strings to numbers/objects and dispatch
dispatch(
setQueryParam(
planParamsToQuery(
planParams,
getState().otp.config
),
searchId
)
)
planParamsToQuery(planParams, getState().otp.config)
.then(query => dispatch(setQueryParam(query, searchId)))
}
}

Expand Down
33 changes: 32 additions & 1 deletion lib/util/query.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import qs from 'qs'

import getGeocoder from './geocoder'
import { getTransitModes, hasTransit, isAccessMode, toSentenceCase } from './itinerary'
import { coordsToString, matchLatLon, stringToCoords } from './map'
import queryParams from './query-params'
Expand Down Expand Up @@ -148,22 +149,52 @@ export function getDefaultQuery (config) {
return defaultQuery
}

/**
* Geocode utility for returning the first result for the provided place name text.
* @param {String} text - text to search
* @param {Object} geocoderConfig
* @return {Location}
*/
async function getFirstGeocodeResult (text, geocoderConfig) {
const geocoder = getGeocoder(geocoderConfig)
// Attempt to geocode search text and return first result if found.
// TODO: Import geocoder from @opentripplanner
return geocoder
.search({ text })
.then((result) => {
const firstResult = result.features && result.features[0]
if (firstResult) {
return geocoder.getLocationFromGeocodedFeature(firstResult)
}
})
}

/**
* Create a otp query based on a the url params.
*
* @param {Object} params An object representing the parsed querystring of url
* params.
* @param config the config in the otp-rr store.
*/
export function planParamsToQuery (params, config) {
export async function planParamsToQuery (params, config) {
const query = {}
for (var key in params) {
switch (key) {
case 'fromPlace':
query.from = parseLocationString(params.fromPlace)
// If a valid location was not found, but the place name text exists,
// attempt to geocode the name.
if (!query.from && params.fromPlace) {
query.from = await getFirstGeocodeResult(params.fromPlace, config.geocoder)
}
break
case 'toPlace':
query.to = parseLocationString(params.toPlace)
// If a valid location was not found, but the place name text exists,
// attempt to geocode the name.
if (!query.to && params.toPlace) {
query.to = await getFirstGeocodeResult(params.toPlace, config.geocoder)
}
break
case 'arriveBy':
query.departArrive = params.arriveBy === 'true'
Expand Down

0 comments on commit 808a457

Please sign in to comment.