Skip to content

Commit a89ebd4

Browse files
authored
add near and maxDistance parameter to getBoxes request. lets you sear… (#492)
* add near and maxDistance parameter to getBoxes request. lets you search for senseBoxes around a location * fix linting Co-authored-by: Umut Tas <[email protected]>
1 parent 244b5b9 commit a89ebd4

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

packages/api/lib/controllers/boxesController.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ const geoJsonStringifyReplacer = function geoJsonStringifyReplacer (key, box) {
200200
* @apiParam {Boolean="true","false"} [classify=false] if specified, the api will classify the boxes accordingly to their last measurements.
201201
* @apiParam {Boolean="true","false"} [minimal=false] if specified, the api will only return a minimal set of box metadata consisting of [_id, updatedAt, currentLocation, exposure, name] for a fast response.
202202
* @apiParam {Boolean="true","false"} [full=false] if true the API will return populated lastMeasurements (use this with caution for now, expensive on the database)
203+
* @apiParam {String} [near] A comma separated coordinate, if specified, the api will only return senseBoxes within maxDistance (in m) of this location
204+
* @apiParam {Number} [maxDistance=1000] the amount of meters around the near Parameter that the api will search for senseBoxes
203205
* @apiUse ExposureFilterParam
204206
* @apiUse BBoxParam
205207
* @apiSampleRequest https://api.opensensemap.org/boxes
@@ -574,6 +576,8 @@ module.exports = {
574576
{ name: 'classify', defaultValue: 'false', allowedValues: ['true', 'false'] },
575577
{ name: 'minimal', defaultValue: 'false', allowedValues: ['true', 'false'] },
576578
{ name: 'full', defaultValue: 'false', allowedValues: ['true', 'false'] },
579+
{ name: 'near' },
580+
{ name: 'maxDistance' },
577581
{ predef: 'bbox' },
578582
]),
579583
parseAndValidateTimeParamsForFindAllBoxes,

packages/api/lib/helpers/userParamHelpers.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ const retrieveParametersPredefs = {
385385
'bbox' () {
386386
return { name: 'bbox', dataType: 'bbox' };
387387
},
388+
'near' () {
389+
return { name: 'near', dataType: 'as-is' };
390+
},
391+
'maxDistance' () {
392+
return { name: 'maxDistance', dataType: 'as-is' };
393+
},
388394
'location' () {
389395
return { name: 'location', dataType: ['location'], paramValidatorAndParser: retrieveLocationParameter }; // dataType array ['location'] is needed for setting the userparam correctly
390396
},

packages/models/src/box/box.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ boxSchema.methods.getLocations = function getLocations ({ format, fromDate, toDa
938938
};
939939

940940
const buildFindBoxesQuery = function buildFindBoxesQuery (opts = {}) {
941-
const { phenomenon, fromDate, toDate, bbox } = opts,
941+
const { phenomenon, fromDate, toDate, bbox, near, maxDistance } = opts,
942942
query = {};
943943

944944
// simple string parameters
@@ -953,6 +953,19 @@ const buildFindBoxesQuery = function buildFindBoxesQuery (opts = {}) {
953953
query['locations'] = { '$geoWithin': { '$geometry': bbox } };
954954
}
955955

956+
// near search parameter
957+
if (near) {
958+
query['currentLocation'] = {
959+
'$near': {
960+
'$geometry': {
961+
type: 'Point',
962+
coordinates: [near.split(',')[0], near.split(',')[1]]
963+
},
964+
'$maxDistance': maxDistance ? maxDistance : 1000,
965+
}
966+
};
967+
}
968+
956969
// search for phenomenon only together with time params
957970
if (fromDate || toDate) {
958971
if (phenomenon) {

0 commit comments

Comments
 (0)