简体   繁体   中英

Query to find all lines connected to other

I have problem with one spatial (Oracle) query. Simplified overview of the problem is:

I have two tables:

CREATE TABLE thin_lines (
  id NUMBER PRIMARY KEY,
  geom SDO_GEOMETRY),
  color VARCHAR2(32);

CREATE TABLE thick_lines (
  id NUMBER PRIMARY KEY,
  geom SDO_GEOMETRY),
  color VARCHAR2(32);

Both tables contain 2D lines. The example picture:

在此处输入图片说明

The problem:

I want to create SQL query or PL/SQL procedure to find ids of all blue thin lines (whole blue branch comprising of blue lines) connected to the thick green lines.

Thank you for your assistance.

You should look into the Oracle Spatial Data Type and SDO_INTERSECTION .

I have not tested this and it likely does not work; but it should be a helpful starting point:

DECLARE
    type intersecting_blue_line is table of NUMBER;
    RT_GL thick_lines%ROWTYPE;
    CURSOR get_GreenLines IS
        SELECT *
        FROM thick_lines TKL
        WHERE TKL.color = 'Green';

BEGIN
    OPEN get_GreenLines;
    LOOP
        FETCH get_GreenLines INTO RT_GL;

        for x in (SELECT TNL.id, 
                  FROM thin_lines TNL
                  WHERE TNL.color = 'Blue'
                  AND SDO_GEOM.SDO_INTERSECTION(RT_GL.geom, TNL.geom, 0.005) IS NOT NULL;)
        LOOP
            intersecting_blue_line := x.id;
        END LOOP;

    END LOOP;
    CLOSE get_tables;
END;

SELECT * FROM intersecting_blue_line;

I have found some solution to do this with one query (Oracle 11.2):

WITH recursive (p1, p2) AS (
  SELECT thin.connection_id, thin.geom FROM thin_lines thin, thick_lines thick
    WHERE sdo_touch(thin.geom, thick.geom) = 'TRUE' AND thick.color = 'green' AND thin.color = 'blue'
  UNION ALL
  SELECT thin.connection_id, thin.geom FROM thin_lines thin, recursive r
    WHERE sdo_touch(thin.geom, r.p2) = 'TRUE' and thin.color='blue'
)
CYCLE p1 SET is_cycle TO 1 DEFAULT 0
SELECT distinct p1 FROM recursive order by p1;

It works, but for small set of thin and thick lines. For huge set it is unusable for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM