我正在绘制具有以下顶点的多边形
x y
-0.02208709 -0.039161304
0.01184081 -0.020268029
0.04578401 -0.001351904
0.02210236 0.039176396
-0.01185226 0.020252146
-0.04578784 0.001352696
Run Code Online (Sandbox Code Playgroud)
使用以下代码
plot(x,y)
polygon(x,y)
points(mean(x),mean(y),col="red")
Run Code Online (Sandbox Code Playgroud)
现在我想沿着多边形的边绘制50个等间距点.有什么建议怎么办?
您可以spsample从sp包中执行此操作.
首先,我们将加载库并读入您的顶点.
library(sp)
xy <- read.table(text='x y
-0.02208709 -0.039161304
0.01184081 -0.020268029
0.04578401 -0.001351904
0.02210236 0.039176396
-0.01185226 0.020252146
-0.04578784 0.001352696', header=TRUE)
Run Code Online (Sandbox Code Playgroud)
现在SpatialLines从顶点创建一个对象.这是一个有点乱-看?SpatialLines和?`SpatialLines-Class`,如果你会被卡住.
l <- SpatialLines(list(Lines(Line(rbind(xy, xy[1, ])), ID=1)))
Run Code Online (Sandbox Code Playgroud)
然后对点进行采样并强制data.frame使用as.data.frame(pts)或coordinates(pts).
pts <- spsample(l, 50, type="regular")
coordinates(pts) # only the head shown here
## x y
## [1,] -0.019343310 -0.03763339
## [2,] -0.014987452 -0.03520776
## [3,] -0.010631594 -0.03278213
## [4,] -0.006275735 -0.03035651
## [5,] -0.001919877 -0.02793088
## [6,] 0.002435981 -0.02550525
plot(l)
points(pts, pch=20)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
395 次 |
| 最近记录: |