Prakki
Prakki

Reputation: 159

MongoDB find polygon coordinates using single point and radius

I have a collection with many polygon coordinates, which represent the different areas. What my objective is I will send a lat, long and radius in a mongodb query which should return the polygon coordinates that falls within circle area.

Polygon coordinates:

{
    "_id" : "300",
    "name" : "MeKesler",
    "centroid" : [ 
        0, 
        0
    ],
    "type" : "cnbd_id",
    "coords" : [ 
        [ 
            39.8620784017, 
            -86.14614844330004
        ], 
        [ 
            39.8625395793, 
            -86.15442037579999
        ], 
        [ 
            39.8593030353, 
            -86.156373024
        ], 
        [ 
            39.8586935926, 
            -86.15669488909998
        ], 
        [ 
            39.8534225112, 
            -86.15854024890001
        ], 
        [ 
            39.850391456, 
            -86.1589050293
        ], 
        [ 
            39.8511657057, 
            -86.1479830742
        ], 
        [ 
            39.8511986523, 
            -86.14598751070002
        ], 
        [ 
            39.856881704, 
            -86.14605188370001
        ], 
        [ 
            39.8575241063, 
            -86.14605188370001
        ], 
        [ 
            39.8620784017, 
            -86.14614844330004
        ]
    ]
}

My query:

db.collection.find({
    "coords" : {
        "$within" : { 
            "$center" : [ 
                [ 39.863110, -86.168456], 
                2/69.1
            ]
        }
    }
}) 

Can anyone help on this?

Upvotes: 0

Views: 1483

Answers (1)

ffflabs
ffflabs

Reputation: 17491

I believe the problem is that your document is not valid geojson. The valid syntax for your polygon would be

{
    "_id": "300",
    "name": "MeKesler",
    "centroid": {
        type: "Point",
        coordinates: [0, 0]
    },
    "type": "cnbd_id",

    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [39.8620784017, -86.14614844330004],
                [39.8625395793, -86.15442037579999],
                [39.8593030353, -86.156373024],
                [39.8586935926, -86.15669488909998],
                [39.8534225112, -86.15854024890001],
                [39.850391456, -86.1589050293],
                [39.8511657057, -86.1479830742],
                [39.8511986523, -86.14598751070002],
                [39.856881704, -86.14605188370001],
                [39.8575241063, -86.14605188370001],
                [39.8620784017, -86.14614844330004]
            ]
        ]
    }
}

Also, you should consider creating a 2dsphere index on the geometry field.

Lastly, the geo query should use the $geoWithin operator, and run against the polygon, not against its coordinates.

db.collection.find({
    "geometry" : {
        "$geoWithin" : { 
            "$center" : [ 
                [ 39.863110, -86.168456], 
                2/69.1
            ]
        }
    }
}) 

Upvotes: 2

Related Questions