From de6f6084e0dd972f58d493d002c777957cf9a0d3 Mon Sep 17 00:00:00 2001 From: roemhildtg Date: Tue, 16 Mar 2021 17:14:51 -0500 Subject: [PATCH 1/2] changed: added geocoding api --- app/controllers/api-v2-controller.js | 16 ++++++++++ app/lib/geocoder/mapbox.js | 46 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 app/lib/geocoder/mapbox.js diff --git a/app/controllers/api-v2-controller.js b/app/controllers/api-v2-controller.js index 22ccbd2dd..dca2966be 100644 --- a/app/controllers/api-v2-controller.js +++ b/app/controllers/api-v2-controller.js @@ -13,6 +13,8 @@ const utils = require( '../lib/utils' ); const keys = require( '../lib/router-utils' ).idEncryptionKeys; const router = express.Router(); const quotaErrorMessage = 'Forbidden. No quota left'; +const configModel = require( '../models/config-model' ); +const getMapboxResponse = require( '../lib/geocoder/mapbox' ); // var debug = require( 'debug' )( 'api-controller-v2' ); module.exports = app => { @@ -27,6 +29,9 @@ router } ) .get( '/version', getVersion ) .post( '/version', getVersion ) + + .get( '/geocoder', getGeocodeResponse ) + .all( '*', authCheck ) .all( '*', _setQuotaUsed ) .all( '*', _setDefaultsQueryParam ) @@ -427,6 +432,17 @@ function removeInstance( req, res, next ) { .catch( next ); } +function getGeocodeResponse( req, res ){ + switch( configModel.server.geocoder.provider ){ + case 'mapbox': + getMapboxResponse( req.query, ( response ) => { + res.json( response ); + } ); + break; + default: + throw new Error( 'Geocoder provider is not configured. Please configure `config.geocoder.provider`' ); + } +} /** * @param {module:api-controller~ExpressRequest} req - HTTP request diff --git a/app/lib/geocoder/mapbox.js b/app/lib/geocoder/mapbox.js new file mode 100644 index 000000000..ff58db09e --- /dev/null +++ b/app/lib/geocoder/mapbox.js @@ -0,0 +1,46 @@ +const configModel = require( '../../models/config-model' ); +const request = require( 'request' ); + +module.exports = function getMapboxResponse( query, callback ) { + + const config = configModel.server.geocoder; + let url = config.url || 'https://api.mapbox.com/geocoding/v5/mapbox.places/{address}.json'; + const accessToken = config.apiKey; + + const params = Object.assign( {}, config.params, { + access_token: accessToken, + proximity: query.$center, // -93.17284130807734,45.070291367515466 + bbox: query.$bbox, // -93.13644718051957,45.05118347242032,-93.17284130807734,45.070291367515466 + } ); + + return request( url.replace( '{address}', query.address ), + { + qs: params + }, + ( error, response, body ) => { + let data; + try { + data = JSON.parse( body ); + } catch( e ) { + data = { + features: [], + }; + } + callback( + data.features.map( f => { + return { + geometry: f.geometry, + id: f.id, + type: f.type, + properties: { + name: f.place_name, + type: f.place_type.join( ',' ), + score: f.relevance, + accuracy: f.properties.accuracy, + } + }; + } ) + ); + } + ); +}; From 47c50d23ff00aaec63222051064ac931cabdd509 Mon Sep 17 00:00:00 2001 From: roemhildtg Date: Wed, 17 Mar 2021 12:47:08 -0500 Subject: [PATCH 2/2] change: limit geocoder response to $limit --- app/lib/geocoder/mapbox.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/lib/geocoder/mapbox.js b/app/lib/geocoder/mapbox.js index ff58db09e..e419071aa 100644 --- a/app/lib/geocoder/mapbox.js +++ b/app/lib/geocoder/mapbox.js @@ -11,6 +11,7 @@ module.exports = function getMapboxResponse( query, callback ) { access_token: accessToken, proximity: query.$center, // -93.17284130807734,45.070291367515466 bbox: query.$bbox, // -93.13644718051957,45.05118347242032,-93.17284130807734,45.070291367515466 + limit: query.$limit, } ); return request( url.replace( '{address}', query.address ), @@ -26,7 +27,7 @@ module.exports = function getMapboxResponse( query, callback ) { features: [], }; } - callback( + data = data.features ? data.features.map( f => { return { geometry: f.geometry, @@ -39,8 +40,8 @@ module.exports = function getMapboxResponse( query, callback ) { accuracy: f.properties.accuracy, } }; - } ) - ); + } ) : { error: data.message }; + callback( data ); } ); };