0

I'm trying to snap one polygon to another, and I found that not all points are being snapped as expected. I tried to simplify the issue to make it all clear, and created this dummy example:

SELECT ST_AsText(ST_Snap(point,polygon, 0.00001)) AS point_snapped
FROM (SELECT
   ST_GeomFromText('POINT(-0.00000001 1)') As point,
   ST_GeomFromText('POLYGON((0 0,0 2,2 2,0 0))') As polygon
 ) As foo;
  point_snapped
-----------------
 POINT(-1e-08 1)

Why isn't the point "fixed" to be (0 1)? And why does it work when I modify the polygon to have that point (which doesn't change the geometry at all)?

SELECT ST_AsText(ST_Snap(point,polygon, 0.00001)) AS point_snapped
FROM (SELECT
   ST_GeomFromText('POINT(-0.00000001 1)') As point,
   ST_GeomFromText('POLYGON((0 0,0 1,0 2,2 2,0 0))') As polygon
 ) As foo;
 point_snapped
---------------
 POINT(0 1)

Of course one thing I can do to solve that, for each point not snapped correctly, is to add the closest point to it from the other geometry to that geometry, and then snap. But it's quite difficult, and if there is a nicer way to do it, with built-in PostGIS function, that would be better.

0

This is the expected behavior: snapping occurs on existing vertices only.

Snaps the vertices and segments of a geometry [to] another Geometry's vertices.

The solution would be to add the missing vertices, or to just consider the closest point. Note that you might want some extra validations should the closest point be within the tolerance distance of an existing vertex.

SELECT ST_AsText(ST_Snap(point,ST_ClosestPoint(polygon,point), 0.00001)) AS point_snapped
FROM (SELECT
   ST_GeomFromText('POINT(-0.00000001 1)') As point,
   ST_GeomFromText('POLYGON((0 0,0 2,2 2,0 0))') As polygon
 ) As foo;
 point_snapped
---------------
 POINT(0 1)
| improve this answer | |
  • Thanks!! Do you see a way to do it in one query when snapping polygon to polygon instead of point to polygon? (meaning, snapping each vertex to its closest vertex from the other polygon within the tolerance distance) – Nitzan Weissman Dec 3 '18 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.