RockScience RockScience - 14 days ago 6
R Question

set precision in writeWKT from rgeos package

I am creating a WRT string from a specialPolygons object in R.

However I am surprised by the number of digits in the output. Is there a way to reduce it?

x = sp::SpatialPolygons(Srl = list(sp::Polygons(srl = list(sp::Polygon(coords = cbind(c(-19.8, -19.9, -19.9, -19.8, -19.8),c(148, 148, 148.2, 148.2, 148)))), ID = "1")), pO = 1:1)
rgeos::writeWKT(x)


This gives me:

"POLYGON ((-19.8000000000000007 148.0000000000000000, -19.8999999999999986 148.0000000000000000, -19.8999999999999986 148.1999999999999886, -19.8000000000000007 148.1999999999999886, -19.8000000000000007 148.0000000000000000))"

Answer

Functions in the excellent new sf package seem to work more like you'd hope:

library(sf)
st_as_text(st_as_sfc(x))
## [1] "POLYGON((-19.8 148, -19.9 148, -19.9 148.2, -19.8 148.2, -19.8 148))"