R从空间点数据帧到空间线
我有一个空间点数据帧加载R从空间点数据帧到空间线,r,line,point,spatial,R,Line,Point,Spatial,我有一个空间点数据帧加载 pst<-readOGR("/data_spatial/coast/","points_coast") pst我已经查看了shapefile。有一个id列,但是如果您绘制数据,则该id似乎不是按南北顺序排列的。创建额外的线是因为点顺序不完美,连接表中彼此相邻但在空间上彼此相距较远的点。您可以通过计算点之间的距离,然后按距离排序,尝试找出数据的正确顺序 解决方法是移除超过一定距离(例如500米)的管线。。首先,找出连续坐标之间的距离大于此距离的位置:中断。然后取两
pst<-readOGR("/data_spatial/coast/","points_coast")
pst我已经查看了shapefile。有一个id
列,但是如果您绘制数据,则该id似乎不是按南北顺序排列的。创建额外的线是因为点顺序不完美,连接表中彼此相邻但在空间上彼此相距较远的点。您可以通过计算点之间的距离,然后按距离排序,尝试找出数据的正确顺序
解决方法是移除超过一定距离(例如500米)的管线。。首先,找出连续坐标之间的距离大于此距离的位置:中断。然后取两个断点之间的坐标子集,最后为该子集创建线。最终得到的海岸线由几个(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')
#读取数据
图书馆(rgdal)
pstI看到文件“points_coast.zip”,但不知道如何下载它。你点击什么下载文件?有多少行?这些要点需要组织起来,而不仅仅是串在一起。是否有指定线路ID和/或线路内订购点的属性?若要下载,请单击valider et telechargez le fichier
。感谢koekenbakker提供的小指南:-)。@萨姆纳:我想要一行。。。但我的分数超过了4000分。/谢谢koekenbakker!!真是太棒了!一个小问题。。。我怎样才能写GR?
# 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')