google / s2geometry

Computational geometry and spatial indexing on the sphere
http://s2geometry.io/
Apache License 2.0
2.29k stars 302 forks source link

Contains query on polygon returns unexpected result. #265

Closed metonymic-smokey closed 2 years ago

metonymic-smokey commented 2 years ago

Discussed in https://github.com/google/s2geometry/discussions/264

Originally posted by **metonymic-smokey** July 18, 2022 Hey @jmr , The query I ran was a 'contains' query on a polygon, with the indexed shapes being points. One of the points I received as a hit was unexpected since it lies outside the polygon, although it's close to the edge. The below screenshot is a visualisation of the query polygon and the point returned: ![image](https://user-images.githubusercontent.com/48997495/179445003-d1cf3620-f57f-43ef-881d-0148c8abdefa.png) This is the test for this case: ``` TEST(S2NonWithIn, AllCases) { vector pts(4); S2LatLng latlng; pts[0] = latlng.FromDegrees(41.959368254132166, 7.222348606851546).ToPoint(); pts[1] = latlng.FromDegrees(2.3184942494899996, 13.459529974343564).ToPoint(); pts[2] = latlng.FromDegrees(7.813467030273539, 61.085156337800264).ToPoint(); pts[3] = latlng.FromDegrees(41.00990312902452, 74.64247754703456).ToPoint(); S2Loop* loop = new S2Loop(pts); EXPECT_EQ(true, loop->IsValid()); EXPECT_EQ(true, loop->IsNormalized()); vector> loops; loops.emplace_back(loop); S2Polygon polygon1 = S2Polygon(std::move(loops)); EXPECT_EQ(true, polygon1.IsValid()); // point lies outside S2Point point = latlng.FromDegrees(41.44166591306549, 54.18990767747903).ToPoint(); bool contains = polygon1.Contains(point); EXPECT_EQ(false, contains); } ``` I have validated the vertices using [geojsonlint](https://geojsonlint.com/), so I think the polygon vertices would comply with the spec. Any leads on these results would be appreciated. Thanks!
jmr commented 2 years ago

Your visualization is misleading since the edges aren't drawn as geodesics. The point is inside.

http://www.gcmap.com/mapui?P=41.959368254132166%2C+7.222348606851546+-+41.00990312902452%2C+74.64247754703456%0D%0A41.44166591306549%2C+54.18990767747903&MS=wls&DU=mi

Don't double post by starting a discussion and opening a bug.