在 rgeos 包的 writeWKT 中设置精度

set precision in writeWKT from rgeos package

我正在从 R 中的 specialPolygons 对象创建 WRT 字符串。

然而,我对输出中的位数感到惊讶。有什么办法可以减少吗?

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)

这给了我:

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

这行得通,需要 3 包而不是 1 包很烦人,但是:)

library(geojson)
library(wellknown)
library(jsonlite)
geoj <- as.geojson(x)
geojson2wkt(fromJSON(geoj, FALSE)$features[[1]]$geometry, fmt = 1)

#> [1] "POLYGON ((-19.8 148.0, -19.9 148.0, -19.9 148.2, -19.8 148.2, -19.8 148.0))"

出色的新功能 sf package 似乎更像您希望的那样工作:

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))"