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

geo/geomfn: implement ST_MinimumBoundingCircle #55567

Merged
merged 1 commit into from
Oct 20, 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
4 changes: 4 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,10 @@ calculated, the result is transformed back into a Geography with SRID 4326.</p>
</span></td></tr>
<tr><td><a name="st_memsize"></a><code>st_memsize(geometry: geometry) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Returns the amount of memory space (in bytes) the geometry takes.</p>
</span></td></tr>
<tr><td><a name="st_minimumboundingcircle"></a><code>st_minimumboundingcircle(geometry: geometry) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns the smallest circle polygon that can fully contain a geometry.</p>
</span></td></tr>
<tr><td><a name="st_minimumboundingcircle"></a><code>st_minimumboundingcircle(geometry: geometry, num_segs: <a href="int.html">int</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns the smallest circle polygon that can fully contain a geometry.</p>
</span></td></tr>
<tr><td><a name="st_minimumboundingradius"></a><code>st_minimumboundingradius(geometry: geometry) &rarr; tuple{geometry AS center, float AS radius}</code></td><td><span class="funcdesc"><p>Returns a record containing the center point and radius of the smallest circle that can fully contains the given geometry.</p>
</span></td></tr>
<tr><td><a name="st_minimumclearance"></a><code>st_minimumclearance(geometry: geometry) &rarr; <a href="float.html">float</a></code></td><td><span class="funcdesc"><p>Returns the minimum distance a vertex can move before producing an invalid geometry. Returns Infinity if no minimum clearance can be found (e.g. for a single point).</p>
Expand Down
32 changes: 32 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/geospatial
Original file line number Diff line number Diff line change
Expand Up @@ -5541,3 +5541,35 @@ SELECT ST_AsText(center), round(radius, 2) FROM ST_MinimumBoundingRadius('POLYGO
----
POINT (0 0) 2


subtest st_minimumboundincircle

query T
SELECT ST_AsText(ST_SnapToGrid(ST_MinimumBoundingCircle(ST_GeomFromText('GEOMETRYCOLLECTION (LINESTRING(55 75,125 150), POINT(20 80))')), 0.001));
----
POLYGON ((135.597000000000008 115, 134.384999999999991 102.689999999999998, 130.794000000000011 90.853999999999999, 124.963000000000008 79.945000000000007, 117.116 70.384, 107.555000000000007 62.536999999999999, 96.646000000000001 56.706000000000003, 84.810000000000002 53.115000000000002, 72.5 51.902999999999999, 60.189999999999998 53.115000000000002, 48.353999999999999 56.706000000000003, 37.445 62.536999999999999, 27.884 70.384, 20.036999999999999 79.945000000000007, 14.206 90.853999999999999, 10.615 102.689999999999998, 9.403 115, 10.615 127.310000000000002, 14.206 139.146000000000015, 20.036999999999999 150.055000000000007, 27.884 159.616000000000014, 37.445 167.462999999999994, 48.353999999999999 173.294000000000011, 60.189999999999998 176.884999999999991, 72.5 178.097000000000008, 84.810000000000002 176.884999999999991, 96.646000000000001 173.294000000000011, 107.555000000000007 167.462999999999994, 117.116 159.616000000000014, 124.963000000000008 150.055000000000007, 130.794000000000011 139.146000000000015, 134.384999999999991 127.310000000000002, 135.597000000000008 115))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test for the minimum bounding circle of a single point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


query T
SELECT ST_AsText(ST_SnapToGrid(ST_MinimumBoundingCircle(ST_GeomFromText('GEOMETRYCOLLECTION (LINESTRING(0 0, 4 0), POINT(0 4))')), 0.001));
----
POLYGON ((4.828 2, 4.774 1.448, 4.613 0.918, 4.352 0.429, 4 0, 3.571 -0.352, 3.082 -0.613, 2.552 -0.774, 2 -0.828, 1.448 -0.774, 0.918 -0.613, 0.429 -0.352, 0 0, -0.352 0.429, -0.613 0.918, -0.774 1.448, -0.828 2, -0.774 2.552, -0.613 3.082, -0.352 3.571, 0 4, 0.429 4.352, 0.918 4.613, 1.448 4.774, 2 4.828, 2.552 4.774, 3.082 4.613, 3.571 4.352, 4 4, 4.352 3.571, 4.613 3.082, 4.774 2.552, 4.828 2))

query T
SELECT ST_AsText(ST_SnapToGrid(ST_MinimumBoundingCircle(ST_GeomFromText('POINT EMPTY')), 0.001));
----
POLYGON EMPTY

query T
SELECT ST_AsText(ST_SnapToGrid(ST_MinimumBoundingCircle(ST_GeomFromText('POINT(0 0)')), 0.001));
----
POINT (0 0)

query T
SELECT ST_AsText(ST_SnapToGrid(ST_MinimumBoundingCircle(ST_GeomFromText('GEOMETRYCOLLECTION (LINESTRING(55 75,125 150), POINT(20 80))'), 4), 0.001));
----
POLYGON ((135.597000000000008 115, 130.794000000000011 90.853999999999999, 117.116 70.384, 96.646000000000001 56.706000000000003, 72.5 51.902999999999999, 48.353999999999999 56.706000000000003, 27.884 70.384, 14.206 90.853999999999999, 9.403 115, 14.206 139.146000000000015, 27.884 159.616000000000014, 48.353999999999999 173.294000000000011, 72.5 178.097000000000008, 96.646000000000001 173.294000000000011, 117.116 159.616000000000014, 130.794000000000011 139.146000000000015, 135.597000000000008 115))

query B
SELECT ST_MinimumBoundingCircle(NULL::geometry) IS NULL;
----
true
45 changes: 44 additions & 1 deletion pkg/sql/sem/builtins/geo_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -5539,6 +5539,50 @@ See http://developers.google.com/maps/documentation/utilities/polylinealgorithm`
Volatility: tree.VolatilityImmutable,
}),

