【问题标题】:Creating random points in a polygon shapefile在多边形 shapefile 中创建随机点
【发布时间】:2020-01-09 04:41:18
【问题描述】:

我正在尝试在多边形 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 坐标显示一个图例

【问题讨论】:

    标签: r shapefile


    【解决方案1】:

    也许这就是你需要的。

    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()
    

    【讨论】:

    • @zx8754 感谢 Marco 的解决方案,它奏效了。一个小问题,当我导出地图时,图例与地图边界相交。我试图将位置更改为左下角,但没有进行任何更改。您能否提供一些选项来将图例位置调整到理想的位置。
    • @amithaldar “导出地图”是什么意思?您是否将绘图保存在文件中?如果是,为避免地图和图例重叠,您需要放大尺寸(宽度和高度)。告诉我。
    • 是的,我正在将绘图以 pdf 格式自动保存在目标文件夹中。我尝试了不同的宽度和高度,但图例仍然与边界重叠。虽然如果我手动拉伸查看器,那么图例就会得到调整。如果能自动保存就好了。我使用的代码为:dev.copy2pdf(file="Random Plots (D015).pdf", width = 7, height = 5)
    • @amithaldar 这就是我使用dev.copy2pdf(file="Random Plots (D015).pdf", width = 9, height = 7) 得到的; filebin.net/bpjgc8lhd6grl04r 没有重叠。如果您觉得我上面的分析器有用,请点赞并接受。谢谢。
    • @zx8754 嗨,Marco,我会弄清楚如何显示图例。非常感谢您的解决方案,它真的是一个很大的帮助。不幸的是,我的声誉目前低于 15,因此,不能为你的答案投票,但我会在我跨过门槛的那一刻做。再次感谢您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2020-02-13
    • 2019-10-07
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    相关资源
    最近更新 更多