【发布时间】:2018-11-05 18:12:51
【问题描述】:
我有一个 WKT 文件,其中包含数百个 POLYGON((...,...,...)) 条目。是否有用于读取、绘制和转换此类数据的 R 包?我没有发现任何明确的内容。当可能有更复杂的现有方法时,只想避免使用字符串。提前致谢。
【问题讨论】:
-
google 搜索后出现的哪些软件包没有按照您的需要运行?
我有一个 WKT 文件,其中包含数百个 POLYGON((...,...,...)) 条目。是否有用于读取、绘制和转换此类数据的 R 包?我没有发现任何明确的内容。当可能有更复杂的现有方法时,只想避免使用字符串。提前致谢。
【问题讨论】:
好的,我找到了两个包,可以让我找到一个简单的解决方案。这是从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
【讨论】: