Skip to content

Commit

Permalink
Merge pull request PastVu#431 from kabalin/fix-nearest
Browse files Browse the repository at this point in the history
Fix nearest photos listing in photo view mode.
  • Loading branch information
kabalin authored Dec 5, 2021
2 parents 28514a5 + 20df77c commit 889c1f8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
17 changes: 17 additions & 0 deletions commons/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,26 @@ Utils.geo = (function () {
return d;
}

/**
* Degrees to radians
*
* @param {number} deg
* @returns {number}
*/
function deg2rad(deg) {
return deg * (Math.PI / 180);
}

/**
* Radians to meters (assuming a spherical Earth)
*
* @param {number} rad
* @returns {number}
*/
function rad2meter(rad) {
return turf.radiansToLength(rad, 'metres');
}

function geoToPrecision(geo, precision) {
_.forEach(geo, (item, index, array) => {
array[index] = Utils.math.toPrecision(item, precision || 6);
Expand Down Expand Up @@ -813,6 +829,7 @@ Utils.geo = (function () {

return {
deg2rad,
rad2meter,
geoToPrecision,
geoToPrecisionRound,
getDistanceFromLatLonInKm,
Expand Down
20 changes: 17 additions & 3 deletions controllers/photo.js
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,20 @@ async function giveUserPhotosAround({ cid, limitL, limitR }) {
return { left, right };
}

// Returns array of nearest photos
/**
* Returns array of photos nearest to specified point.
*
* @param {object} obj
* @param {Array} obj.geo Point coordinate pair
* @param {number} obj.type
* @param {number} obj.year
* @param {number} obj.year2
* @param {number} obj.except cid to exclude
* @param {number} obj.distance distance in radians
* @param {number} obj.limit
* @param {number} obj.skip
* @returns {object[]} photos
*/
async function giveNearestPhotos({ geo, type, year, year2, except, distance, limit, skip }) {
if (!Utils.geo.checkLatLng(geo)) {
throw new BadParamsError();
Expand Down Expand Up @@ -1964,10 +1977,11 @@ async function giveNearestPhotos({ geo, type, year, year2, except, distance, lim
query.cid = { $ne: except };
}

// For 2dsphere GeoJSON point $maxDistance is specified in meters.
if (_.isNumber(distance) && distance > 0 && distance < 7) {
query.geo.$nearSphere.$maxDistance = distance;
query.geo.$nearSphere.$maxDistance = Utils.geo.rad2meter(distance);
} else {
query.geo.$nearSphere.$maxDistance = 2;
query.geo.$nearSphere.$maxDistance = Utils.geo.rad2meter(2);
}

if (_.isNumber(limit) && limit > 0 && limit < 30) {
Expand Down

0 comments on commit 889c1f8

Please sign in to comment.