From 3e999bcd00d01a1815b5a8e02eec648a1a97643f Mon Sep 17 00:00:00 2001 From: Trevor Gerhardt Date: Mon, 13 Mar 2017 15:50:42 +0100 Subject: [PATCH] fix(geocoder): Fix geocoder issues from using two different ones. --- src/actions/browsochrones.js | 7 +++--- src/actions/index.js | 17 +++++++------ src/components/form.js | 5 ---- src/utils/feature-to-label.js | 10 -------- src/utils/mapbox-geocoder.js | 46 ----------------------------------- 5 files changed, 13 insertions(+), 72 deletions(-) delete mode 100644 src/utils/feature-to-label.js delete mode 100644 src/utils/mapbox-geocoder.js diff --git a/src/actions/browsochrones.js b/src/actions/browsochrones.js index 2a657eb..a45a5b7 100644 --- a/src/actions/browsochrones.js +++ b/src/actions/browsochrones.js @@ -5,9 +5,9 @@ import { } from '@conveyal/woonerf/fetch' import Browsochrones from 'browsochrones' import fetch from 'isomorphic-fetch' +import {search as geocode} from 'isomorphic-mapzen-search' import {getAsObject as getHash} from '../utils/hash' -import {geocode} from '../utils/mapbox-geocoder' import messages from '../utils/messages' import { @@ -70,13 +70,14 @@ function geocodeQs ({ .map(async (p) => { if (qs[p]) { const results = await geocode({ + apiKey: process.env.MAPZEN_SEARCH_KEY, boundary: geocoder.boundary, - focusLatlng: geocoder.focusLatlng, + focusPoint: geocoder.focusLatlng, text: qs[p] }) if (results.features.length > 0) { return { - label: results.features[0].place_name, + label: results.features[0].properties.label, latlng: lonlat(results.features[0].geometry.coordinates) } } diff --git a/src/actions/index.js b/src/actions/index.js index 2f7cf9a..20b5491 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -1,20 +1,21 @@ import lonlat from '@conveyal/lonlat' -import Leaflet from 'leaflet' -import {createAction} from 'redux-actions' - import fetch, { incrementFetches as incrementWork, decrementFetches as decrementWork } from '@conveyal/woonerf/fetch' +import {reverse} from 'isomorphic-mapzen-search' +import Leaflet from 'leaflet' +import {createAction} from 'redux-actions' -import featureToLabel from '../utils/feature-to-label' import {setKeyTo} from '../utils/hash' -import {reverse} from '../utils/mapbox-geocoder' const END = 'end' const START = 'start' -const reverseGeocode = ({latlng}) => reverse(process.env.MAPBOX_ACCESS_TOKEN, latlng) +const reverseGeocode = ({latlng}) => reverse({ + apiKey: process.env.MAPZEN_SEARCH_KEY, + point: latlng +}) export const addActionLogItem = createAction('add action log item', (item) => { const payload = typeof item === 'string' @@ -114,7 +115,7 @@ export function updateStart ({ reverseGeocode({latlng}) .then(({features}) => { if (!features || features.length < 1) return - const label = featureToLabel(features[0]) + const label = features[0].properties.label return [ addActionLogItem(`Set start address to: ${label}`), setStartLabel(label) @@ -312,7 +313,7 @@ export function updateEnd ({ actions.push( setEnd({latlng}), reverseGeocode({latlng}) - .then(({features}) => setEndLabel(featureToLabel(features[0]))) + .then(({features}) => setEndLabel(features[0].properties.label)) ) } diff --git a/src/components/form.js b/src/components/form.js index 6f01b9b..5717fb3 100644 --- a/src/components/form.js +++ b/src/components/form.js @@ -1,7 +1,6 @@ import Pure from '@conveyal/woonerf/components/pure' import React from 'react' import Geocoder from 'react-select-geocoder' -import featureToLabel from '../utils/feature-to-label' import messages from '../utils/messages' export default class Form extends Pure { @@ -20,8 +19,6 @@ export default class Form extends Pure { f.id} name='start-address' onChange={onChangeStart} placeholder={messages.Geocoding.StartPlaceholder} @@ -36,8 +33,6 @@ export default class Form extends Pure { f.id} name='end-address' onChange={onChangeEnd} placeholder={messages.Geocoding.EndPlaceholder} diff --git a/src/utils/feature-to-label.js b/src/utils/feature-to-label.js deleted file mode 100644 index 2b671dd..0000000 --- a/src/utils/feature-to-label.js +++ /dev/null @@ -1,10 +0,0 @@ -export default function featureToLabel (opts) { - let {label, localadmin, locality} = opts.properties - if (label && localadmin && locality) { - if (locality === 'Indianapolis city (balance)') { - locality = 'Indianapolis' - } - return label.replace(localadmin, locality) - } - return opts.place_name || label -} diff --git a/src/utils/mapbox-geocoder.js b/src/utils/mapbox-geocoder.js deleted file mode 100644 index 93acfda..0000000 --- a/src/utils/mapbox-geocoder.js +++ /dev/null @@ -1,46 +0,0 @@ -import lonlat from '@conveyal/lonlat' -import {mapbox} from 'mapbox.js' - -mapbox.accessToken = process.env.MAPBOX_ACCESS_TOKEN - -const MAPBOX_TYPES = ['address', 'neighborhood', 'place', 'poi'] -const geocoder = mapbox.geocoder('mapbox.places') - -export function search (apiKey, text, { - boundary, - focusLatlng, - format -} = {}) { - return geocode({boundary, focusLatlng, text}) - .then((results) => ({...results, features: results.features.slice(0, 3)})) -} - -export function geocode ({ - boundary, - focusLatlng, - text -}) { - if (!text) return Promise.resolve([]) - return new Promise((resolve, reject) => { - geocoder.query({ - country: boundary.country, - proximity: lonlat.toCoordinates(focusLatlng), - query: text, - types: MAPBOX_TYPES - }, (err, response) => { - if (err) return reject(err) - if (!response) return resolve([]) - resolve(response.results) - }) - }) -} - -export function reverse (apiKey, latlng) { - return new Promise((resolve, reject) => { - geocoder.reverseQuery(latlng, (err, results) => { - if (err) return reject(err) - if (!results) return resolve() - resolve(results) - }) - }) -}