-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
geo/geomfn: implement ST_MakePolygon({geometry,_geometry}) #50979
Merged
Merged
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 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 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package geomfn | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/pkg/geo" | ||
"github.com/cockroachdb/errors" | ||
"github.com/twpayne/go-geom" | ||
) | ||
|
||
// MakePolygon creates a Polygon geometry from linestring and optional inner linestrings. | ||
// Returns errors if geometries are not linestrings. | ||
func MakePolygon(outer *geo.Geometry, interior ...*geo.Geometry) (*geo.Geometry, error) { | ||
layout := geom.XY | ||
outerGeomT, err := outer.AsGeomT() | ||
if err != nil { | ||
return nil, err | ||
} | ||
outerRing, ok := outerGeomT.(*geom.LineString) | ||
if !ok { | ||
return nil, errors.Newf("argument must be LINESTRING geometries") | ||
} | ||
if outerRing.NumCoords() < 4 { | ||
return nil, errors.Newf("shell must have at least 4 points") | ||
} | ||
if !isClosed(layout, outerRing) { | ||
return nil, errors.Newf("shell must be closed") | ||
} | ||
srid := outerRing.SRID() | ||
coords := make([][]geom.Coord, len(interior)+1) | ||
coords[0] = outerRing.Coords() | ||
for i, g := range interior { | ||
interiorRingGeomT, err := g.AsGeomT() | ||
if err != nil { | ||
return nil, err | ||
} | ||
interiorRing, ok := interiorRingGeomT.(*geom.LineString) | ||
if !ok { | ||
return nil, errors.Newf("argument must be LINESTRING geometries") | ||
} | ||
if interiorRing.SRID() != srid { | ||
return nil, errors.Newf("mixed SRIDs are not allowed") | ||
} | ||
if interiorRing.NumCoords() < 4 { | ||
return nil, errors.Newf("holes must have at least 4 points") | ||
} | ||
if !isClosed(layout, interiorRing) { | ||
return nil, errors.Newf("holes must be closed") | ||
} | ||
coords[i+1] = interiorRing.Coords() | ||
} | ||
|
||
polygon, err := geom.NewPolygon(layout).SetSRID(srid).SetCoords(coords) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return geo.NewGeometryFromGeomT(polygon) | ||
} | ||
|
||
// isClosed checks if a LineString is closed to make a valid Polygon. | ||
// Returns whether the last coordinate is the same as the first. | ||
func isClosed(layout geom.Layout, g *geom.LineString) bool { | ||
return g.Coord(0).Equal(layout, g.Coord(g.NumCoords()-1)) | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,260 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package geomfn | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/geo" | ||
"github.com/cockroachdb/cockroach/pkg/geo/geopb" | ||
"github.com/cockroachdb/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMakePolygon(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
outer string | ||
outerSRID geopb.SRID | ||
interior []string | ||
interiorSRIDs []geopb.SRID | ||
expected string | ||
expectedSRID geopb.SRID | ||
err error | ||
}{ | ||
{ | ||
"Single input variant - 2D", | ||
"LINESTRING(75 29,77 29,77 29, 75 29)", | ||
geopb.DefaultGeometrySRID, | ||
[]string{}, | ||
[]geopb.SRID{}, | ||
"POLYGON((75 29,77 29,77 29,75 29))", | ||
geopb.DefaultGeometrySRID, | ||
nil, | ||
}, | ||
{ | ||
"Single input variant - 2D with SRID", | ||
"LINESTRING(75 29,77 29,77 29, 75 29)", | ||
geopb.SRID(4326), | ||
[]string{}, | ||
[]geopb.SRID{}, | ||
"POLYGON((75 29,77 29,77 29,75 29))", | ||
geopb.SRID(4326), | ||
nil, | ||
}, | ||
{ | ||
"Single input variant - 2D square", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.DefaultGeometrySRID, | ||
[]string{}, | ||
[]geopb.SRID{}, | ||
"POLYGON((40 80, 80 80, 80 40, 40 40, 40 80))", | ||
geopb.DefaultGeometrySRID, | ||
nil, | ||
}, | ||
{ | ||
"With inner holes variant - 2D single interior ring", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.DefaultGeometrySRID, | ||
[]string{ | ||
"LINESTRING(50 70, 70 70, 70 50, 50 50, 50 70)", | ||
}, | ||
[]geopb.SRID{geopb.DefaultGeometrySRID}, | ||
"POLYGON((40 80,80 80,80 40,40 40,40 80),(50 70,70 70,70 50,50 50,50 70))", | ||
geopb.DefaultGeometrySRID, | ||
nil, | ||
}, | ||
{ | ||
"With inner holes variant - 2D single interior ring with SRIDs", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.SRID(4326), | ||
[]string{ | ||
"LINESTRING(50 70, 70 70, 70 50, 50 50, 50 70)", | ||
}, | ||
[]geopb.SRID{geopb.SRID(4326)}, | ||
"POLYGON((40 80,80 80,80 40,40 40,40 80),(50 70,70 70,70 50,50 50,50 70))", | ||
geopb.SRID(4326), | ||
nil, | ||
}, | ||
{ | ||
"With inner holes variant - 2D two interior rings", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.DefaultGeometrySRID, | ||
[]string{ | ||
"LINESTRING(50 70, 70 70, 70 50, 50 50, 50 70)", | ||
"LINESTRING(60 60, 75 60, 75 45, 60 45, 60 60)", | ||
}, | ||
[]geopb.SRID{geopb.DefaultGeometrySRID, geopb.DefaultGeometrySRID}, | ||
"POLYGON((40 80,80 80,80 40,40 40,40 80),(50 70,70 70,70 50,50 50,50 70),(60 60,75 60,75 45,60 45,60 60))", | ||
geopb.DefaultGeometrySRID, | ||
nil, | ||
}, | ||
{ | ||
"With inner holes variant - 2D two interior rings with SRID", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.SRID(4326), | ||
[]string{ | ||
"LINESTRING(50 70, 70 70, 70 50, 50 50, 50 70)", | ||
"LINESTRING(60 60, 75 60, 75 45, 60 45, 60 60)", | ||
}, | ||
[]geopb.SRID{geopb.SRID(4326), geopb.SRID(4326)}, | ||
"POLYGON((40 80,80 80,80 40,40 40,40 80),(50 70,70 70,70 50,50 50,50 70),(60 60,75 60,75 45,60 45,60 60))", | ||
geopb.SRID(4326), | ||
nil, | ||
}, | ||
{ | ||
"ERROR: Invalid argument - POINT", | ||
"POINT(3 2)", | ||
geopb.DefaultGeometrySRID, | ||
[]string{}, | ||
[]geopb.SRID{}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("argument must be LINESTRING geometries"), | ||
}, | ||
{ | ||
"ERROR: Invalid argument - POINT rings", | ||
"LINESTRING(75 29,77 29,77 29, 75 29)", | ||
geopb.DefaultGeometrySRID, | ||
[]string{"POINT(3 2)"}, | ||
[]geopb.SRID{0}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("argument must be LINESTRING geometries"), | ||
}, | ||
{ | ||
"ERROR: Unmatched SRIDs - Single interior ring", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.SRID(4326), | ||
[]string{ | ||
"LINESTRING(50 70, 70 70, 70 50, 50 50, 50 70)", | ||
}, | ||
[]geopb.SRID{geopb.SRID(26918)}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("mixed SRIDs are not allowed"), | ||
}, | ||
{ | ||
"ERROR: Unmatched SRIDs - Default SRID on interior ring", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.SRID(4326), | ||
[]string{ | ||
"LINESTRING(50 70, 70 70, 70 50, 50 50, 50 70)", | ||
}, | ||
[]geopb.SRID{geopb.DefaultGeometrySRID}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("mixed SRIDs are not allowed"), | ||
}, | ||
{ | ||
"ERROR: Unmatched SRIDs - Two interior rings", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.SRID(4326), | ||
[]string{ | ||
"LINESTRING(50 70, 70 70, 70 50, 50 50, 50 70)", | ||
"LINESTRING(60 60, 75 60, 75 45, 60 45, 60 60)", | ||
}, | ||
[]geopb.SRID{geopb.SRID(4326), geopb.SRID(26918)}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("mixed SRIDs are not allowed"), | ||
}, | ||
{ | ||
"ERROR: Unclosed shell", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 70)", | ||
geopb.DefaultGeographySRID, | ||
[]string{}, | ||
[]geopb.SRID{}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("shell must be closed"), | ||
}, | ||
{ | ||
"ERROR: Unclosed interior ring", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.SRID(4326), | ||
[]string{ | ||
"LINESTRING(50 70, 70 70, 70 50, 50 50, 50 60)", | ||
}, | ||
[]geopb.SRID{geopb.SRID(4326), geopb.SRID(26918)}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("holes must be closed"), | ||
}, | ||
{ | ||
"ERROR: Shell has 3 points", | ||
"LINESTRING(40 80, 80 80, 40 80)", | ||
geopb.DefaultGeographySRID, | ||
[]string{}, | ||
[]geopb.SRID{}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("shell must have at least 4 points"), | ||
}, | ||
{ | ||
"ERROR: Shell has 2 points", | ||
"LINESTRING(40 80, 40 80)", | ||
geopb.DefaultGeographySRID, | ||
[]string{}, | ||
[]geopb.SRID{}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("shell must have at least 4 points"), | ||
}, | ||
{ | ||
"ERROR: Interior ring has 3 points", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.SRID(4326), | ||
[]string{ | ||
"LINESTRING(50 70, 70 70, 50 70)", | ||
}, | ||
[]geopb.SRID{geopb.SRID(4326), geopb.SRID(26918)}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("holes must have at least 4 points"), | ||
}, | ||
{ | ||
"ERROR: Interior ring has 2 points", | ||
"LINESTRING(40 80, 80 80, 80 40, 40 40, 40 80)", | ||
geopb.SRID(4326), | ||
[]string{ | ||
"LINESTRING(50 70, 50 70)", | ||
}, | ||
[]geopb.SRID{geopb.SRID(4326), geopb.SRID(26918)}, | ||
"", | ||
geopb.DefaultGeometrySRID, | ||
errors.Newf("holes must have at least 4 points"), | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
outer, err := geo.MustParseGeometry(tc.outer).CloneWithSRID(tc.outerSRID) | ||
require.NoError(t, err) | ||
interior := make([]*geo.Geometry, 0, len(tc.interior)) | ||
for i, g := range tc.interior { | ||
interiorRing, err := geo.MustParseGeometry(g).CloneWithSRID(tc.interiorSRIDs[i]) | ||
require.NoError(t, err) | ||
interior = append(interior, interiorRing) | ||
} | ||
polygon, err := MakePolygon(outer, interior...) | ||
if tc.err != nil { | ||
require.Errorf(t, err, tc.err.Error()) | ||
} else { | ||
require.NoError(t, err) | ||
expected, err := geo.MustParseGeometry(tc.expected).CloneWithSRID(tc.expectedSRID) | ||
require.NoError(t, err) | ||
assert.Equal(t, expected, polygon) | ||
} | ||
}) | ||
} | ||
} |
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.
one more minor detail: can you also add a test to make sure linestrings are closed with at least four points, e.g.
(same for interior loops)
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.
Added a check for number of points and closure. Thanks! Not sure if this is only Postgis behavior or
geom
should implement this check as well. At the moment,geom
doesn't check for this.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.
Probably, but we'll make do with this for now :)
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.
Filed #51074.