I need to convert kilometers to radians. Is this correct formula? I need radians for nearsphere in MongoDB.
If I need to convert 5 kilometers to radians I do this:
5/6371
And I get this result (does it seem correct):
0.000784806153
UPDATE
This is not a math issue, I really need to know if I am doing the correct calculations from kilometers to radians to be able to do geospatial queries with MongoDB.
I arrived here, was confused, then I watched some Khan academy videos and it made more sense at that point and then I was able to actually look at equations from other sources to further educate myself.
Here's my train of thought.
I see a diagram about radians and I first think radius from a geolocation point which is wrong.
Instead, imagine the earth cut perfectly in half and just focus on one of the halves.
distance = earth radius * radians
Thus with some very easy algebra...
radians = distance / earth radius
km
radians = distance in km / 6371
mi
radians = distance in mi / 3959
Sometimes thinking it through is fun.
Double-check this... https://www.translatorscafe.com/unit-converter/en/length/7-89/kilometer-Earth%E2%80%99s%20equatorial%20radius/
Despite my best efforts, mongo would not behave correctly as documented for a $geoNear query on a 2d index. never worked
let aggregate = [
{
$geoNear: {
near: { type: 'Point', coordinates: lonLatArray },
spherical: false,
distanceField: 'dist.calculated',
includeLocs: 'dist.location',
maxDistance: distanceInMeters / (6371 * 1000),
query: {
mode: 'nearme',
fcmToken: { $exists: true }
}
}
},
{ $skip: skip },
{ $limit: LIMIT }
];
However, when I changed to a 2dsphere index, it worked perfectly.
let aggregate = [
{
$geoNear: {
near: { type: 'Point', coordinates: lonLatArray },
spherical: true,
distanceField: 'dist.calculated',
includeLocs: 'dist.location',
maxDistance: distanceInMeters,
query: {
mode: 'nearme',
fcmToken: { $exists: true }
}
}
},
{ $skip: skip },
{ $limit: LIMIT }
];
But education never seems like a waste of time.