Question

I am trying to use SDO_GEOM.SDO_CLOSEST_POINTS purely as a sql query. All the examples use it in conjunction with pl/sql. Is it possible to use it in a query without pl/sql and could anyone provide an example of the syntax for how to do that?

My specific task is trying to return the vertex on a line that is closest to a point.

http://docs.oracle.com/cd/B28359_01/appdev.111/b28400/sdo_objgeom.htm#SPATL1113

Thank you.

Was it helpful?

Solution

It's not possible to call a PL/SQL procedure from an SQL query.

I would suggest that you create an Oracle object type that wraps the OUT parameters from SDO_GEOM.SDO_CLOSEST_POINTS and then define your own PL/SQL function that calls the procedure that returns an instance of your object type.

Something like this:

CREATE TYPE closest_points_type AS OBJECT (
  dist NUMBER
, geoma mdsys.sdo_geometry
, geomb mdsys.sdo_geometry
)
/

CREATE FUNCTION sdo_closest_points_sql (
  p_geom1     IN sdo_geometry
, p_geom2     IN sdo_geometry
, p_tolerance IN NUMBER
, p_unit      IN VARCHAR2
)
RETURN closest_points_type
IS

  l_dist  NUMBER;
  l_geoma mdsys.sdo_geometry;
  l_geomb mdsys.sdo_geometry;

BEGIN

  sdo_geom.sdo_closest_points(
    geom1     => p_geom1
  , geom2     => p_geom2
  , tolerance => p_tolerance
  , unit      => p_unit
  , dist      => l_dist
  , geoma     => l_geoma
  , geomb     => l_geomb
  );

  RETURN closest_points_type(l_dist, l_geoma, l_geomb);

END sdo_closest_points_sql;
/

You should then be able to call this function from a SELECT statement, and interrogate the resulting object like so:

WITH q1 AS (
  SELECT
    sdo_closest_points_sql(
      mdsys.sdo_geometry(2002, NULL, NULL, sdo_elem_info_array(1,2,1), sdo_ordinate_array(1,1, 1,10, 1,20))
    , mdsys.sdo_geometry(2002, NULL, NULL, sdo_elem_info_array(1,2,1), sdo_ordinate_array(2,5, 2,15, 2,25))
    , 0.05
    , NULL
    ) result
  FROM dual
)
SELECT 
  (q1.result).dist dist
, (q1.result).geoma geoma
, (q1.result).geomb geomb
FROM q1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top