Get distance between multiple points in mongodb Geojson - mongodb

So I've got the following model:
Ride
{
location: {type: "Point", coordinates:[]},
waypoints: {type: "Multipoint", coordinates: [[Number]]}
//array of coordinates
}
I need to be able to get the total distance of ride i.e sum of the distance between any two waypoints. How do I write a query for this. I am assuming multipoint means something in Mongodb
What sort of index do I need on waypoints?
Is this the correct way of storing waypoints?

Related

PostGIS: Linestring length

I am using PostGIS to calculate length of a user-defined linestring. The column is defined as geography(LineString,4326).
The linestring is represented by this GeoJSON:
"track": {
"type": "LineString",
"coordinates": [
[
49.364325571013,
16.785549033597
],
[
49.363254969491,
16.642149334451
]
]
}
SELECT ST_Length("geography") FROM table; returns 15945.7486086962 but the length measured on Google Maps is ~10 km.
What am I doing wrong? How to measure the length to get the same value as from Google Maps?
I believe it is the classic issue of switching x,y positions.
Considering x,y:
SELECT
ST_Length(
ST_GeogFromText('SRID=4326;LINESTRING(49.364325571013 16.785549033597,49.363254969491 16.642149334451)'),true);
st_length
------------------
15869.9069442778
and the "same" LineString switching to y,x ..
SELECT
ST_Length(
ST_GeogFromText('SRID=4326;LINESTRING(16.785549033597 49.364325571013,16.642149334451 49.363254969491)'),true)
st_length
------------------
10416.8606521809
Figured out the problem.
I was using ST_GeomFromText('LINESTRING(lat lon, lat lon)') to create the line. The correct order is lon lat, so ST_GeomFromText('LINESTRING(lon lat, lon lat)').
GeoJSON uses the same order of coordinates: 49.363254969491 = latitude, 16.642149334451 = longitude.
The reason I didn't realize this was because I used Leaflet to draw the line. I basically took the points by geoJSON.getLayers()[0].feature.geometry.coordinates and passed them to a Polyline object. It created the Polyline points by taking the first coordinate from the GeoJSON as Lat, the second as Lng. This way it got reversed the second time and got rendered correctly.
So after switching lon lat in the query I had to use L.GeoJSON.coordsToLatLngs() function to correctly render the line.

in aggregate $neogear "distanceField " is in meters or in another unit?

I'm using the aggregate $geoNear and I don't know if the field distanceField return a value in meters or in another unit
{near : { type: "Point", coordinates: [long,lat] },
distanceField: "dist.calculated",
maxDistance: 50000,
includeLocs:"dist.location",
num: 10, spherical :true }
I only want to know the unit of measurement
Thanks, everyone.
As per documentation:
Specify the distance in meters if the specified point is GeoJSON and in radians if the specified point is legacy coordinate pairs.
So it's in meters in your case since you're specifying a GeoJSON

Is there any way to check whether a google map polygon or circle area is greater than the earth hemisphere

Is there any way to check whether a google map polygon or circle area is greater than the earth hemisphere so that we can know the geowithin queries will not work properly in mongo queries Link.Or what is the condition where the geo spatial query works perfect.
{
<location field>: {
$geoWithin: {
$geometry: {
type: <"Polygon" or "MultiPolygon"> ,
coordinates: [ <coordinates> ]
}
}
}
}
For egs: Area of a polygon can be calculated by:
google.maps.geometry.spherical.computeArea(e.getPath())
where e is the event object varible when the polygon completed event. but how can we compare those with the earth hemisphere area?
how can we compare those with the earth hemisphere area?
Provided that the Earth radius is about 6371 km, you can just calculate the spherical surface area of the hemisphere:
A = 2πr^2
A = 2π(6371)^2
A = 255032236 square km
If the result of google.maps.geometry.spherical.computeArea(e.getPath()) is greater than the above value, then the area polygon/circle is greater than one of the Earth's hemisphere.

Mapbox directions - check distance from routes

I'm using mapbox direction for draw route between two points. I need to check distance from this route to some other marker on map.
code for directions. Thanks.
var directions = L.mapbox.directions({
profile: 'mapbox.driving'
});
directions.setOrigin(L.latLng(lat1, long1));
directions.setDestination(L.latLng(lat2 , long2));
directions.query();
var directionsLayer = L.mapbox.directions.layer(directions).addTo(map);
var directionsRoutesControl = L.mapbox.directions.routesControl('routes', directions)
.addTo(map)
Check the response from the directions query the Route object. The response is an array of n routes. And in the response is distance: Number indicating the distance traveled in meters
You can also use the Mapbox Command Line Interface to see the responses from the directions query. This particular query gives a response of "distance": 642
mapbox-cli> mapbox directions "[-79.4460, 43.6656]" "[-79.4434, 43.6602]"

Convert kilometers to radians

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.
Kilometers to radians or distance and radian conversion
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.
Now face that half toward you and look at the math diagram.
Think of the math diagram as showing from the center of the earth measuring the edge of the earth based on the arc length, after all the earth is curved so any measurement will be curved on the surface of the earth.
Radians are like degrees in a circle and the arc length is literally the distance between A and B in the math diagram.
To you, its a straight line on a bird's eye view, but really it's just a curve in 3d space along the edge of the earth.
Eureka! A lightbulb went on in my head.
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/
Now in regards to Mongo v3.2 specifically using mongoose in node.js
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.
You are correct.
In spherical geometry you divide the distance with the radius of the sphere.
Mind that you should keep the units. So if you calculate the sphere radius in kilometers then you should use distance in kilometer. If you use miles then you should use Earth radius in miles (approx: 3,963.2).
The equatorial radius of the Earth is approximately 3,963.2 miles or 6,378.1 kilometers.
Note : 1 KM = 0.621371 Mile
Here is some simple formulas for calculation :
100 KM to Miles : (100 * 0.621371)
100 KM to radiant : 100 / 6378.1
100 Miles to radiant : 100 / 3963.2
So if you have data in Kilometers then you must use (100 / 6378.1) and for Miles data you can use (100 / 3963.2)
To convert:
distance to radians: divide the distance by the radius of the sphere (e.g. the Earth) in the same units as the distance measurement.
radians to distance: multiply the radian measure by the radius of the sphere (e.g. the Earth) in the units system that you want to convert the distance to.
for Example: if you want to convert 5 miles in radians, then we need to divide the distance(5 miles) by the radius of the sphere(also in miles), which is 3959. then 5/3959 is 0.0012629451...
Thanks