Wednesday, 28 February 2018

postgresql - Snap point to point with St_Snap Postgis


I write a request that allows to snap a point to another. Here is the request:


 SELECT
f.gid as gid,
ST_Snap (f.Geom, g.Geom, min, 5) as geom

FROM
boiten as f,
(SELECT ST_Collect (Geom) as Geom
FROM ft_chambre) as g

It works but the snapped points missed the closest position


Update:


For the CRS it's 2154 ,I want to snap the point to the red ones enter image description here I modified my code to this:


SELECT
f.gid as gid,

ST_Snap(f.Geom, g.Geom, st_distance(f.Geom, g.Geom)*1.2) as geom
FROM
boiten as f,
(SELECT ST_Collect(Geom) as Geom
FROM ft_chambre) as g

enter image description here But like you see there are points witch snapped to the wrong place or has snapped to more than one.


For exemple This point has snapped to the wrong place where the other point has already snapped to enter image description here



Answer



A quick hack to guarantee that you always snap to the closest point:



SELECT f.gid AS gid,
ST_Snap(
f.Geom,
ST_ClosestPoint(g.Geom, f.Geom),
ST_Distance(
f.Geom,
ST_ClosestPoint(g.Geom, f.Geom)
) * 1.01
) AS geom
FROM (

SELECT ST_Collect(Geom) as Geom
FROM ft_chambre
) AS g,
boiten AS f

Let me add: if this gives you the same (apoarently wrong) result, it's the tolerance that you need to define properly. If there are points that you don't want to be snapped due to them being too far, you need to specify what distance is too far.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...