-
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.
geo/geomfn: implement ST_Relate and ST_ContainsProperly
Added ST_ContainsProperly to the optimizer as well that calls it to use Covers. Also update the RFC to claim ST_ContainsProperly as indexed backed. Release note (sql change): Implemented the geometry based builtins `ST_Relate` and `ST_ContainsProperly`.
- Loading branch information
Showing
13 changed files
with
425 additions
and
60 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
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
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,55 @@ | ||
// 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 ( | ||
"unicode" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/geo" | ||
"github.com/cockroachdb/cockroach/pkg/geo/geos" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// Relate returns the DE-9IM relation between A and B. | ||
func Relate(a *geo.Geometry, b *geo.Geometry) (string, error) { | ||
if a.SRID() != b.SRID() { | ||
return "", geo.NewMismatchingSRIDsError(a, b) | ||
} | ||
return geos.Relate(a.EWKB(), b.EWKB()) | ||
} | ||
|
||
// MatchesDE9IM checks whether the given DE-9IM relation matches the DE-91M pattern. | ||
// See: https://en.wikipedia.org/wiki/DE-9IM. | ||
func MatchesDE9IM(str string, pattern string) (bool, error) { | ||
if len(str) != 9 { | ||
return false, errors.Newf("str %q should be of length 9", str) | ||
} | ||
if len(pattern) != 9 { | ||
return false, errors.Newf("pattern %q should be of length 9", pattern) | ||
} | ||
for i := 0; i < len(str); i++ { | ||
switch unicode.ToLower(rune(pattern[i])) { | ||
case '*': | ||
continue | ||
case 't': | ||
if str[i] < '0' || str[i] > '2' { | ||
return false, nil | ||
} | ||
case 'f': | ||
if unicode.ToLower(rune(str[i])) != 'f' { | ||
return false, nil | ||
} | ||
default: | ||
return false, errors.Newf("unrecognized pattern character at position %d: %s", i, pattern) | ||
} | ||
} | ||
return true, nil | ||
} |
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 ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/geo" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRelate(t *testing.T) { | ||
testCases := []struct { | ||
a *geo.Geometry | ||
b *geo.Geometry | ||
expected string | ||
}{ | ||
{leftRect, rightRect, "FF2F11212"}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
t.Run(fmt.Sprintf("tc:%d", i), func(t *testing.T) { | ||
ret, err := Relate(tc.a, tc.b) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, ret) | ||
}) | ||
} | ||
} | ||
|
||
func TestMatchesDE9IM(t *testing.T) { | ||
testCases := []struct { | ||
str string | ||
pattern string | ||
expected bool | ||
expectedError string | ||
}{ | ||
{"", "T**FF*FF*", false, `str "" should be of length 9`}, | ||
{"TTTTTTTTT", "T**FF*FF*T", false, `pattern "T**FF*FF*T" should be of length 9`}, | ||
{"TTTTTTTTT", "T**FF*FF*T", false, `pattern "T**FF*FF*T" should be of length 9`}, | ||
{"000FFF000", "cTTFfFTTT", false, `unrecognized pattern character at position 0: cTTFfFTTT`}, | ||
{"120FFF021", "TTTFfFTTT", true, ""}, | ||
{"02FFFF000", "T**FfFTTT", true, ""}, | ||
{"020F1F010", "TTTFFFTtT", false, ""}, | ||
{"020FFF0f0", "TTTFFFTtT", false, ""}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("%s has pattern %s", tc.str, tc.pattern), func(t *testing.T) { | ||
ret, err := MatchesDE9IM(tc.str, tc.pattern) | ||
if tc.expectedError == "" { | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, ret) | ||
} else { | ||
require.EqualError(t, err, tc.expectedError) | ||
} | ||
}) | ||
} | ||
|
||
t.Run("errors if SRIDs mismatch", func(t *testing.T) { | ||
_, err := Relate(mismatchingSRIDGeometryA, mismatchingSRIDGeometryB) | ||
requireMismatchingSRIDError(t, err) | ||
}) | ||
} |
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
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.