gpt4 book ai didi

r - 如何为每个数据集条目在相应的多边形中创建随机点

转载 作者:行者123 更新时间:2023-12-05 07:35:40 24 4
gpt4 key购买 nike

我创建了一个多边形图层 (Poly.buffer),以及一个虚拟观测数据集 (df),它与该多边形图层有一个公共(public)字段(即 SiteName)。

我现在想为相应多边形内的每个数据集条目生成一个随机点。

目标是有 138 个随机点,它们驻留在相应的多边形中并具有数据列表的所有属性。

这是我尝试过的(基于此 https://gis.stackexchange.com/questions/74250/how-to-create-random-point-in-according-polygon-for-each-dataset-entry-in-a-list ):

##load packages
library(ggplot2)
library(sp)
library(rgdal)
library(rgeos)

##First create dummy dataset
SiteName=c("Site 1", "Site 2", "Site 3", "Site 4", "Site 5")
Latitude=c("-16.40407","-16.41253","-16.39207","-16.40035","-16.40457")
Longitude=c("145.8157","145.7117","145.8308","145.8368","145.8405")

Poly=data.frame(SiteName,Latitude, Longitude)
Poly$Latitude=as.numeric(as.character(Poly$Latitude))
Poly$Longitude=as.numeric(as.character(Poly$Longitude))

Site1=data.frame(SiteName="Site 1",value=1:10)
Site2=data.frame(SiteName="Site 2",value=1:3)
Site3=data.frame(SiteName="Site 3",value=1:64)
Site4=data.frame(SiteName="Site 4",value=1:13)
Site5=data.frame(SiteName="Site 5",value=1:18)
Site1x=data.frame(SiteName="Site 1",value=1:30)
df=dplyr::bind_rows(Site1,Site2,Site3,Site4,Site5,Site1x)

##Create Poly.buffer##
coordinates(Poly)=~Longitude+Latitude
proj4string(Poly)=CRS("+proj=longlat +datum=WGS84")
Poly.utm <- spTransform(Poly, CRS("+proj=utm +zone=55 +south +units=m
+ellps=WGS84"))
Poly.buffer=gBuffer(Poly.utm,width=400,byid=TRUE)
Poly.buffer=spTransform(Poly.buffer, CRS("+proj=longlat +datum=WGS84"))

### Preparing the SpatialPointsDataFrame
spdf <- matrix(as.numeric(NA), nlevels(Poly.buffer$SiteName), 1)
spdf <- as.list(spdf)

### Sample the coordinate, match it with data in spdf.
### sample(spsample()) fix the size of the sample
for (i in seq(Poly.buffer$SiteName))
spdf[i] <- SpatialPointsDataFrame(
sample(spsample(Poly.buffer[order(Poly.buffer$SiteName)==i,], n = 200, "random"),
table(df$SiteName)[[i]]),
df[df$SiteName==dimnames(table(df$SiteName))[[1]][i],],
proj4string=poly.crs,
match.ID=FALSE)

## Merging together the list to make on SpatialDataFrame
do.call("rbind", spdf) -> spdf

我可以运行此代码的唯一方法是将 match.ID 选择为 FALSE。但是这样做会为每个与相应多边形不匹配的数据集条目生成坐标。

请看下面我绘制它时的样子:

df.spdf=as.data.frame(spdf)
fPoly.buffer=fortify(Poly.buffer)
ggplot(data=df.spdf, aes(x=x,y=y)) + geom_polygon(data=fPoly.buffer,
aes(long, lat, group = group)) + geom_point(size=0.1,aes(color=SiteName))

enter image description here

当我尝试使用 match.ID=TRUE 运行代码时,我收到以下错误消息:

Error in SpatialPointsDataFrame(sample(spsample(Poly[order(Poly$SiteName) == : row.names of data and coords do not match

我不确定如何处理这个问题。任何建议将不胜感激。

请看下面,它应该是什么样子。当我从虚拟 df 中排除 Site1x 时,代码工作正常。但我需要它才能使用它。

enter image description here

最佳答案

所以问题是当我执行 do.call 函数时,它会根据行号合并不同的列表组件。我通过简单地重新排序我的数据集并重新分配新的行号来解决这个问题。

df=df[order(df$SiteName),]
rownames(df) <- 1:nrow(df)

关于r - 如何为每个数据集条目在相应的多边形中创建随机点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49397452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com