Skip to content

Instantly share code, notes, and snippets.

@tcha-tcho
Created August 11, 2023 21:24
Show Gist options
  • Save tcha-tcho/faf81c23eed6331202eea55787b36bbe to your computer and use it in GitHub Desktop.
Save tcha-tcho/faf81c23eed6331202eea55787b36bbe to your computer and use it in GitHub Desktop.
// get streets near point, ordered by proximity to point ASC in bulk (positions)
// eg: http://localhost:3000/street/bulk/geojson
app.get('/street/bulk/geojson', function ( req, res ) {
// http://142.132.241.141:4700/street/near/geojson?lat=-23.264973&lon=-47.299873
// http://142.132.241.141:4700/street/bulk/geojson?positions=-23.264973,-47.299873
getData(req).then(data => {
const max_distance = data.dist || 0.01;
const max_matches = data.max_matches || 100;
if (!data.positions) {
data.positions = data.lat+','+data.lon
}
if (typeof data.positions === 'string') {
data.positions = data.positions.split(';');
}
let all = {};
function complete() {
res.setHeader('Access-Control-Allow-Origin', '*');
const cors = 'Origin, X-Requested-With, Content-Type, Accept';
res.setHeader('Access-Control-Allow-Headers', cors);
res.json( all );
}
let len = data.positions.length;
let count = 0;
for (var i = data.positions.length - 1; i >= 0; i--) {
let point = data.positions[i];
let arr = point.split(',');
const lat = (arr[0]||'').trim();
const lon = (arr[1]||'').trim();
const pointobj = { lat: lat, lon: lon };
conn.bulk.query( pointobj, async function( _err, ordered ) {
if ( !ordered || !ordered.length ) {
ordered = [];
}
// remove points over a certain distance (in degrees)
ordered = ordered.filter( function( o ){
return o.proj.dist <= max_distance;
});
// ordered = ordered.map( function(o) {
// return Array.isArray( o.street.name ) ? o.street.name[0] : o.street.name;
// });
var number = data.number;
var street = ordered[0];
conn.search.query( pointobj, number, street, function( err, location ) {
let address = "";
if( err || _err ){
address = "Error: "+(err||"")+" "+(_err||"");
} else {
if( !location ) {
address = street;
} else {
address = JSON.stringify(location);
}
all[ point ] = address;
};
count ++;
if (count >= len) {
complete();
}
});
}, max_matches);
};
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment