4

I have a PostGIS table of thousands of linestring segments wherein each line segment has only two nodes, start and end. All of the lines have a start and/or end node that matches another line segment in the table (they are all connected end-to-end). I'm trying to create a table of points representing line intersections where a line segments intersects another line segment in the table, but NOT intersections that are only intersections because they share a start or end node with another line in the table.

The query shown below selects all intersecting start and end nodes, but NOT the crossing segments (which is what I want)

CREATE TABLE intersection_points as
SELECT      
    ST_Intersection(a.geom, b.geom),
    Count(Distinct a.id)
FROM
    public."82n16nw_gsl_paths" as a,
    public."82n16nw_gsl_paths" as b
WHERE
    ST_Touches(a.geom, b.geom)
    AND a.id != b.id
GROUP BY
    ST_Intersection(a.geom, b.geom)
;

graphic of lines and intersection points: query result showing intersection points

the QGIS 'Line Intersections' tool selects the start/end node intersections AND the segment intersections that I'm after, but there is no way to only identify the segment-only points from this tool.

jamierob
  • 1,559
  • 12
  • 25
  • SELECT a.id, ST_Intersection(a.geom, b.geom) geom FROM gslpaths a JOIN gslpaths b ON ST_Crosses(a.geom, b.geom) WHERE a.id < b.id – Cyril Mikhalchenko May 15 '22 at 16:30
  • Worth noting that ST_Crosses won't find line pairs which overlap in a line. – dr_jts May 15 '22 at 17:02
  • @dr_jts - The OP picture shows only one intersection of line segments :-)... – Cyril Mikhalchenko May 15 '22 at 17:08
  • Yes. But it turned out the actual data had overlapping lines. – dr_jts May 15 '22 at 17:24
  • @dr_jts, I did not download the data and did not analyze it; 2) I do not notice any feedback from OP; 3) by the way we should correct the name of the question "OP ask to select intersecting segments", while in the content of the question OP asks to select points, and then in the comments it turns out that points are PointZ, etc. :-)...
  • – Cyril Mikhalchenko May 15 '22 at 19:35