【问题标题】:R from SpatialPointsDataFrame to SpatialLinesR 从 SpatialPointsDataFrame 到 SpatialLines
【发布时间】:2014-03-25 18:45:12
【问题描述】:

我有一个 SpatialPointsDataFrame 加载与

pst<-readOGR("/data_spatial/coast/","points_coast")

我想在输出中获得 SpatialLines,我找到了一些东西

coord<-as.data.frame(coordinates(pst))
Slo1<-Line(coord)

Sli1<-Lines(list(Slo1),ID="coastLine")
coastline <- SpatialLines(list(Sli1))
class(coastline)

它似乎有效,但是当我尝试 plot(coastline) 时,我有一条不应该存在的线......

有人可以帮助我吗? The shapefile is here

【问题讨论】:

  • 我看到文件“points_coast.zip”,但不知道如何下载。你点击什么下载文件?
  • 多少行?这些点需要组织起来,而不是串在一起。是否有带有指定 ID 到行和/或行内排序的点的属性?
  • 要下载,请点击valider et telechargez le fichier
  • 感谢 koekenbakker 的小方法 :-)。 @mdsumner :我想要一条线...但是我有超过 4000 分../

标签: r line point spatial


【解决方案1】:

我查看了 shapefile。有一个id 列,但是如果您绘制数据,似乎id 没有按南北顺序排列。创建额外的线是因为点顺序不完美,连接表中彼此相邻但在空间方面彼此相距较远的点。您可以通过计算点之间的距离然后按距离排序来尝试找出数据的正确排序。

一种解决方法是删除那些长于一定距离的线条,例如500 m..首先,找出连续坐标之间的距离大于这个距离:breaks。然后取两个breaks 之间的坐标子集,最后为该子集创建线。您最终会得到一条由多个 (breaks-1) 段组成且没有错误段的海岸线。

# read data
library(rgdal)
pst<-readOGR("/data_spatial/coast/","points_coast")
coord<-as.data.frame(coordinates(pst))
colnames(coord) <- c('X','Y')

# determine distance between consective coordinates
linelength = LineLength(as.matrix(coord),sum=F)
# 'id' of long lines, plus first and last item of dataset
breaks = c(1,which(linelength>500),nrow(coord))

# check position of breaks
breaks = c(1,which(linelength>500),nrow(coord))

# plot extent of coords and check breaks
plot(coord,type='n')
points(coord[breaks,], pch=16,cex=1)

# create vector to be filled with lines of each subset
ll <- vector("list", length(breaks)-1)
for (i in 1: (length(breaks)-1)){
    subcoord = coord[(breaks[i]+1):(breaks[i+1]),]

# check if subset contains more than 2 coordinates
    if (nrow(subcoord) >= 2){
        Slo1<-Line(subcoord)
        Sli1<-Lines(list(Slo1),ID=paste0('section',i))
        ll[[i]] = Sli1
    }

}

# remove any invalid lines
nulls = which(unlist(lapply(ll,is.null)))
ll = ll[-nulls]
lin = SpatialLines(ll)
# add result to plot
lines(lin,col=2)

# write shapefile
df = data.frame(row.names=names(lin),id=1:length(names(lin)))
lin2 = SpatialLinesDataFrame(sl=lin, data=df)
proj4string(lin2) <- proj4string(pst)
writeOGR(obj=lin2, layer='coastline', dsn='/data_spatial/coast', driver='ESRI Shapefile')

【讨论】:

  • 感谢 koekenbakker !效果很好!一个小问题……我怎么写OGR?
猜你喜欢
  • 2014-08-19
  • 2019-05-06
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 2014-03-12
  • 2019-05-02
  • 2014-08-08
  • 2016-10-21
相关资源
最近更新 更多