[SERVER-17092] $geoWithin does not return result on certains Polygons Created: 28/Jan/15  Updated: 28/Apr/18  Resolved: 28/Jan/15

Status: Closed
Project: Core Server
Component/s: Geo
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Michael THOMAS [X] Assignee: Siyuan Zhou
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File polygon view on sphere.png     JPEG File polygon-view.jpg    
Issue Links:
Related
related to SERVER-19328 $geoIntersects does not work as expected Closed
is related to SERVER-34699 $geoWithin doesn't return correct geo... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

1. Create dummy collection

test.js

 
db['mongo-test'].insert({
    "city" : "Paris",
    "geom" : {
        "type" : "Point",
        "coordinates" : [ 
            2.3522219, 
            48.856614
        ]
    }
});

2. Create 2dsphere index

test.js

db['mongo-test'].ensureIndex({ geom : '2dsphere'});

3. Request with a non-working Polygon !

test.js

db['mongo-test'].find({"geom":{"$geoWithin":{"$geometry":{"type":"Polygon","coordinates":[[[-97.20703125,15.623036831528264],[-97.20703125,67.20403234340081],[105.46875,67.20403234340081],[105.46875,15.623036831528264],[-97.20703125,15.623036831528264]]]}}}})

No result is return

The lat/lng of the record is Paris, and France is entirely in the given Polygon (see attachment)

Participants:

 Description   

Dear,

I encounter a special comportment with the $geoWithin command. with certains $box or $geometry, the request does not return any result.

I try with small polygon and zoom out to bigger (bigger always contains smaller), at a certain range, with exactly the same request, no more result appears !

Maybe i missed something.

Best Regards,

Michael.



 Comments   
Comment by Siyuan Zhou [ 28/Jan/15 ]

Micatom,

I guess your question is "Is $box different with GeoJSON polygon?"

Yes. They are different. $box assumes the points you have are on a flat surface, while GeoJSON polygon will be interpreted as a shape on spherical surface, so the distance calculation and geometry relationship are different. Please see the documentation for more details. $box and GeoJSON $geoWithin. I recommend using GeoJSON since it gives the accurate result in your case and 2dsphere index will speed it up well.

Thanks,
Siyuan

Comment by Michael THOMAS [X] [ 28/Jan/15 ]

Yes, it's what my tests brings me to... 4 ways to draw a quad points polygon in a sphere . I note the solution of adding points to ensure the right polygon selection. For now the bounding-box runs well for me.

Is the bounding box is proceed differently from quad the polygon before to query the index ?

You're doing a Great Job, thanks for your quick answer.

Best Regards,
Michael.

Comment by Siyuan Zhou [ 28/Jan/15 ]

Micatom,

The shortest path between longitude -112 and 117 is actually on the opposite of the sphere, as shown on the attached polygon view on sphere. To specify the polygon you want, please add some middle points along the edge of polygon, so that the longitude distance between any adjacent 2 points is less than 180 degree. Just FYI, another potentially unexpected effect is that the distortion of map projection. As you may have noticed, the edge is actually a curve on the map.

If the geometry of query keeps growing, for geometries that have an area greater than the area of a single hemisphere, MongoDB queries for the smaller of the complementary geometries. Upcoming MongoDB 3.0 supports "Big Polygon" which defines a strict winding order for polygons to support polygon whose area is greater than a single hemisphere.

Hope this answered your question. I am going to close this issue as "Working as Designed".

Thanks,
Siyuan

Comment by Michael THOMAS [X] [ 28/Jan/15 ]

With $geometry, Making tests with somes Polygons, it seems, it return the opposite of the requested polygon (as if the inverted polygon was applyed)

Comment by Michael THOMAS [X] [ 28/Jan/15 ]

Correction : Using the bouding box command, we got the elements.

db['mongo-test'].find(
 {
    "geom":{"$geoWithin":
     {
      "$box": [[-112.1484375,5.266007882805511],[117.7734375,72.28906720017675]]
     }}}
);

Generated at Thu Feb 08 03:43:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.