[SERVER-78343] geoWithin not returning correct data Created: 22/Jun/23 Updated: 29/Oct/23 Resolved: 07/Sep/23 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Khalid Bashir Bajwa | Assignee: | Will Buerger |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | greenerbuild | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Assigned Teams: |
Query Integration
|
||||||||
| Operating System: | ALL | ||||||||
| Steps To Reproduce: | Here is an example collection db.geo_json_test.insertMany([{name: "00A", location: {type: "Point", coordinates: [-81.09670311007132, 46.575020657518266]}},{name: "147", location: {type: "Point", coordinates: [-81.067788, 46.3822689]}},{name: "001", location: {type: "Point", coordinates: [-92.16504975147868, 66.85083025648714]}},{name: "002", location: {type: "Point", coordinates: [-80.9422811, 46.512260000000005]}},{name: "000", location: {type: "Point", coordinates: [-80.942295, 46.51226270000001]}},{name: "008", location: {type: "Point", coordinates: [-81.09670311007132, 46.575020657518266]}},{name: "007", location: {type: "Point", coordinates: [-81.09653748648944, 46.575012427925024]}},{name: "006", location: {type: "Point", coordinates: [-81.0972791024576, 46.57375187851832]}},{name: "005", location: {type: "Point", coordinates: [-81.101199979413, 46.56988771702767]}},{name: "004", location: {type: "Point", coordinates: [-81.05537133207827, 46.56975708845099]}},{name: "003", location: {type: "Point", coordinates: [-81.09831646261244, 46.57729676918206]}},{name: "02", location: {type: "Point", coordinates: [-81.07875689437888, 46.56184186179797]}}])
The following query to the collection returns all points db.geo_json_test.find({'$and': [{'location': {'$geoWithin': {'$geometry': {'type': 'Polygon', 'coordinates': [[[-13, 71], [-161, 71], [-161, 16], [-13, 16], [-13, 71]]]}}}}]})
However, the following query which is changing the longitude by 1 unit further to the east returns only one point. This polygon is a little bigger than the previous one and should include all the points that were included in the previous one db.geo_json_test.find({'$and': [{'location': {'$geoWithin': {'$geometry': {'type': 'Polygon', 'coordinates': [[[-12, 71], [-161, 71], [-161, 16], [-12, 16], [-12, 71]]]}}}}]}) |
||||||||
| Sprint: | QI 2023-07-24, QI 2023-08-07, QI 2023-08-21, QI 2023-09-04, QI 2023-09-18 | ||||||||
| Participants: | |||||||||
| Description |
|
The geoWithin query is not returning the data as expected. I have looked at large polygons mentioned here
|
| Comments |
| Comment by Khalid Bashir Bajwa [ 29/Oct/23 ] |
|
Thank you will.buerger@mongodb.com. $polygon worked. |
| Comment by Will Buerger [ 07/Sep/23 ] |
|
Hi khalidbashirbajwa@gmail.com, The results you’re receiving are as expected. When using the $geoWithin operator, there are 2 “modes” with which the operator can check the geospatial condition. When you provide a shape with the geoJSON specification, with the $geometry operator, it checks for containment using the geodesic coordinate system, simulating the earth as a sphere. When you provide a shape with a non-geoJSON geometry, like the $polygon operator, it checks for containment using planar (flat) geometry, like looking at a map. Geodesic lines do not appear straight when projected onto a flat Cartesian coordinate system and can sometimes appear dramatically different than the Cartesian projection, as in this case. You can see by comparing these two visualizations that when you move one of the vertices in a way that you might think of as “expanding” the polygon, it actually causes the point to fall outside the resulting geodesic line. Before: http://www.gcmap.com/mapui?P=16N+161W+-+16N+13W%2C+46.57502N+81.0976W&MS=bm&DU=mi After: http://www.gcmap.com/mapui?P=16N+161W+-+16N+12W%2C+46.57502N+81.0976W&MS=bm&DU=mi You might consider using $polygon rather than $geometry, if you want the results as you’ve described. Will |