|
|
@ -6,47 +6,35 @@ const logger = require('pelias-logger').get('api'); |
|
|
|
const logging = require( '../helper/logging' ); |
|
|
|
const logging = require( '../helper/logging' ); |
|
|
|
const Document = require('pelias-model').Document; |
|
|
|
const Document = require('pelias-model').Document; |
|
|
|
|
|
|
|
|
|
|
|
function createDocWithoutLineage(result) { |
|
|
|
const boundingBoxRegex = /^-?\d+\.\d+,-?\d+\.\d+,-?\d+\.\d+,-?\d+\.\d+$/; |
|
|
|
const doc = new Document('whosonfirst', result.placetype, result.id.toString()); |
|
|
|
|
|
|
|
doc.setName('default', result.name); |
|
|
|
|
|
|
|
doc.setCentroid( { lat: result.geom.lat, lon: result.geom.lon } ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parsedBoundingBox = result.geom.bbox.split(',').map(parseFloat); |
|
|
|
|
|
|
|
doc.setBoundingBox({ |
|
|
|
|
|
|
|
upperLeft: { |
|
|
|
|
|
|
|
lat: parsedBoundingBox[3], |
|
|
|
|
|
|
|
lon: parsedBoundingBox[0] |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
lowerRight: { |
|
|
|
|
|
|
|
lat: parsedBoundingBox[1], |
|
|
|
|
|
|
|
lon: parsedBoundingBox[2] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const esDoc = doc.toESDocument(); |
|
|
|
|
|
|
|
esDoc.data._id = esDoc._id; |
|
|
|
|
|
|
|
esDoc.data._type = esDoc._type; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return esDoc.data; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function validBoundingBox(bbox) { |
|
|
|
|
|
|
|
return boundingBoxRegex.test(bbox); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function synthesizeDocs(result) { |
|
|
|
function synthesizeDocs(result) { |
|
|
|
const doc = new Document('whosonfirst', result.placetype, result.id.toString()); |
|
|
|
const doc = new Document('whosonfirst', result.placetype, result.id.toString()); |
|
|
|
doc.setName('default', result.name); |
|
|
|
doc.setName('default', result.name); |
|
|
|
doc.setCentroid( { lat: result.geom.lat, lon: result.geom.lon } ); |
|
|
|
|
|
|
|
|
|
|
|
// only assign centroid if both lat and lon are finite numbers
|
|
|
|
const parsedBoundingBox = result.geom.bbox.split(',').map(parseFloat); |
|
|
|
if (_.conformsTo(result.geom, { 'lat': _.isFinite, 'lon': _.isFinite } )) { |
|
|
|
doc.setBoundingBox({ |
|
|
|
doc.setCentroid( { lat: result.geom.lat, lon: result.geom.lon } ); |
|
|
|
upperLeft: { |
|
|
|
} |
|
|
|
lat: parsedBoundingBox[3], |
|
|
|
|
|
|
|
lon: parsedBoundingBox[0] |
|
|
|
// lodash conformsTo verifies that an object has a property with a certain format
|
|
|
|
}, |
|
|
|
if (_.conformsTo(result.geom, { 'bbox': validBoundingBox } )) { |
|
|
|
lowerRight: { |
|
|
|
const parsedBoundingBox = result.geom.bbox.split(',').map(parseFloat); |
|
|
|
lat: parsedBoundingBox[1], |
|
|
|
doc.setBoundingBox({ |
|
|
|
lon: parsedBoundingBox[2] |
|
|
|
upperLeft: { |
|
|
|
} |
|
|
|
lat: parsedBoundingBox[3], |
|
|
|
}); |
|
|
|
lon: parsedBoundingBox[0] |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
lowerRight: { |
|
|
|
|
|
|
|
lat: parsedBoundingBox[1], |
|
|
|
|
|
|
|
lon: parsedBoundingBox[2] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (_.isEmpty(result.lineage)) { |
|
|
|
if (_.isEmpty(result.lineage)) { |
|
|
|
// there are no hierarchies so just return what's been assembled so far
|
|
|
|
// there are no hierarchies so just return what's been assembled so far
|
|
|
@ -106,7 +94,7 @@ function setup(placeholderService, should_execute) { |
|
|
|
res.meta = {}; |
|
|
|
res.meta = {}; |
|
|
|
res.data = _.flatten(results.map((result) => { |
|
|
|
res.data = _.flatten(results.map((result) => { |
|
|
|
if (_.isEmpty(result.lineage)) { |
|
|
|
if (_.isEmpty(result.lineage)) { |
|
|
|
return createDocWithoutLineage(result); |
|
|
|
return synthesizeDocs(result); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return synthesizeDocs(result); |
|
|
|
return synthesizeDocs(result); |
|
|
|