diff --git a/helper/geojsonify.js b/helper/geojsonify.js index 5392e957..e8494a9d 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 = [ @@ -19,20 +21,62 @@ var DETAILS_PROPS = [ 'neighbourhood' ]; -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 'local_admin'; + 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 : {}; @@ -148,12 +192,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); } /**