Skip to content
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

builtins: implement ST_Points #53496

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,8 @@ calculated, the result is transformed back into a Geography with SRID 4326.</p>
<tr><td><a name="st_pointonsurface"></a><code>st_pointonsurface(geometry: geometry) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns a point that intersects with the given Geometry.</p>
<p>This function utilizes the GEOS module.</p>
</span></td></tr>
<tr><td><a name="st_points"></a><code>st_points(geometry: geometry) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns all coordinates in the given Geometry as a MultiPoint, including duplicates.</p>
</span></td></tr>
<tr><td><a name="st_polyfromtext"></a><code>st_polyfromtext(str: <a href="string.html">string</a>, srid: <a href="int.html">int</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns the Geometry from a WKT or EWKT representation with an SRID. If the shape underneath is not Polygon, NULL is returned. If the SRID is present in both the EWKT and the argument, the argument value is used.</p>
</span></td></tr>
<tr><td><a name="st_polyfromtext"></a><code>st_polyfromtext(val: <a href="string.html">string</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns the Geometry from a WKT or EWKT representation. If the shape underneath is not Polygon, NULL is returned.</p>
Expand Down
58 changes: 58 additions & 0 deletions pkg/geo/geomfn/unary_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,61 @@ func dimensionFromGeomT(geomRepr geom.T) (int, error) {
return 0, errors.AssertionFailedf("unknown geometry type: %T", geomRepr)
}
}

// Points returns the points of all coordinates in a geometry as a multipoint.
func Points(g geo.Geometry) (geo.Geometry, error) {
t, err := g.AsGeomT()
if err != nil {
return geo.Geometry{}, err
}
layout := t.Layout()
if gc, ok := t.(*geom.GeometryCollection); ok && gc.Empty() {
layout = geom.XY
}
points := geom.NewMultiPoint(layout).SetSRID(t.SRID())
iter := geo.NewGeomTIterator(t, geo.EmptyBehaviorOmit)
for {
geomRepr, hasNext, err := iter.Next()
if err != nil {
return geo.Geometry{}, err
} else if !hasNext {
break
} else if geomRepr.Empty() {
continue
}
switch geomRepr := geomRepr.(type) {
case *geom.Point:
if err = pushCoord(points, geomRepr.Coords()); err != nil {
return geo.Geometry{}, err
}
case *geom.LineString:
for i := 0; i < geomRepr.NumCoords(); i++ {
if err = pushCoord(points, geomRepr.Coord(i)); err != nil {
return geo.Geometry{}, err
}
}
case *geom.Polygon:
for i := 0; i < geomRepr.NumLinearRings(); i++ {
linearRing := geomRepr.LinearRing(i)
for j := 0; j < linearRing.NumCoords(); j++ {
if err = pushCoord(points, linearRing.Coord(j)); err != nil {
return geo.Geometry{}, err
}
}
}
default:
return geo.Geometry{}, errors.AssertionFailedf("unexpected type: %T", geomRepr)
}
}
return geo.MakeGeometryFromGeomT(points)
}

// pushCoord is a helper function for PointsFromGeomT that appends
// a coordinate to a multipoint as a point.
func pushCoord(points *geom.MultiPoint, coord geom.Coord) error {
point, err := geom.NewPoint(points.Layout()).SetCoords(coord)
if err != nil {
return err
}
return points.Push(point)
}
58 changes: 58 additions & 0 deletions pkg/geo/geomfn/unary_operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -123,3 +124,60 @@ func TestDimension(t *testing.T) {
})
}
}