"st_minimumboundingcircle": makeBuiltin(defProps(),
geometryOverload1(
func(evalContext *tree.EvalContext, g *tree.DGeometry) (tree.Datum, error) {
polygon, _, _, err := geomfn.MinimumBoundingCircle(g.Geometry)
if err != nil {
return nil, err
}
return tree.NewDGeometry(polygon), nil
},
types.Geometry,
infoBuilder{
info: "Returns the smallest circle polygon that can fully contain a geometry.",
},
tree.VolatilityImmutable,
),
tree.Overload{
Types: tree.ArgTypes{{"geometry", types.Geometry}, {" num_segs", types.Int}},
ReturnType: tree.FixedReturnType(types.Geometry),
Info: infoBuilder{
info: "Returns the smallest circle polygon that can fully contain a geometry.",
}.String(),
Volatility: tree.VolatilityImmutable,
Fn: func(evalContext *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
g := tree.MustBeDGeometry(args[0])
numOfSeg := tree.MustBeDInt(args[1])
_, centroid, radius, err := geomfn.MinimumBoundingCircle(g.Geometry)
if err != nil {
return nil, err
}

polygon, err := geomfn.Buffer(
centroid,
geomfn.MakeDefaultBufferParams().WithQuadrantSegments(int(numOfSeg)),
radius,
)
if err != nil {
return nil, err
}

return tree.NewDGeometry(polygon), nil
},
},
),

//
// Unimplemented.
//
Expand Down Expand Up @@ -5568,7 +5612,6 @@ See http://developers.google.com/maps/documentation/utilities/polylinealgorithm`
"st_lengthspheroid": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 48968}),
"st_linecrossingdirection": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 48969}),
"st_linesubstring": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 48975}),
"st_minimumboundingcircle": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 48987}),
"st_node": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 48993}),
"st_orientedenvelope": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 49003}),
"st_polygonize": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 49011}),
Expand Down