【问题标题】:How to read, plot and convert WKT to table in R?如何在 R 中读取、绘制并将 WKT 转换为表格?
【发布时间】:2018-11-05 18:12:51
【问题描述】:

我有一个 WKT 文件,其中包含数百个 POLYGON((...,...,...)) 条目。是否有用于读取、绘制和转换此类数据的 R 包?我没有发现任何明确的内容。当可能有更复杂的现有方法时,只想避免使用字符串。提前致谢。

【问题讨论】:

  • google 搜索后出现的哪些软件包没有按照您的需要运行?

标签: r wkt


【解决方案1】:

好的,我找到了两个包,可以让我找到一个简单的解决方案。这是从POLYGON((...,...)) WKT 类型中提取坐标的代码。

str="POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"

library(rgeos)
# For this library you need to `sudo apt-get install libgeos++-dev` in Linux
test <-readWKT(str)
library(sp)
plot(test)
coords <- as.data.frame(coordinates(test@polygons[[1]]@Polygons[[1]])) # Extracts coordinates of the polygon

编辑:上述适用于单个字符串/WKT 对象。以下代码可应用于 WKT 文件,创建矩阵列表:

df <- read.table("yourfile.wkt",header = F, sep = "\t")
wow <- apply(df, 1, function(x) readWKT(as.character(x))) # Applies readWKT to every row of your df, i.e. to each WKT object
works = list()
for (i in 1:length(wow)) { 
  works[[i]] <- as.data.frame(coordinates(wow[[i]]@polygons[[1]]@Polygons[[1]]))
} # Loop populates a list with the coordinate matrices of each object of type polygon

【讨论】:

    【解决方案2】:

    一个选项是使用wellknown,它有一个fxn来查看leaflet的数据

    library(wellknown)
    str <- "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"
    wktview(str)
    

    如果你转换成geojson,你可以提取坐标,例如,

    wkt2geojson(str)$geometry$coordinates
    

    【讨论】:

      【解决方案3】:

      另一种选择是 sfmapview 包的组合:

      library(sf)
      library(mapview)
      str <- "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"
      pnt <- st_as_sfc(str, crs = 4326)
      mapview(pnt)
      

      【讨论】:

        猜你喜欢
        • 2015-08-04
        • 2018-09-29
        • 1970-01-01
        • 1970-01-01
        • 2021-11-07
        • 2023-04-11
        • 2014-01-22
        • 1970-01-01
        • 2020-12-26
        相关资源
        最近更新 更多