-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
geomfn: implement validity operators
Unfortunately we cannot implement ST_IsValidDetail because it returns a composite type, which we do not yet support. Release note (sql change): Implements ST_IsValid, ST_IsValidReason and ST_MakeValid operators for geometry types.
- Loading branch information
Showing
8 changed files
with
579 additions
and
2 deletions.
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,74 @@ | ||
// 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/cockroach/pkg/geo/geos" | ||
) | ||
|
||
// ValidDetail contains information about the validity of a geometry. | ||
type ValidDetail struct { | ||
IsValid bool | ||
// Reason is only populated if IsValid = false. | ||
Reason string | ||
// InvalidLocation is only populated if IsValid = false. | ||
InvalidLocation *geo.Geometry | ||
} | ||
|
||
// IsValid returns whether the given Geometry is valid. | ||
func IsValid(g *geo.Geometry) (bool, error) { | ||
isValid, err := geos.IsValid(g.EWKB()) | ||
if err != nil { | ||
return false, err | ||
} | ||
return isValid, nil | ||
} | ||
|
||
// IsValidReason returns the reasoning for whether the Geometry is valid or invalid. | ||
func IsValidReason(g *geo.Geometry) (string, error) { | ||
reason, err := geos.IsValidReason(g.EWKB()) | ||
if err != nil { | ||
return "", err | ||
} | ||
return reason, nil | ||
} | ||
|
||
// IsValidDetail returns information about the validity of a Geometry. | ||
// It takes in a flag parameter which behaves the same as the GEOS module, where 1 | ||
// means that self-intersecting rings forming holes are considered valid. | ||
func IsValidDetail(g *geo.Geometry, flags int) (ValidDetail, error) { | ||
isValid, reason, locEWKB, err := geos.IsValidDetail(g.EWKB(), flags) | ||
if err != nil { | ||
return ValidDetail{}, err | ||
} | ||
var loc *geo.Geometry | ||
if len(locEWKB) > 0 { | ||
loc, err = geo.ParseGeometryFromEWKB(locEWKB) | ||
if err != nil { | ||
return ValidDetail{}, err | ||
} | ||
} | ||
return ValidDetail{ | ||
IsValid: isValid, | ||
Reason: reason, | ||
InvalidLocation: loc, | ||
}, nil | ||
} | ||
|
||
// MakeValid returns a valid form of the given Geometry. | ||
func MakeValid(g *geo.Geometry) (*geo.Geometry, error) { | ||
validEWKB, err := geos.MakeValid(g.EWKB()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return geo.ParseGeometryFromEWKB(validEWKB) | ||
} |
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,148 @@ | ||
// 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 ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/geo" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIsValid(t *testing.T) { | ||
testCases := []struct { | ||
wkt string | ||
expected bool | ||
}{ | ||
{"POINT(1.0 1.0)", true}, | ||
{"LINESTRING(1.0 1.0, 2.0 2.0, 3.0 3.0)", true}, | ||
{"POLYGON((0.0 0.0, 1.0 0.0, 1.0 1.0, 0.0 0.0))", true}, | ||
|
||
{"POLYGON((1.0 1.0, 2.0 2.0, 1.5 1.5, 1.5 -1.5, 1.0 1.0))", false}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.wkt, func(t *testing.T) { | ||
g, err := geo.ParseGeometry(tc.wkt) | ||
require.NoError(t, err) | ||
ret, err := IsValid(g) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, ret) | ||
}) | ||
} | ||
} | ||
|
||
func TestIsValidReason(t *testing.T) { | ||
testCases := []struct { | ||
wkt string | ||
expected string | ||
}{ | ||
{"POINT(1.0 1.0)", "Valid Geometry"}, | ||
{"LINESTRING(1.0 1.0, 2.0 2.0, 3.0 3.0)", "Valid Geometry"}, | ||
{"POLYGON((0.0 0.0, 1.0 0.0, 1.0 1.0, 0.0 0.0))", "Valid Geometry"}, | ||
|
||
{"POLYGON((1.0 1.0, 2.0 2.0, 1.5 1.5, 1.5 -1.5, 1.0 1.0))", "Self-intersection[1.5 1.5]"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.wkt, func(t *testing.T) { | ||
g, err := geo.ParseGeometry(tc.wkt) | ||
require.NoError(t, err) | ||
ret, err := IsValidReason(g) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, ret) | ||
}) | ||
} | ||
} | ||
|
||
func TestIsValidDetail(t *testing.T) { | ||
testCases := []struct { | ||
wkt string | ||
flags int | ||
expected ValidDetail | ||
}{ | ||
{"POINT(1.0 1.0)", 0, ValidDetail{IsValid: true}}, | ||
{"LINESTRING(1.0 1.0, 2.0 2.0, 3.0 3.0)", 0, ValidDetail{IsValid: true}}, | ||
{"POLYGON((0.0 0.0, 1.0 0.0, 1.0 1.0, 0.0 0.0))", 0, ValidDetail{IsValid: true}}, | ||
{"POINT(1.0 1.0)", 1, ValidDetail{IsValid: true}}, | ||
{"LINESTRING(1.0 1.0, 2.0 2.0, 3.0 3.0)", 1, ValidDetail{IsValid: true}}, | ||
{"POLYGON((0.0 0.0, 1.0 0.0, 1.0 1.0, 0.0 0.0))", 1, ValidDetail{IsValid: true}}, | ||
{ | ||
"POLYGON ((14 20, 8 45, 20 35, 14 20, 16 30, 12 30, 14 20))", | ||
1, | ||
ValidDetail{IsValid: true}, | ||
}, | ||
|
||
{ | ||
"POLYGON ((14 20, 8 45, 20 35, 14 20, 16 30, 12 30, 14 20))", | ||
0, | ||
ValidDetail{ | ||
IsValid: false, | ||
Reason: "Ring Self-intersection", | ||
InvalidLocation: geo.MustParseGeometry("POINT(14 20)"), | ||
}, | ||
}, | ||
{ | ||
"POLYGON((1.0 1.0, 2.0 2.0, 1.5 1.5, 1.5 -1.5, 1.0 1.0))", | ||
0, | ||
ValidDetail{ | ||
IsValid: false, | ||
Reason: "Self-intersection", | ||
InvalidLocation: geo.MustParseGeometry("POINT(1.5 1.5)"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("%s(%d)", tc.wkt, tc.flags), func(t *testing.T) { | ||
g, err := geo.ParseGeometry(tc.wkt) | ||
require.NoError(t, err) | ||
ret, err := IsValidDetail(g, tc.flags) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, ret) | ||
}) | ||
} | ||
} | ||
|
||
func TestMakeValid(t *testing.T) { | ||
testCases := []struct { | ||
wkt string | ||
expected string | ||
}{ | ||
{"POINT(1.0 1.0)", "POINT(1.0 1.0)"}, | ||
{"LINESTRING(1.0 1.0, 2.0 2.0, 3.0 3.0)", "LINESTRING(1.0 1.0, 2.0 2.0, 3.0 3.0)"}, | ||
{"POLYGON((0.0 0.0, 1.0 0.0, 1.0 1.0, 0.0 0.0))", "POLYGON((0.0 0.0, 1.0 0.0, 1.0 1.0, 0.0 0.0))"}, | ||
|
||
{ | ||
"POLYGON((1.0 1.0, 2.0 2.0, 1.5 1.5, 1.5 -1.5, 1.0 1.0))", | ||
"GEOMETRYCOLLECTION(POLYGON((1 1,1.5 1.5,1.5 -1.5,1 1)),LINESTRING(1.5 1.5,2 2))", | ||
}, | ||
{ | ||
"SRID=4326;POLYGON((1.0 1.0, 2.0 2.0, 1.5 1.5, 1.5 -1.5, 1.0 1.0))", | ||
"SRID=4326;GEOMETRYCOLLECTION(POLYGON((1 1,1.5 1.5,1.5 -1.5,1 1)),LINESTRING(1.5 1.5,2 2))", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.wkt, func(t *testing.T) { | ||
g, err := geo.ParseGeometry(tc.wkt) | ||
require.NoError(t, err) | ||
|
||
ret, err := MakeValid(g) | ||
require.NoError(t, err) | ||
|
||
expected, err := geo.ParseGeometry(tc.expected) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, ret) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.