mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-25 11:49:53 -05:00
### Does this PR have any customer impact? No ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) Bugfix ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) No ### Release Note (Refer [Template](../docs/release_notes/pgmongo_template.md)) ### Description This PR addresses compatibility issues between $geoWithin and ST_Covers. It aims to fix some issues and outline others that are not easily resolved at the moment. **Issue 1**: PostGIS uses a clockwise orientation rule for polygons, where exterior rings should be oriented clockwise and hole rings should be oriented counterclockwise. This is different from the GeoJSON standard specification, which is the opposite. Therefore, we need to ensure that all our polygons follow a clockwise orientation to be modeled correctly. Few referrence where the difference is mention: 1- https://gis.stackexchange.com/questions/306567/postgis-geojson-query-not-validating#comment496061_306571 2- https://dev.to/rodreegez/geojson-right-hand-rule-2idk **Solution**: This PR fixes the issue by forcing polygons to follow a clockwise orientation. This resolves some cases where a polygon sharing a boundary point is considered a match in ST_Covers. Without this fix, the same polygon or any shape sharing a boundary point would not be considered a match. **Issue 2**: For a Multi*/Multi* GeoJSON type coverage, PostGIS expects a single component to cover all the components of the other GeoJSON, which is not the behavior in Mongo. **Solution**: This PR fixes the issue by reading individual components from Multi*/GeometryCollection GeoJSON types and calling ST_Covers for each one individually. If all components are covered by the query, it is considered a match. **Issue 3**: The coverage of polygons with holes is faulty in PostGIS, and there is no workaround for this issue. These limitations are documented and should throw not supported error for now until we fix this in pgmongo layer. Mongo DB also has some undefined behaviors as they don't guarantee degeneracy https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/#degenerate-geometry, where the polygon doesn't guarantee to contain another polygon sharing its boundaries. Also along with this PR the wkb iterator now follows a visitor patter in order to be reusable for other scenarios as well e.g. wkb traversing logic of $centerSphere is now also used for multi multi geowithin checks Related work items: #2958169