Exposing JTS's MinimumBoundingCircle functionality
Sunday September 11 2011 at 17:55
Keywords JTS ST_MinimumBoundingCircle
Summary This article exposes Jts’s MinimumBoundingCircle for Oracle sdo_geometry users.
Today I decided to expose the Java Topology Suite’s (JTS ) MinimumBoundingCircle functionality via the JTS installer and package.
The MinimumBoundingCircle PL/SQL wrappers I have created are:
/**
* ST_MinimumBoundingCircle
* Computes the Minimum Bounding Circle (MBC) for the points in a Geometry.
* The MBC is the smallest circle which contains all the input points (this
* is sometimes known as the Smallest Enclosing Circle). This is equivalent
* to computing the Maximum Diameter of the input point set.
* @param p_geom : sdo_geometry : first geometry subject to MBC calculation
* @param p_precision : int : number of decimal places of precision
* @return circle : sdo_geometry : Result of MBC calculation
* @author Martin Davis, JTS 1.12
* @history Simon Greener, September 2011, Original Coding of wrapper
* @copyright : Licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License.
* http://creativecommons.org/licenses/by-sa/2.5/au/
**/
FUNCTION ST_MinimumBoundingCircle( p_geom IN mdsys. sdo_geometry,
p_precision IN NUMBER )
RETURN mdsys. sdo_geometry deterministic;
.
/**
* ST_MinimumBoundingCircle
* Computes the Minimum Bounding Circle (MBC) for the points in a Geometry.
* The MBC is the smallest circle which contains all the input points (this
* is sometimes known as the Smallest Enclosing Circle). This is equivalent
* to computing the Maximum Diameter of the input point set.
* @param p_geom : sdo_geometry : first geometry subject to MBC calculation
* @param p_precision : int : number of decimal places of precision
* @param p_srid : int : SRID of projected space in which actual overlay occurs before
* being projected back to p_geom1.sdo_srid.
* @return circle : sdo_geometry : Result of MBC calculation
* @author Martin Davis, JTS 1.12
* @history Simon Greener, September 2011, Original Coding of wrapper
* @copyright : Licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License.
* http://creativecommons.org/licenses/by-sa/2.5/au/
**/
FUNCTION ST_MinimumBoundingCircle( p_geom IN mdsys. sdo_geometry,
p_precision IN NUMBER ,
p_srid IN NUMBER )
RETURN mdsys. sdo_geometry deterministic;
Here are some examples.
-- Point is returned as is
SELECT JTS. ST_MinimumBoundingCircle( sdo_geometry( 2001 , NULL , sdo_point_type( 1 , 1 , NULL ) , NULL , NULL ) , 2 ) AS mbc FROM dual;
.
MBC
----------------------------------------------------------------------
MDSYS. SDO_GEOMETRY( 2001 , NULL , MDSYS. SDO_POINT_TYPE( 1 , 1 , NULL ) , NULL , NULL )
.
-- Straight line defined by two points
SELECT JTS. ST_MinimumBoundingCircle( sdo_geometry( 'MULTIPOINT ((10 10), (20 20))' , 0 ) , 2 ) AS mbc FROM dual;
.
MBC
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MDSYS. SDO_GEOMETRY( 2003 , NULL , MDSYS. SDO_POINT_TYPE( 15 , 15 , 7.07106781186548 ) , MDSYS. SDO_ELEM_INFO_ARRAY( 1 , 1003 , 4 ) , MDSYS. SDO_ORDINATE_ARRAY( 7.92893218813452 , 15 , 22.07106781186548 , 15 , 15 , 22.07106781186548 ) )
.
-- Three Points In Line
SELECT JTS. ST_MinimumBoundingCircle( sdo_geometry( 'MULTIPOINT ((10 10), (20 20), (30 30))' , 0 ) , 2 ) AS mbc FROM dual;
.
MBC
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MDSYS. SDO_GEOMETRY( 2003 , NULL , MDSYS. SDO_POINT_TYPE( 20 , 20 , 14.142135623731 ) , MDSYS. SDO_ELEM_INFO_ARRAY( 1 , 1003 , 4 ) , MDSYS. SDO_ORDINATE_ARRAY( 5.857864376269 , 20 , 34.142135623731 , 20 , 20 , 34.142135623731 ) )
.
-- three points
SELECT JTS. ST_MinimumBoundingCircle( sdo_geometry( 'MULTIPOINT ((10 10), (20 20), (10 20))' , 0 ) , 2 ) AS mbc FROM dual;
.
MBC
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MDSYS. SDO_GEOMETRY( 2003 , NULL , MDSYS. SDO_POINT_TYPE( 15 , 15 , 7.07106781186548 ) , MDSYS. SDO_ELEM_INFO_ARRAY( 1 , 1003 , 4 ) , MDSYS. SDO_ORDINATE_ARRAY( 7.92893218813452 , 15 , 22.07106781186548 , 15 , 15 , 22.07106781186548 ) )
.
-- Triangle With Middle Point
SELECT JTS. ST_MinimumBoundingCircle( sdo_geometry( 'MULTIPOINT ((10 10), (20 20), (10 20), (15 19))' , 0 ) , 2 ) AS mbc FROM dual;
.
MBC
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MDSYS. SDO_GEOMETRY( 2003 , NULL , MDSYS. SDO_POINT_TYPE( 15 , 15 , 7.07106781186548 ) , MDSYS. SDO_ELEM_INFO_ARRAY( 1 , 1003 , 4 ) , MDSYS. SDO_ORDINATE_ARRAY( 7.92893218813452 , 15 , 22.07106781186548 , 15 , 15 , 22.07106781186548 ) )
</ pre></ code>
Now let's test and visualise some lines and circles.
<code id="SourceCode"><pre>
with line as (
select sdo_geometry(' LINESTRING( 0 0 , 10 10 , 10 0 , 20 10 ) ',0) as line from dual
)
select JTS.ST_MinimumBoundingCircle(line,2) as dGeom from line;
.
DGEOM
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MDSYS.SDO_GEOMETRY(2003,NULL,MDSYS.SDO_POINT_TYPE(10,5,11.1803398874989),MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,4),MDSYS.SDO_ORDINATE_ARRAY(-1.1803398874989,5,21.1803398874989,5,10,16.1803398874989))
.
-- Optimized Rectangle
with rect as (
select sdo_geometry(2003,null,null,sdo_elem_info_array(1,1003,3),sdo_ordinate_array(1,1,10,10)) as rect from dual
)
select JTS.ST_MinimumBoundingCircle(rect,2) as dGeom from rect;
.
DGEOM
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MDSYS.SDO_GEOMETRY(2003,NULL,MDSYS.SDO_POINT_TYPE(5.5,5.5,6.36396103067893),MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,4),MDSYS.SDO_ORDINATE_ARRAY(-0.86396103067893,5.5,11.86396103067893,5.5,5.5,11.86396103067893))
Which look like:
*1. Line*
!http://www.spatialdbadvisor.com/images/134.png!
*2. Rectangle*
!http://www.spatialdbadvisor.com/images/135.png!
I hope this helps someone.
Comment [2]
What an interesting concept!
Now to find a use for it :)
— Thomas · 13 September 2011, 10:31 · #
Thomas,
Well, it is a part of JTS, and even has its own Wikipedia page, called Smallest circle problem so it must be useful!
I suspect its main area of application is in spatial statistics.
regards
Simon
— Simon Greener · 13 September 2011, 16:12 · #