I've a model contains geojson points. Finding nearest with spring data is quite easy but how can retrieve nearest location for a giving route?
I am getting the route information from google:
http://maps.googleapis.com/maps/api/directions/xml?origin=48.208174,16.373819&destination=48.340670,16.717540&sensor=false&units=metric&mode=driving
MongoDB geolocation requires that a collection have at most only one 2dsphere index, so create an index on location object. Now we will be creating a route in NodeJS and MongoDB Query to get the nearest results according to user location.
Documents Query One of the more common ways to query MongoDB with Spring Data is by making use of the Query and Criteria classes – which very closely mirror native operators. 2.1. Is This is simply a criterion using equality – let's see how it works.
MongoDB uses the same syntax as the US Geological Survey or other persons working with maps would use. For example, a simple point is represented like this: On the globe, longitude < 0 mean west of Greenwich, England, by convention. Numbers > 0 are to the east.
Starting in MongoDB 4.0, $near queries are supported for sharded collections. In earlier MongoDB versions, $near queries are not supported for sharded collections; instead, for sharded clusters, you must use the $geoNear aggregation stage or the geoNear command (available in MongoDB 4.0 and earlier).
The route information from the maps googleapi is broken down into steps that have a start location and end location with latitude/longitude coordinates.
Computing the distance of the points in your model to all the start/end locations in the route would give you a measure of how far the point is from the route. The minimum 'distance' from the route start/end points would be the nearest location to the route.
You can optimize the computation by discard any points when the computed distance is greater than the previous minimum cumulative distance.
Google maps api returns 'steps' of the route, which has coordinates of the edges of that stretch. You can use those edges to create extrapolated points on that straight stretch. Lets call them p1,p2,p3,p4...pN. Then you run $near query in your database for these points, you will get nearest locations around that route.
Open street map database gives information of coordinates of the route, which you can use to supplement your data. Detailed answer here : Get exact geo coordinates along an entire route, Google Maps or OpenStreetMap
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With