I need to find the intersection of various roads but each road is divided into multiple segments so there are many records for one road. I have this query which i can use to find the geometries but each shows up as null. I need to have one geometry so that i can insert that into one record.
SELECT SDO_GEOM.SDO_INTERSECTION(coll_a.GEOMETRY, coll_b.GEOMETRY, 0.05)
FROM OTTAWACOLLECTORS coll_a, OTTAWACOLLECTORS coll_b
WHERE coll_a.ROAD_NAME = 'KENT' AND coll_b.ROAD_NAME = 'METCALFE';
The most efficient approach for that sort of problem is to use the SDO_JOIN() filter. It is designed to efficiently match spatially many objects with many others using the spatial indexes.
I am assuming that your table looks like this:
It contains road segments. Each road segment is identified by a road identifier and a segment identifier.
The following creates the new table INTERSECTIONS that contain one row for each intersection, i.e. whenever two road segments interact. The intersection is computed as a geometric point. Each row contains the identifier of each segment (road identifier and segment identifier) as well as the name of each road.
Some explanations:
SDO_JOIN()
is a "table" function. It takes the names of two input tables (table name and the name of the geometry column) and a match criteria - here " ANYINTERACT", meaning any kind of interaction: segments may cross or just touch each other.TABLE()
constructor casts that array so as to make it look as a regular table, making it easy to embed it in a relational query. That "virtual" table is called J in the query.OTTAWACOLLECTORS
table twice (like in your example). It joins them with theTABLE()
result:J.ROWID1=A.ROWID AND J.ROWID2=B.ROWID
J.ROWID1>J.ROWID2
filter is there to eliminate unwanted results. Say road segments A and B intersect. SDO_JOIN will return 4 combinations: (A,B), but also (B,A) and also (A,A) and (B,B) since a segment obviously intersects itself! The purpose of comparing the rowids is to only retain one of (A,B) or (B,A).SDO_GEOM.SDO_INTERSECTION()
like in your example).Note that this query will not return results instantly: it may take minutes to complete, depending on the number of road segments you need to process, and of course on the hardware you run it on. If you run it on Oracle 12c (12.1.0.1 or 12.1.0.2) and you own the proper license for Oracle Spatial, then make sure you have the Vector Performance Accelerator option turned on.