Can I Use MongoDB Geospatial Query to Find Closest LineString to Point?

459 Views Asked by At

I have a collection of geoJSON LineString objects and need to determine which is closest to a point. I don't have much experience with Mongo DB, but have used the $geoNear to find closest points. Is there a way to adapt this to work with a collection of LineStrings?

Example collection:

{
    "_id": ObjectId("5ee3e2deee404124a8ba4382"),
    "geoJSON": {
         "type": "Feature",
         "geometry": {
            "type": "LineString",
            "coordinates": [
                [
                    -85.5,
                    31.0
                ],
                [
                    -85.6,
                    31.0
                ]
            ]
        }
    }
}
{
    "_id": ObjectId("5ee3e2deee404124a8ba4383"),
    "geoJSON": {
         "type": "Feature",
         "geometry": {
            "type": "LineString",
            "coordinates": [
                [
                    -85.55,
                    31.5
                ],
                [
                    -85.6,
                    31.5
                ]
            ]
        }
    }
}
{
    "_id": ObjectId("5ee3e2deee404124a8ba4384"),
    "geoJSON": {
         "type": "Feature",
         "geometry": {
            "type": "LineString",
            "coordinates": [
                [
                    -85.5,
                    32.0
                ],
                [
                    -85.6,
                    32.0
                ]
            ]
        }
    }
}

I'd like to search this collection to determine which line is closest to the point [-85.55, 31.77]. This should return the third line (blue line in the image below). Is there a way to do this efficiently in MongoDB?

Plot

0

There are 0 best solutions below