func TestPoints(t *testing.T) {
testCases := []struct {
wkt string
expected string
}{
{"POINT EMPTY", "MULTIPOINT EMPTY"},
{"POINT (1 2)", "MULTIPOINT (1 2)"},
{"MULTIPOINT EMPTY", "MULTIPOINT EMPTY"},
{"MULTIPOINT (1 2, 3 4)", "MULTIPOINT (1 2, 3 4)"},
{"LINESTRING EMPTY", "MULTIPOINT EMPTY"},
{"LINESTRING (1 2, 3 4)", "MULTIPOINT (1 2, 3 4)"},
{"MULTILINESTRING EMPTY", "MULTIPOINT EMPTY"},
{"MULTILINESTRING ((1 2, 3 4), (5 6, 7 8))", "MULTIPOINT (1 2, 3 4, 5 6, 7 8)"},
{"POLYGON EMPTY", "MULTIPOINT EMPTY"},
{"POLYGON ((1 2, 3 4, 5 6, 1 2))", "MULTIPOINT (1 2, 3 4, 5 6, 1 2)"},
{"MULTIPOLYGON EMPTY", "MULTIPOINT EMPTY"},
{"MULTIPOLYGON (((1 2, 3 4, 5 6, 1 2)), ((7 8, 9 0, 1 2, 7 8)))", "MULTIPOINT (1 2, 3 4, 5 6, 1 2, 7 8, 9 0, 1 2, 7 8)"},
{"GEOMETRYCOLLECTION EMPTY", "MULTIPOINT EMPTY"},
{"GEOMETRYCOLLECTION (GEOMETRYCOLLECTION EMPTY)", "MULTIPOINT EMPTY"},
{
`GEOMETRYCOLLECTION (
LINESTRING(1 1, 2 2),
POINT(1 1),
GEOMETRYCOLLECTION(
MULTIPOINT(2 2, 3 3),
GEOMETRYCOLLECTION EMPTY,
POINT(1 1),
erikgrinaker marked this conversation as resolved.
Show resolved Hide resolved
MULTIPOLYGON(((1 2, 2 3, 3 4, 1 2))),
LINESTRING(3 3, 4 4),
GEOMETRYCOLLECTION(
POINT(4 4),
MULTIPOLYGON (EMPTY, ((1 2, 3 4, 5 6, 1 2), (2 3, 3 4, 4 5, 2 3)), EMPTY),
POINT(5 5)
)
),
MULTIPOINT EMPTY
)`,
"MULTIPOINT (1 1, 2 2, 1 1, 2 2, 3 3, 1 1, 1 2, 2 3, 3 4, 1 2, 3 3, 4 4, 4 4, 1 2, 3 4, 5 6, 1 2, 2 3, 3 4, 4 5, 2 3, 5 5)",
},
}

for _, tc := range testCases {
t.Run(tc.wkt, func(t *testing.T) {
srid := geopb.SRID(4000)
g, err := geo.ParseGeometryFromEWKT(geopb.EWKT(tc.wkt), srid, true)
require.NoError(t, err)

result, err := Points(g)
require.NoError(t, err)
wkt, err := geo.SpatialObjectToWKT(result.SpatialObject(), 0)
require.NoError(t, err)
require.EqualValues(t, tc.expected, wkt)
require.EqualValues(t, srid, result.SRID())
})
}
}
20 changes: 20 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/geospatial
Original file line number Diff line number Diff line change
Expand Up @@ -2632,6 +2632,26 @@ Square (left) Polygon ST_Polygon
Square (right) Polygon ST_Polygon 2 2 2 5 1
Square overlapping left and right square Polygon ST_Polygon 2 2 2 5 1

query TT
SELECT
dsc,
ST_AsEWKT(ST_Points(geom))
FROM geom_operators_test
ORDER BY dsc
----
Empty GeometryCollection MULTIPOINT EMPTY
Empty LineString MULTIPOINT EMPTY
Empty Point MULTIPOINT EMPTY
Faraway point MULTIPOINT (5 5)
Line going through left and right square MULTIPOINT (-0.5 0.5, 0.5 0.5)
NULL NULL
Nested Geometry Collection MULTIPOINT (0 0)
Point middle of Left Square MULTIPOINT (-0.5 0.5)
Point middle of Right Square MULTIPOINT (0.5 0.5)
Square (left) MULTIPOINT (-1 0, 0 0, 0 1, -1 1, -1 0)
Square (right) MULTIPOINT (0 0, 1 0, 1 1, 0 1, 0 0)
Square overlapping left and right square MULTIPOINT (-0.1 0, 1 0, 1 1, -0.1 1, -0.1 0)

query TTTT
SELECT
a.dsc,
Expand Down
17 changes: 17 additions & 0 deletions pkg/sql/sem/builtins/geo_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,23 @@ Flags shown square brackets after the geometry type have the following meaning:
tree.VolatilityImmutable,
),
),
"st_points": makeBuiltin(
defProps(),
geometryOverload1(
func(ctx *tree.EvalContext, g *tree.DGeometry) (tree.Datum, error) {
points, err := geomfn.Points(g.Geometry)
if err != nil {
return nil, err
}
return tree.NewDGeometry(points), nil
},
types.Geometry,
infoBuilder{
info: "Returns all coordinates in the given Geometry as a MultiPoint, including duplicates.",
},
tree.VolatilityImmutable,
),
),
"st_exteriorring": makeBuiltin(
defProps(),
geometryOverload1(
Expand Down