简体   繁体   中英

Can we check by using geospatial MongoDB query whether a point[lat ,long] lies inside polygon?

I have a below data of lat long which represents polygon of a society.I have 5000 records of type data like below.I want to use mongodb geospatial queries to check if a given point(lat,long) (12.948,77.66) lies inside that society list [5000 records].Can i achieve it using mongodb geospatial queries.If yes then what should be the structure of documents in mongodb and what should be the mongodb query?.Below is the sample data of one society polygon points.

Data:

"geofence": [
                {
                    "latitude": 12.9475827,
                    "longitude": 77.67696
                },
                {
                    "latitude": 12.9477697,
                    "longitude": 77.674655
                },
                {
                    "latitude": 12.9463797,
                    "longitude": 77.67454
                },
                {
                    "latitude": 12.946131,
                    "longitude": 77.675466
                },
                {
                    "latitude": 12.947066,
                    "longitude": 77.675473
                },
                {
                    "latitude": 12.947198,
                    "longitude": 77.676847
                },
                {
                    "latitude": 12.94757,
                    "longitude": 77.67695
                }
]

Found the answer for this.

Storing in MongoDB in form of geo-json polygon using below java code:

GeoJsonPolygon geoJsonPolygon = new GeoJsonPolygon(pointsList);

MongoDB query

db.mygate_geofencing.find({polygons:
                 {$geoIntersects:
                     {$geometry:{ "type" : "Point",
                          "coordinates" : [ 12.947874, 77.677494] }
                      }
                  }
             });

Java Code:

Creating a point and then checking the intersecting polygons.

GeoJsonPoint geoJsonPoint = new GeoJsonPoint(truncate(customerLat,6),truncate(customerLng,6));
Query query = new Query().addCriteria(Criteria.where("polygons").intersects(geoJsonPoint));
List<GeoFencing> mygateGeoFencingList = mongoTemplate.find(query, GeoFencing.class);

GeoFencing Class

@Id
@Field("_id")
private String id;

@Field("polygons")
private GeoJsonPolygon polygons;

@Field("societyName")
private String societyName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM