How to find polygon in geoJson saved in MongoDB

998 views Asked by At

I got some geoJSON files that I put into a MongoDB base. The geoJSON schema are standardized, so I expect to do not change it. The geoJSON files represents tagged defined by a set of area (polygons) A tag may hold many polygons Different tags may hold same areas or polygons.

The geoJSON file is like :

{
    "_id": {
        "$oid": "5570864ee4b08fb4e548beb6"
    },
    "type": "FeatureCollection",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "features": [
        {
            "type": "Feature",
            "properties": {
                "ida": "2335",
                "name": "myName",                    
                 ...,
                "surf_m2": 13432292
            },
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                -1.366403393656118,
                                47.19539498257428
                            ],
                            [
                                -1.357701323667323,
                                47.19374649592587
                            ],
                            [
                                -1.357566935700819,
                                47.1932311120653
                            ],
                            [
                                -1.357111040694128,
                                47.192966611925904
                            ],
                            ...
                        ]
                    ]
                ]
            }
        }
    ]
}

Image I have many documents like this.

How can I query mongoDB in the shell to find area (by name, id or whatever) that are within the polygons available in the base.

I have tryed this :

db.Collection.find({"features":{"$geoIntersects":{"$geometry":{"type":"Point", "coordinates":[-1.3545706,47.166627 ]}}}})

Or

db.runCommand( { geoNear : "Collection" ,near : { type : "Point",coordinates:[-1.3545706,47.166627]},spherical : true }

My issue is that I wasn't able to reach the coordinates as it is not directly accessible at root level, but at features.geometry.coordinates.

Help...

1

There are 1 answers

0
Anthony On BEST ANSWER

Here is how you can do it runtime, by replacing the GPS position by the one you want.

db.CollectionName.find({"features.geometry":{"$geoIntersects":{"$geometry":{"type":"Point", "coordinates":[ -1.346784,47.141102]}}}})