在多边形shapefile中创建随机点

时间:2019-09-06 09:52:52

标签: r shapefile

我正在尝试在多边形shapefile中创建随机图,然后以索引编号为1,2,3,...以及包含与每个索引编号相对应的XY坐标的图例进行绘制。

data(meuse.grid)
gridded(meuse.grid) = ~x+y
image(meuse.grid)
plot(meuse.grid, main="Inventory Region")
plots <- points(spsample(meuse.grid, n=10, type='regular'), col='red', pch=20, cex=2)

按索引,我的意思是序列号。从1到第n个点。 np是我根据采样强度创建的对象,对不起,我在问题中未作澄清。在我的情况下,np =10。我想在一个多边形shapefile中创建10个随机点(常规),然后创建一个显示多边形边界的图,其中所有随机点都带有序列号。分配给每个点。我还要显示所有这10个点XY坐标的图例

1 个答案:

答案 0 :(得分:2)

也许这就是您所需要的。

library(sp)
data(meuse.grid)
gridded(meuse.grid) = ~x+y
plot(meuse.grid, main="Inventory Region")
set.seed(1234)
pts <- spsample(meuse.grid, n=10, type='regular')
# Plot point numbers
xy <- pts@coords
npts <- nrow(xy)
points(pts, col='red', pch=20, cex=2)
text(xy[,1], xy[,2], 1:npts, col="red", pos=4)
# Plot table of x y coordinates
library(grid)
library(gridExtra)
vp = viewport(x=.1, y=.75, width=.15, height=.3,just="left", clip="on", angle=0)
pushViewport(vp)
tbl <- tableGrob(cbind(1:npts, xy), theme = ttheme_default(base_size=8, padding=unit(c(2,2), "mm")))
grid.draw(tbl)
upViewport()

enter image description here

相关问题