TransWikia.com

Splitting lines table using point table in PostGIS

Geographic Information Systems Asked by JonasPedersen on December 23, 2020

My problem is that I want to get the shortest route between multiple point pairs which are stored in a table (PostGIS).

In PostGIS I also have a network created using pgr_createTopology on a table containing about a 1000 MultiLineString rows. It’s my understanding that to calculate routes between points with pg_routing, you need to either accept that the route goes to and from the nearest network node or you need to work around that limitation by adding new nodes to the network which corresponds to the stops you want to calculate on.

To add new nodes to my network I’ve tried tried to follow the answer from @alpha-beta-soup in this tread
Split lines into non-overlapping subsets based on points
Before following the answer I used ArcMap to snap the points to the lines. I then imported the two shapefiles (network and radars both with geometry columns “geom”) into PostGIS using the PostGIS Shapefile loader (2.1).

When using alpha_beta_soup’s code I get a table with all the right columns, but there are zero rows in it, I’ve tried running only the first part of the code, but I still only get a table with no content. This is the first part of the code:

CREATE TABLE tmp_lineswithstops AS (
WITH subq AS (
    SELECT
    ST_Line_Locate_Point(
        roads.geom,
        ST_ClosestPoint(roads.geom, radars.geom)
    ) AS LR,
    rank() OVER (
        PARTITION BY roads.gid
        ORDER BY ST_Line_Locate_Point(
            roads.geom,
            ST_ClosestPoint(roads.geom, radars.geom)
        )
    ) AS LRRank,
    ST_ClosestPoint(roads.geom, radars.geom),
    roads.*
    FROM network AS roads
    LEFT OUTER JOIN radars
    ON ST_Distance(roads.geom, radars.geom) < 0.0001
    WHERE ST_Equals(ST_StartPoint(roads.geom), radars.geom) IS false
    AND ST_Equals(ST_EndPoint(roads.geom), radars.geom) IS false
    ORDER BY gid, LRRank
)
SELECT LR FROM subq
);  

The two tables look like this:
radars table

network table

Does anyone have a clue as to what i’m doing wrong?

One Answer

In case anyone lands here searching how to split lines with points, here is a current (2019) solution.

  • union point geometry
  • snap lines to points with tolerance of 1m - for projected coordinate system (if using geographic you'll need to adjust this value to degrees (0.00001 very roughly equals 1m)
  • split lines at points
  • insert source lines that weren't processed because they didn't intersect any points

id column must be unique or this won't work correctly:

create table edges_split as
  select a.id, (st_dump(st_split(st_snap(a.geom,b.geom,1),b.geom))).geom as geom from
    edges a, 
    (select id, st_union(geom) as geom from points) b;
insert into edges_split (id, geom) 
  select id, geom from edges 
  where id not in (select id from edges_split);

Answered by jbalk on December 23, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP