- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 SpatialPointsDataFrame 加载
pst<-readOGR("/data_spatial/coast/","points_coast")
coord<-as.data.frame(coordinates(pst))
Slo1<-Line(coord)
Sli1<-Lines(list(Slo1),ID="coastLine")
coastline <- SpatialLines(list(Sli1))
class(coastline)
最佳答案
我看过shapefile。有一个id
列,但是如果您绘制数据,则 id 似乎不是按南北顺序排列的。创建额外的线是因为点顺序不完美,连接表中彼此相邻但在空间上彼此相距较远的点。您可以尝试通过计算点之间的距离然后按距离排序来找出数据的正确排序。
解决方法是删除那些长于特定距离的线条,例如500 m.. 首先,找出连续坐标之间的距离大于这个距离的地方:breaks
.然后取两个 breaks
之间的坐标子集最后为该子集创建 Lines。您最终会得到一条由多个 ( 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')
关于R 从 SpatialPointsDataFrame 到 SpatialLines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22597663/
我正在使用 sp 包创建 SpatialLines 对象并将其保存在对象列表 allLines 中。稍后我需要将 SpatialLines 相互比较,但这超出了当前的问题。 到目前为止,我只需要构造S
我有一个 SpatialPointsDataFrame 加载 pst500),nrow(coord)) # check position of breaks breaks = c(1,which(li
我正在尝试从 spatialLinesDataFrame 转换为 igraph 对象,并认为我可能会丢失我想要保留的信息。 igraph 相当新,所以请耐心等待。下面的例子说明了: # create
使用 leaflet ,我正在尝试绘制一些线条并根据“速度”变量设置它们的颜色。我的数据从编码的折线级别开始(即一系列纬度/经度点,编码为字母数字字符串),每个 EPL 都有一个速度值。 我能够解码折
我有一个代表道路的 SpatialLines 对象和一个 SpatialPolygons 对象含城市。 我想知道如何用 SpatialPolygons 覆盖 SpatialLines 对象R 中的对象
我有一个代表道路的 SpatialLines 对象和一个 SpatialPolygons 对象含城市。 我想知道如何用 SpatialPolygons 覆盖 SpatialLines 对象R 中的对象
我是一名优秀的程序员,十分优秀!