|
|
@ -5,70 +5,113 @@ var GeoJSON = require('geojson'), |
|
|
|
|
|
|
|
|
|
|
|
// Properties to be copied when details=true
|
|
|
|
// Properties to be copied when details=true
|
|
|
|
var DETAILS_PROPS = [ |
|
|
|
var DETAILS_PROPS = [ |
|
|
|
'alpha3', |
|
|
|
'housenumber', |
|
|
|
'admin0', |
|
|
|
'street', |
|
|
|
'admin1', |
|
|
|
|
|
|
|
'admin1_abbr', |
|
|
|
|
|
|
|
'admin2', |
|
|
|
|
|
|
|
'local_admin', |
|
|
|
|
|
|
|
'locality', |
|
|
|
|
|
|
|
'neighborhood', |
|
|
|
|
|
|
|
'category', |
|
|
|
'category', |
|
|
|
'address', |
|
|
|
'postalcode', |
|
|
|
'country_a', |
|
|
|
'country_a', |
|
|
|
'country', |
|
|
|
'country', |
|
|
|
'region', |
|
|
|
'region', |
|
|
|
'region_a', |
|
|
|
'region_a', |
|
|
|
'county', |
|
|
|
'county', |
|
|
|
|
|
|
|
'localadmin', |
|
|
|
|
|
|
|
'locality', |
|
|
|
'neighbourhood' |
|
|
|
'neighbourhood' |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var META_MAP = { |
|
|
|
function search( docs, params ){ |
|
|
|
'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' } |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function geojsonifyPlaces( docs, params ){ |
|
|
|
|
|
|
|
|
|
|
|
var details = params ? params.details : {}; |
|
|
|
var details = params ? params.details : {}; |
|
|
|
details = details === true || details === 1; |
|
|
|
details = details === true || details === 1; |
|
|
|
|
|
|
|
|
|
|
|
// flatten & expand data for geojson conversion
|
|
|
|
// flatten & expand data for geojson conversion
|
|
|
|
var geodata = docs.map( function( doc ) { |
|
|
|
var geodata = docs |
|
|
|
|
|
|
|
.map(geojsonifyPlace.bind(null, details)) |
|
|
|
|
|
|
|
.filter( function( doc ){ |
|
|
|
|
|
|
|
return !!doc; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// something went very wrong
|
|
|
|
// convert to geojson
|
|
|
|
if( !doc || !doc.hasOwnProperty( 'center_point' ) ) { |
|
|
|
var geojson = GeoJSON.parse( geodata, { Point: ['lat', 'lng'] }); |
|
|
|
return warning(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var output = {}; |
|
|
|
// bounding box calculations
|
|
|
|
|
|
|
|
computeBBox(geojson); |
|
|
|
|
|
|
|
|
|
|
|
// provide metadata to consumer
|
|
|
|
return geojson; |
|
|
|
output.id = doc._id; |
|
|
|
} |
|
|
|
output.layer = doc._type; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// map center_point
|
|
|
|
function geojsonifyPlace(details, place) { |
|
|
|
output.lat = parseFloat( doc.center_point.lat ); |
|
|
|
|
|
|
|
output.lng = parseFloat( doc.center_point.lon ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (details) { |
|
|
|
// something went very wrong
|
|
|
|
// map name
|
|
|
|
if( !place || !place.hasOwnProperty( 'center_point' ) ) { |
|
|
|
if( !doc.name || !doc.name.default ) { return warning(); } |
|
|
|
return warning('No doc or center_point property'); |
|
|
|
output.name = doc.name.default; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
copyProperties( doc, DETAILS_PROPS, output ); |
|
|
|
var geocoding = {}; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// generate region-specific text string
|
|
|
|
addMetaData(place, geocoding); |
|
|
|
output.text = outputGenerator( doc ); |
|
|
|
addDetails(details, place, geocoding); |
|
|
|
|
|
|
|
addLabel(place, geocoding); |
|
|
|
|
|
|
|
|
|
|
|
return output; |
|
|
|
var output = {}; |
|
|
|
|
|
|
|
|
|
|
|
// filter-out invalid entries
|
|
|
|
output.geocoding = geocoding; |
|
|
|
}).filter( function( doc ){ |
|
|
|
// map center_point for GeoJSON to work properly
|
|
|
|
return doc; |
|
|
|
// these should not show up in the final feature properties
|
|
|
|
}); |
|
|
|
output.lat = parseFloat(place.center_point.lat); |
|
|
|
|
|
|
|
output.lng = parseFloat(place.center_point.lon); |
|
|
|
|
|
|
|
|
|
|
|
// convert to geojson
|
|
|
|
return output; |
|
|
|
var geojson = GeoJSON.parse( geodata, { Point: ['lat', 'lng'] }); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// bounding box calculations
|
|
|
|
/** |
|
|
|
|
|
|
|
* Add details properties when needed |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param {boolean} details |
|
|
|
|
|
|
|
* @param {object} src |
|
|
|
|
|
|
|
* @param {object} dst |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function addDetails(details, src, dst) { |
|
|
|
|
|
|
|
if (details) { |
|
|
|
|
|
|
|
// map name
|
|
|
|
|
|
|
|
if( !src.name || !src.name.default ) { return warning(src); } |
|
|
|
|
|
|
|
dst.name = src.name.default; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
copyProperties(src, DETAILS_PROPS, dst); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Add region-specific label string |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param {object} src |
|
|
|
|
|
|
|
* @param {object} dst |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function addLabel(src, dst) { |
|
|
|
|
|
|
|
dst.label = outputGenerator(src); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Compute bbox that encompasses all features in the result set. |
|
|
|
|
|
|
|
* Set bbox property on the geojson object. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param {object} geojson |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function computeBBox(geojson) { |
|
|
|
// @note: extent() sometimes throws Errors for unusual data
|
|
|
|
// @note: extent() sometimes throws Errors for unusual data
|
|
|
|
// eg: https://github.com/pelias/pelias/issues/84
|
|
|
|
// eg: https://github.com/pelias/pelias/issues/84
|
|
|
|
try { |
|
|
|
try { |
|
|
@ -80,8 +123,6 @@ function search( docs, params ){ |
|
|
|
console.error( 'bbox error', e.message, e.stack ); |
|
|
|
console.error( 'bbox error', e.message, e.stack ); |
|
|
|
console.error( 'geojson', JSON.stringify( geojson, null, 2 ) ); |
|
|
|
console.error( 'geojson', JSON.stringify( geojson, null, 2 ) ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return geojson; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -90,16 +131,30 @@ function search( docs, params ){ |
|
|
|
* |
|
|
|
* |
|
|
|
* @param {object} source |
|
|
|
* @param {object} source |
|
|
|
* @param {[]} props |
|
|
|
* @param {[]} props |
|
|
|
* @param {object} dest |
|
|
|
* @param {object} dst |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function copyProperties( source, props, dest ) { |
|
|
|
function copyProperties( source, props, dst ) { |
|
|
|
props.forEach( function ( prop ) { |
|
|
|
props.forEach( function ( prop ) { |
|
|
|
if ( source.hasOwnProperty( prop ) ) { |
|
|
|
if ( source.hasOwnProperty( prop ) ) { |
|
|
|
dest[prop] = source[prop]; |
|
|
|
dst[prop] = source[prop]; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Determine and set place id, type, and source |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param {object} src |
|
|
|
|
|
|
|
* @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.type = meta.type; |
|
|
|
|
|
|
|
dst.source = meta.source; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* emit a warning if the doc format is invalid |
|
|
|
* emit a warning if the doc format is invalid |
|
|
@ -107,9 +162,9 @@ function copyProperties( source, props, dest ) { |
|
|
|
* @note: if you see this error, fix it ASAP! |
|
|
|
* @note: if you see this error, fix it ASAP! |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function warning( doc ) { |
|
|
|
function warning( doc ) { |
|
|
|
console.error( 'error: invalid doc', __filename, doc ); |
|
|
|
console.error( 'error: invalid doc', __filename, doc); |
|
|
|
return false; // remove offending doc from results
|
|
|
|
return false; // remove offending doc from results
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.search = search; |
|
|
|
module.exports.search = geojsonifyPlaces; |