[SERVER-67025] $geoWithin a rectangular Polygon results differ from box Created: 04/Jun/22 Updated: 02/May/23 Resolved: 07/Jun/22 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | 4.0.12 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Mark Alvaro | Assignee: | Chris Kelly |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Operating System: | ALL | ||||||||
| Participants: | |||||||||
| Description |
|
When issuing a $geoWithin using a Polygon as the search region, GeoJSON Points near-ish to the region are unexpectedly included or excluded. Since the Polygon in question is actually rectangular, changing to use $geoWithin with a $box fixed the issue. Is this expected? Is there any suggested workaround other than using a box? Here is a sample point and the Polygon-based find that incorrectly matches:
This box-based find, however, works as expected:
and does not match the document. |
| Comments |
| Comment by Mark Alvaro [ 07/Jun/22 ] |
|
Hey there! I know this is closed but just wanted to ask: is there a recommended approach for using MongoDB to store geospatial data that will be rendered on a map with a rectangular/planar/grid coordinate/xy-like viewport, where we only want to retrieve what is in the box? It sounds like the answer is only if you can use legacy coordinates and don't need to use GeoJSON. Is that correct? Basically using your screenshot above, I want to show GeoJSON geometries in the box (not in the polygon) in a map UI component, but I'm still not clear how I could query them. |
| Comment by Chris Kelly [ 07/Jun/22 ] |
|
Hi Mark, Thanks for your report. To demonstrate what's going on, I'm attaching a screenshot of how boxes are rendered versus GeoJSON Polygons with your queries: When using $geometry with type: Polygon, the line that joins two points on a curved surface may or may not contain the same set of co-ordinates that joins those two points on a flat surface. The line that joins two points on a curved surface will be a geodesic. A geodesic denotes the shortest possible line between two points on a sphere in this case. When using $box, $geoWithin returns documents based on grid coordinates and does not query for GeoJSON objects. It is worth noting that $polygon also exists (not using $geometry) which queries on legacy coordinate pairs as well, and does not query GeoJSON objects. Regards, |
| Comment by Mark Alvaro [ 07/Jun/22 ] |
|
Ah but now I see an attachment that makes me wonder about the library we're using to render. |
| Comment by Mark Alvaro [ 07/Jun/22 ] |
|
Wanted to add that I see $box's docs note that it filters based on grid coordinates and not GeoJSON shapes, but the part that still confuses me is when rendered using GeoJSON supporting UI components, the points returned from Mongo appear outside the rendered Polygon, specifically for the Point/Polygon in the example on the ticket description. |