我正在从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))"
答案 0 :(得分:2)
优秀的新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))"
答案 1 :(得分:1)
这很有效,烦人需要3 pkgs而不是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))"