PostGIS – Using ST_Snap with Condition

postgissnappingsql

I don't understand. I try to snap points to line but only points with the same id than line.
I try

CREATE TABLE snaped_pr2 AS
(SELECT axe, ST_ClosestPoint(pr.geom,rr.geom)::geometry(POINT,2154) AS geom
FROM pointr pr JOIN road rr ON pr.axe=rr.idroad);

or

CREATE TABLE snaped_pr2 AS
(SELECT axe, ST_ClosestPoint(pr.geom,rr.geom)::geometry(POINT,2154) AS geom
FROM pointr pr,road rr WHERE pr.axe=rr.idroad);

This code snap all points and create many points on lines intersections (snap point to line with not the same id)!

in image, the point with 29_D0026 must snap to line 29_D0026, and not to 29_D00168, i.e.

enter image description here

Best Answer

try this method it will select the closest line to each point and find the shortest point and shortest line. Maybe your attribute join is not correct? little bit unclear what the problem is but give this a shot and post a picture or two for clarity

select pr.axe,st_distance(pr.geom,rr.geom) dist,st_closestpoint(pr.geom,rr.geom) geom, st_shortestline(pr.geom,rr.geom) line_geom
    from points pr 
    cross join lateral
    (select r.geom
        from road r 
        order by r.geom <-> pr.geom
        limit 1) rr 
order by dist;
Related Question