Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data - MongoDb finding nearest locations around a route

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

like image 458
aymeba Avatar asked Jan 28 '16 20:01

aymeba


People also ask

How to get the nearest location in MongoDB using MongoDB query?

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.

How to query MongoDB with spring data?

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.

What is the syntax of the map in MongoDB?

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.

Are near queries supported for sharded collections in MongoDB?

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).


2 Answers

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.

like image 87
Ron Puri Avatar answered Oct 07 '22 14:10

Ron Puri


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.

enter image description here

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

like image 39
DhruvPathak Avatar answered Oct 07 '22 14:10

DhruvPathak