Browse Source

Add categories and address properties to result doc

pull/122/head
Diana Shkolnikov 10 years ago
parent
commit
a68c7fae53
  1. 69
      helper/geojsonify.js
  2. 23
      test/unit/helper/geojsonify.js

69
helper/geojsonify.js

@ -3,23 +3,33 @@ var GeoJSON = require('geojson'),
extent = require('geojson-extent'), extent = require('geojson-extent'),
outputGenerator = require('./outputGenerator'); outputGenerator = require('./outputGenerator');
function search( docs, params ){ // Properties to be copied when details=true
var DETAILS_PROPS = [
'alpha3',
'admin0',
'admin1',
'admin1_abbr',
'admin2',
'local_admin',
'locality',
'neighborhood',
'category',
'address'
];
// emit a warning if the doc format is invalid
// @note: if you see this error, fix it ASAP! function search( docs, params ){
function warning(){
console.error( 'error: invalid doc', __filename );
return false; // remove offending doc from results
}
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( function( doc ) {
// something went very wrong // something went very wrong
if( !doc ) { return warning(); } if( !doc || !doc.center_point ) {
return warning();
}
var output = {}; var output = {};
@ -28,7 +38,6 @@ function search( docs, params ){
output.layer = doc._type; output.layer = doc._type;
// map center_point // map center_point
if( !doc.center_point ) { return warning(); }
output.lat = parseFloat( doc.center_point.lat ); output.lat = parseFloat( doc.center_point.lat );
output.lng = parseFloat( doc.center_point.lon ); output.lng = parseFloat( doc.center_point.lon );
@ -37,15 +46,7 @@ function search( docs, params ){
if( !doc.name || !doc.name.default ) { return warning(); } if( !doc.name || !doc.name.default ) { return warning(); }
output.name = doc.name.default; output.name = doc.name.default;
// map admin values copyProperties( doc, DETAILS_PROPS, output );
if( doc.alpha3 ){ output.alpha3 = doc.alpha3; }
if( doc.admin0 ){ output.admin0 = doc.admin0; }
if( doc.admin1 ){ output.admin1 = doc.admin1; }
if( doc.admin1_abbr ){ output.admin1_abbr = doc.admin1_abbr; }
if( doc.admin2 ){ output.admin2 = doc.admin2; }
if( doc.local_admin ){ output.local_admin = doc.local_admin; }
if( doc.locality ){ output.locality = doc.locality; }
if( doc.neighborhood ){ output.neighborhood = doc.neighborhood; }
} }
// generate region-specific text string // generate region-specific text string
@ -67,4 +68,34 @@ function search( docs, params ){
return geojson; return geojson;
} }
/**
* Copy specified properties from source to dest.
* Ignore missing properties.
*
* @param {object} source
* @param {[]} props
* @param {object} dest
*/
function copyProperties( source, props, dest ) {
var count = props.length;
for ( var i = 0; i < count; i++ ) {
var prop = props[i];
if ( source.hasOwnProperty( prop ) ) {
dest[prop] = source[prop];
}
}
}
/**
* emit a warning if the doc format is invalid
*
* @note: if you see this error, fix it ASAP!
*/
function warning( doc ) {
console.error( 'error: invalid doc', __filename, doc );
return false; // remove offending doc from results
}
module.exports.search = search; module.exports.search = search;

23
test/unit/helper/geojsonify.js

@ -43,7 +43,11 @@ module.exports.tests.search = function(test, common) {
'\'round midnight jazz and blues bar' '\'round midnight jazz and blues bar'
], ],
'output': 'osmnode:2208150035' 'output': 'osmnode:2208150035'
} },
'category': [
'food',
'nightlife'
]
}, },
{ {
'_id': 'id2', '_id': 'id2',
@ -95,7 +99,11 @@ module.exports.tests.search = function(test, common) {
'empire state building' 'empire state building'
], ],
'output': 'osmway:34633854' 'output': 'osmway:34633854'
} },
'category': [
'tourism',
'transport'
]
} }
]; ];
@ -124,7 +132,13 @@ module.exports.tests.search = function(test, common) {
'admin2': 'Angel', 'admin2': 'Angel',
'local_admin': 'test1', 'local_admin': 'test1',
'locality': 'test2', 'locality': 'test2',
'neighborhood': 'test3' 'neighborhood': 'test3',
'category': [ 'food', 'nightlife' ],
'address': {
'number': '13',
'street': 'Liverpool Road',
'zip': 'N1 0RW'
}
} }
}, },
{ {
@ -172,7 +186,8 @@ module.exports.tests.search = function(test, common) {
'admin2': 'New York', 'admin2': 'New York',
'local_admin': 'Manhattan', 'local_admin': 'Manhattan',
'locality': 'New York', 'locality': 'New York',
'neighborhood': 'Koreatown' 'neighborhood': 'Koreatown',
'category': [ 'tourism', 'transport' ]
} }
} }
] ]

Loading…
Cancel
Save