Skip to content

Commit

Permalink
add near and maxDistance parameter to getBoxes request. lets you sear… (
Browse files Browse the repository at this point in the history
#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]>
  • Loading branch information
umut0 and Umut Tas authored Jun 11, 2021
1 parent 244b5b9 commit a89ebd4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/api/lib/controllers/boxesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ const geoJsonStringifyReplacer = function geoJsonStringifyReplacer (key, box) {
* @apiParam {Boolean="true","false"} [classify=false] if specified, the api will classify the boxes accordingly to their last measurements.
* @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.
* @apiParam {Boolean="true","false"} [full=false] if true the API will return populated lastMeasurements (use this with caution for now, expensive on the database)
* @apiParam {String} [near] A comma separated coordinate, if specified, the api will only return senseBoxes within maxDistance (in m) of this location
* @apiParam {Number} [maxDistance=1000] the amount of meters around the near Parameter that the api will search for senseBoxes
* @apiUse ExposureFilterParam
* @apiUse BBoxParam
* @apiSampleRequest https://api.opensensemap.org/boxes
Expand Down Expand Up @@ -574,6 +576,8 @@ module.exports = {
{ name: 'classify', defaultValue: 'false', allowedValues: ['true', 'false'] },
{ name: 'minimal', defaultValue: 'false', allowedValues: ['true', 'false'] },
{ name: 'full', defaultValue: 'false', allowedValues: ['true', 'false'] },
{ name: 'near' },
{ name: 'maxDistance' },
{ predef: 'bbox' },
]),
parseAndValidateTimeParamsForFindAllBoxes,
Expand Down
6 changes: 6 additions & 0 deletions packages/api/lib/helpers/userParamHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ const retrieveParametersPredefs = {
'bbox' () {
return { name: 'bbox', dataType: 'bbox' };
},
'near' () {
return { name: 'near', dataType: 'as-is' };
},
'maxDistance' () {
return { name: 'maxDistance', dataType: 'as-is' };
},
'location' () {
return { name: 'location', dataType: ['location'], paramValidatorAndParser: retrieveLocationParameter }; // dataType array ['location'] is needed for setting the userparam correctly
},
Expand Down
15 changes: 14 additions & 1 deletion packages/models/src/box/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ boxSchema.methods.getLocations = function getLocations ({ format, fromDate, toDa
};

const buildFindBoxesQuery = function buildFindBoxesQuery (opts = {}) {
const { phenomenon, fromDate, toDate, bbox } = opts,
const { phenomenon, fromDate, toDate, bbox, near, maxDistance } = opts,
query = {};

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

// near search parameter
if (near) {
query['currentLocation'] = {
'$near': {
'$geometry': {
type: 'Point',
coordinates: [near.split(',')[0], near.split(',')[1]]
},
'$maxDistance': maxDistance ? maxDistance : 1000,
}
};
}

// search for phenomenon only together with time params
if (fromDate || toDate) {
if (phenomenon) {
Expand Down

0 comments on commit a89ebd4

Please sign in to comment.