Geofence report using MongoDB - "the right way"

I have a collection of millions of GPS events and a collection of several user-defined geophotos. Geofences are geoJSON polygons.

My mission is to allow my users to select geofence, time range, and then generate a report that shows when their car entered geofencing and then when it left.

The first approach I took is pretty simple and doesn't use any of MongoDB's geospatial features:

  • Run a query to get a list of all GPS events for a time range.
  • Stream (using mongoose QueryStream) the results and notice the first GPS event that goes into geofencing, and then notice the next GPS event that goes out. Insert these 2 events into an array. A simple point-in-polygon function is used to determine if a GPS event is inside or outside the geoforums.
  • The result looks like this: [{start: start_event, finish: finish_event}, {start: start_event, finish: finish_event}]

Of course, this is probably the most inefficient way to do it, and of course, if I increase the date range in a couple of days, the time it takes becomes too long.

There are two other approaches I am thinking of:

  • Instead of tracing all the events using the process above, first limit the list of GPS events to those near the geofence. For example, if I can work out the bounding box of a geogonal polygon, I can define maxDistance to query $ geoNear. This should significantly reduce the number of events that need to be handled using the same approach as above.

  • Use the $ geoWithin query to select only events that are within the geopotential. The problem with this is that it is more difficult to determine when the vehicle was entering or exiting. I would have to rely on some time threshold to determine the sessions when the vehicle was inside the geofence.

At this point, I'm leaning towards approach # 1, but I'd love to hear how other people solve this problem with MongoDB - specifically using the aggregation pipeline or mapreduce.

+3


source to share





All Articles