3

Is there a snap to grid function available in sf for R similar to ST_SnapToGrid(geometry geomA, float size)in PostGIS? It is mentioned as a solution to non-noded intersection problem for PostGIS and am looking for a solution in R. Using other snap options with just the polygon lead to geometry errors (even with small tolerance).

  • Perhaps all coordinates can be rounded to certain precision, for the same effect as ST_SnapToGrid – Michael Dorman Feb 3 at 13:17
  • I'm not sure I understand your suggestion. I ran into errors when I tried to simplify my features as bits of some polygons were missing bits of other polygons or st_snap. – user3386170 Feb 3 at 19:07
  • I've posted example below – Michael Dorman Feb 4 at 6:20
2

Rounding coordinates may be equivalent to snapping all of the shapes coordinates to a regular grid.

Before -

> pnt = st_point(c(0,0))
> pol = st_buffer(pnt, 1)
> plot(pol)

enter image description here

After -

> pol[[1]] = round(pol[[1]], 1)
> plot(pol)

enter image description here

  • I'll try this later and mark it as accepted once I've checked that it works, but it looks promising! – user3386170 Feb 4 at 14:31
2

The R equivalent of the ST_SnapToGrid in PostGIS is in the lwgeom package:

# Snap to grid of 5000 m
lwgeom::st_snap_to_grid(x, 5000)

Works well to solve the non-noded intersection problem, and is quicker than applying a buffer of the same tolerance.

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.