diff --git a/helper/geojsonify.js b/helper/geojsonify.js index aa88471e..82c4cf88 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -1,7 +1,9 @@ -var GeoJSON = require('geojson'), - extent = require('geojson-extent'), - outputGenerator = require('./outputGenerator'); +var GeoJSON = require('geojson'); +var extent = require('geojson-extent'); +var outputGenerator = require('./outputGenerator'); +var logger = require('pelias-logger').get('api'); + // Properties to be copied when details=true var DETAILS_PROPS = [ @@ -20,20 +22,62 @@ var DETAILS_PROPS = [ 'confidence' ]; -var META_MAP = { - 'geoname': { type: '???', source: 'gn' }, // TODO: not sure how to map. will need to use categories? - 'osmnode': { type: 'venue', source: 'osm' }, - 'osmway': { type: 'venue', source: 'osm' }, - 'admin0': { type: 'country', source: 'qs' }, - 'admin1': { type: 'region', source: 'qs' }, - 'admin2': { type: 'county', source: 'qs' }, - 'neighborhood': { type: 'neighbourhood', source: 'qs' }, - 'locality': { type: 'locality', source: 'qs' }, - 'local_admin': { type: 'local_admin', source: 'qs' }, - 'osmaddress': { type: 'address', source: 'osm' }, - 'openaddresses': { type: 'address', source: 'oa' } + +var SOURCES = { + 'geoname': 'gn', + 'osmnode': 'osm', + 'osmway': 'osm', + 'admin0': 'qs', + 'admin1': 'qs', + 'admin2': 'qs', + 'neighborhood': 'qs', + 'locality': 'qs', + 'local_admin': 'qs', + 'osmaddress': 'osm', + 'openaddresses': 'oa' }; +function lookupSource(src) { + return SOURCES.hasOwnProperty(src._type) ? SOURCES[src._type] : src._type; +} + +function lookupLayer(src) { + switch(src._type) { + case 'osmnode': + case 'osmway': + return 'venue'; + case 'admin0': + return 'country'; + case 'admin1': + return 'region'; + case 'admin2': + return 'county'; + case 'neighborhood': + return 'neighbourhood'; + case 'locality': + return 'locality'; + case 'local_admin': + return 'localadmin'; + case 'osmaddress': + case 'openaddresses': + return 'address'; + case 'geoname': + if (src.category && src.category.indexOf('admin') !== -1) { + if (src.category.indexOf('admin:city') !== -1) { return 'locality'; } + if (src.category.indexOf('admin:admin1') !== -1) { return 'region'; } + if (src.category.indexOf('admin:admin2') !== -1) { return 'county'; } + return 'neighbourhood'; // this could also be 'local_admin' + } + + if (src.name) { return 'venue'; } + if (src.address) { return 'address'; } + } + + logger.warn('[geojsonify]: could not map _type ', src._type); + + return src._type; +} + function geojsonifyPlaces( docs, params ){ var details = params ? params.details : {}; @@ -149,12 +193,9 @@ function copyProperties( source, props, dst ) { * @param {object} dst */ function addMetaData(src, dst) { - // lookup mapping, or set both values to _type if not found - var meta = META_MAP[src._type] || { type: src._type, source: src._type }; - dst.id = src._id; - dst.layer = meta.type; - dst.source = meta.source; + dst.layer = lookupLayer(src); + dst.source = lookupSource(src); } /** diff --git a/test/ciao/index.coffee b/test/ciao/index.coffee index 0a460aa6..b495c747 100644 --- a/test/ciao/index.coffee +++ b/test/ciao/index.coffee @@ -1,6 +1,6 @@ #> api root -path: '/' +path: '/v1/' #? endpoint available response.statusCode.should.equal 200 diff --git a/test/ciao/place/msuccess.coffee b/test/ciao/place/msuccess.coffee index 5c69eab1..8f1b0056 100644 --- a/test/ciao/place/msuccess.coffee +++ b/test/ciao/place/msuccess.coffee @@ -1,6 +1,6 @@ #> valid place query -path: '/place?id=geoname:4221195&id=geoname:4193595' +path: '/v1/place?id=geoname:4221195&id=geoname:4193595' #? 200 ok response.statusCode.should.equal 200 diff --git a/test/ciao/place/success.coffee b/test/ciao/place/success.coffee index d4fdea2b..323046a0 100644 --- a/test/ciao/place/success.coffee +++ b/test/ciao/place/success.coffee @@ -1,6 +1,6 @@ #> valid place query -path: '/place?id=geoname:4221195' +path: '/v1/place?id=geoname:4221195' #? 200 ok response.statusCode.should.equal 200 diff --git a/test/ciao/reverse/success.coffee b/test/ciao/reverse/success.coffee index 3f936fbd..78c73832 100644 --- a/test/ciao/reverse/success.coffee +++ b/test/ciao/reverse/success.coffee @@ -1,5 +1,5 @@ #> valid reverse query -path: '/reverse?lat=29.49136&lon=-82.50622' +path: '/v1/reverse?lat=29.49136&lon=-82.50622' #? 200 ok response.statusCode.should.equal 200 @@ -12,4 +12,4 @@ json.date.should.be.within now-5000, now+5000 #? valid geojson json.type.should.equal 'FeatureCollection' -json.features.should.be.instanceof Array \ No newline at end of file +json.features.should.be.instanceof Array diff --git a/test/ciao/search/success.coffee b/test/ciao/search/success.coffee index 79ab9bbc..c1ceba9e 100644 --- a/test/ciao/search/success.coffee +++ b/test/ciao/search/success.coffee @@ -1,5 +1,5 @@ #> valid search query -path: '/search?input=lake&lat=29.49136&lon=-82.50622' +path: '/v1/search?input=lake&lat=29.49136&lon=-82.50622' #? 200 ok response.statusCode.should.equal 200 @@ -12,4 +12,4 @@ json.date.should.be.within now-5000, now+5000 #? valid geojson json.type.should.equal 'FeatureCollection' -json.features.should.be.instanceof Array \ No newline at end of file +json.features.should.be.instanceof Array diff --git a/test/ciao/suggest/nearby_nobias.coffee b/test/ciao/suggest/nearby_nobias.coffee index df0b7930..6c7347d2 100644 --- a/test/ciao/suggest/nearby_nobias.coffee +++ b/test/ciao/suggest/nearby_nobias.coffee @@ -1,6 +1,6 @@ #> suggest without geo bias -path: '/suggest/nearby?input=a' +path: '/v1/suggest/nearby?input=a' #? 400 bad request -response.statusCode.should.equal 400 \ No newline at end of file +response.statusCode.should.equal 400 diff --git a/test/ciao/suggest/success.coffee b/test/ciao/suggest/success.coffee index e54e6424..71cd13a7 100644 --- a/test/ciao/suggest/success.coffee +++ b/test/ciao/suggest/success.coffee @@ -1,6 +1,6 @@ #> valid suggest query -path: '/suggest?input=a&lat=0&lon=0' +path: '/v1/suggest?input=a&lat=0&lon=0' #? 200 ok response.statusCode.should.equal 200 @@ -13,4 +13,4 @@ json.date.should.be.within now-5000, now+5000 #? valid geojson json.type.should.equal 'FeatureCollection' -json.features.should.be.instanceof Array \ No newline at end of file +json.features.should.be.instanceof Array diff --git a/test/ciao/suggest/success_nearby.coffee b/test/ciao/suggest/success_nearby.coffee index ca958720..128b880e 100644 --- a/test/ciao/suggest/success_nearby.coffee +++ b/test/ciao/suggest/success_nearby.coffee @@ -1,6 +1,6 @@ #> valid suggest query -path: '/suggest/nearby?input=a&lat=29.49136&lon=-82.50622' +path: '/v1/suggest/nearby?input=a&lat=29.49136&lon=-82.50622' #? 200 ok response.statusCode.should.equal 200 @@ -13,4 +13,4 @@ json.date.should.be.within now-5000, now+5000 #? valid geojson json.type.should.equal 'FeatureCollection' -json.features.should.be.instanceof Array \ No newline at end of file +json.features.should.be.instanceof Array