Geospatial Queries for Line Intersection

Hi, I’m in the process of determining whether I should use MongoDB or Postgres (or something else) for an app that will be making pretty intensive geospatial queries.

More specifically, they will involve determining the paths (a MultiLineString?) a coordinate point would be close to, and based on a matching timestamp.

The vice versa would also need to happen. Given a path of coordinates, I would need to query which coordinate points this path got close to.

Could MongoDB be the right tool for the job? Is MongoDB able to support this out of the box or with a relatively idiomatic query construction? This post seems to suggest using $centerSphere with $geoWithin would work despite the docs saying

Selects documents with geospatial data that exists entirely within a specified shape.

Even if so and works with a multi-point path, can the query still work in vice versa where
the points are queried given a path? Thanks!

Hi @cheng_soul, welcome!

Could you elaborate more on what you’re trying to do with example inputs, queries (both cases), and the expected output?

The use of $centerSphere with $geoWithin only works for Points, LineStrings, MultiLineStrings within the defined circle (spherical). For detecting intersections, there is currently an open ticket for this SERVER-30390 (Please upvote or add yourself as a watcher for notifications).


Thanks! So if I say define the circle as the entire Earth, can I use those LineStrings and MultiLineStings and find where they intersect? Basically I’m looking for a database to track where people have traveled and whether they have crossed paths.

Hi @cheng_soul,

For that use case, it’s likely better to use $geoIntersects instead. It’s used to query documents whose geospatial data intersects with a specified GeoJSON object.

Please see Tutorial: Find Restaurants with Geospatial Queries