-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix: Improve jts polygon serde logic #26672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sullivan-Patrick
wants to merge
1
commit into
prestodb:master
Choose a base branch
from
Sullivan-Patrick:improve-polygon-serde
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -454,7 +454,7 @@ public void testGeometryInvalidReason() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // invalid geometries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertInvalidReason("MULTIPOINT ((0 0), (0 1), (1 1), (0 1))", "[MultiPoint] Repeated point: (0.0 1.0)"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertInvalidReason("LINESTRING (0 0, -1 0.5, 0 1, 1 1, 1 0, 0 1, 0 0)", "[LineString] Self-intersection at or near: (0.0 1.0)"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertInvalidReason("POLYGON ((0 0, 1 1, 0 1, 1 0, 0 0))", "Error constructing Polygon: shell is empty but holes are not"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertInvalidReason("POLYGON ((0 0, 1 1, 0 1, 1 0, 0 0))", "Self-intersection"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertInvalidReason("POLYGON ((0 0, 0 1, 0 1, 1 1, 1 0, 0 0), (2 2, 2 3, 3 3, 3 2, 2 2))", "Hole lies outside shell"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertInvalidReason("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0), (2 2, 2 3, 3 3, 3 2, 2 2))", "Hole lies outside shell"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertInvalidReason("POLYGON ((0 0, 0 1, 2 1, 1 1, 1 0, 0 0))", "Ring Self-intersection"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1372,6 +1372,124 @@ private void assertInvalidGeometryJson(String json) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertInvalidFunction("geometry_from_geojson('" + json + "')", "Invalid GeoJSON:.*"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void testDegeneratePolygons() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Single polygon with CCW orientation - should orient to CW | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFunc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "POLYGON ((1 2, 3 4, 5 7, 1 2))", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "POLYGON ((1 2, 5 7, 3 4, 1 2))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Single polygons with no area- should not reverse these | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFunc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "POLYGON ((1 2, 5 6, 3 4, 1 2))", "POLYGON ((1 2, 5 6, 3 4, 1 2))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFunc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "POLYGON ((1 2, 3 4, 5 6, 1 2))", "POLYGON ((1 2, 3 4, 5 6, 1 2))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Single polygons with interior rings- should canonicalize so any shells have | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // CW orientation and holes have CCW orientation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFunc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 3 7, 7 7, 7 3, 3 3))", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFunc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3))", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Multipolygons where polygons after the first are CCW for shell or CW for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // hole. These should be correctly oriented after serde. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has CW shell and CCW hole, second polygon has CCW | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // shell and CCW hole -> second polygon shell should be reoriented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFunc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 20 0, 20 20, 0 20, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has CW shell and CW hole, second polygon has CCW | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // shell and CCW hole -> first polygon hole and second polygon shell should be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // reoriented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFunc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 3 7, 7 7, 7 3, 3 3)), ((0 0, 20 0, 20 20, 0 20, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has CCW shell and CW hole, second polygon has CCW | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // shell and CW hole -> both polygons should have shell and hole reoriented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFunc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 10 0, 10 10, 0 10, 0 0), (3 3, 3 7, 7 7, 7 3, 3 3)), ((0 0, 20 0, 20 20, 0 20, 0 0), (6 6, 6 14, 14 14, 14 6, 6 6)))", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has CCW shell and CCW hole, second polygon has CCW | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // shell and CCW hole -> both polygons should have shells reoriented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFunc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 10 0, 10 10, 0 10, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 20 0, 20 20, 0 20, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has CW shell and CW hole, second polygon has CW | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // shell and CW hole -> both polygons should have holes reoriented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFunc( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 3 7, 7 7, 7 3, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 6 14, 14 14, 14 6, 6 6)))", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // MultiPolygons with zero-area rings. These need to fail because our | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // serialization format holds MultiPolygons as single vectors that rely on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // orientation for determining shell start points. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Second polygon is zero area | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFuncInvalid("MULTIPOLYGON (((1 1, 2 1, 2 2, 1 1)), ((1 1, 2 2, 3 3, 2 2, 1 1)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Single polygon with zero area | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFuncInvalid( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((5 10, 25 30, 15 20, 5 10)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFuncInvalid( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((1 1, 1 2, 1 3, 1 1)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1433
to
+1444
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Missing test for single Polygon with zero-area ring (should succeed). Please add a test for a single Polygon with a zero-area ring to verify that it passes, as required by the PR.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has CW shell and CCW hole, second polygon has CW shell and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // zero-area hole | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFuncInvalid( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 9 9, 14 14, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has CW shell and CCW hole, second polygon has zero-area shell | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // and CCW hole | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFuncInvalid( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 0 20, 0 10, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has CW shell and CCW hole, second polygon has CW shell | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // and zero-area hole | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFuncInvalid( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 9 9, 14 14, 9 9, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has CW shell and CCW hole, second polygon has zero-area shell | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // and zero-area hole | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFuncInvalid( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 0 20, 0 10, 0 0), (6 6, 9 9, 14 14, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has zero-area shell and CCW hole, second polygon has CW shell | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // and CCW hole | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFuncInvalid( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 0 15, 0 0), (3 3, 7 3, 7 7, 3 7, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6))))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has CW shell and zero-area hole, second polygon has CW shell | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // and CCW hole | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFuncInvalid( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 7 3, 9 3, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // First polygon has zero-area shell and zero-area hole, second polygon has CW | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // shell and CCW hole | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testDegeneratePolygonsFuncInvalid( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "MULTIPOLYGON (((0 0, 0 10, 0 5, 0 10, 0 0), (3 3, 7 3, 9 3, 3 3)), ((0 0, 0 20, 20 20, 20 0, 0 0), (6 6, 14 6, 14 14, 6 14, 6 6)))"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void testDegeneratePolygonsFunc(String wkt, String expected) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertFunction(format("ST_ASText(ST_GeometryFromText('%s'))", wkt), VARCHAR, expected); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void testDegeneratePolygonsFuncInvalid(String wkt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assertInvalidFunction(format("ST_ASText(ST_GeometryFromText('%s'))", wkt), "Input MultiPolygon contains one or more zero-area rings."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void testGooglePolylineDecode() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: The comment above this line incorrectly states the required orientation.
Update the comment to state that shells must be clockwise and holes must be counter-clockwise, matching the code's logic.