Skip to content

Commit

Permalink
geo/geomfn: implement ST_MinimumBoundingCircle
Browse files Browse the repository at this point in the history
This commit implements ST_MinimumBoundingCircle builtin function to
return polygon shape which approximates minimum bounding circle to
contain provided geometry.

Resolves: cockroachdb#48987

Signed-off-by: Artem Barger <[email protected]>

Release note (sql change): Returns polygon shape which approximates
minimum bounding circle to contain geometry
  • Loading branch information
C0rWin committed Oct 15, 2020
1 parent a51a8eb commit c34f2e0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,8 @@ 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_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
12 changes: 12 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,15 @@ 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))

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))
14 changes: 13 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,19 @@ 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),
),

//
// Unimplemented.
//
Expand Down Expand Up @@ -5568,7 +5581,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

0 comments on commit c34f2e0

Please sign in to comment.