[英]R function for creating discs around each point in a pattern, then counting number of points in each disc [spatial]
I am attempting to create a disc for each point in a pattern;我正在尝试为模式中的每个点创建一个光盘; each disc will have the same radius.每个圆盘将具有相同的半径。 Then for each disc, I want to count the number of points falling within the disc.然后对于每个光盘,我想计算落在光盘内的点数。 Each pattern has 100-400 points.每个模式有100-400点。 I have written code to do this, but it is quite slow.我已经编写了代码来做到这一点,但它很慢。 The code is below.代码如下。 I cannot provide the shapefile and points as that would be very difficult, but I could create some dummy data if need be.我无法提供 shapefile 和点,因为这会非常困难,但如果需要,我可以创建一些虚拟数据。
W <- as.owin(shape)
#Converts created .shp file into a "window"
#in which everything is plotted and calculated
SPDF <- SpatialPointsDataFrame(P[,1:2], P)
#Converts data frame to spatial points data frame
SP <- as(SPDF, "SpatialPoints") #Converts SPDF to spatial points
SP1 <- as.ppp(coordinates(SP), W)
SP2 <- as.ppp(SP1)
attr(SP1, "rejects")
attr(SP2, "rejects")
aw <- area.owin(W) #Area, in pixels squared, of leaf window created earlier
#awm <- aw * (meas)^2 * 100 #Area window in millimeters squared
# Trichome_Density_Count-----------------------------------------------------------------------------------------------
TC <- nrow(P) #Counts number of rows in XY data points file,
#this is number of trichomes from ImageJ
TD <- TC/awm #Trichome density, trichomes per mm^2
#SPDF2 <- as.SpatialPoints.ppp(SP2)
#kg <- knn.graph(SPDF2, k = 1)
#Creates the lines connecting each NND pairwise connection
#dfkg <- data.frame(kg) #Converts lines into a data frame
#dfkgl <- dfkg$length
meanlength <- 78
discstest <- discs(SP2, radii = meanlength,
separate = TRUE, mask = FALSE, trim = FALSE,
delta = NULL, npoly=NULL)
#Function creates discs for each trichome
#Using nearest neighbor lengths as radii
#NEED TO ADD CLIPPING
ratiolist <- c()
for (i in 1:length(discstest)) {
ow2sp <- owin2SP(discstest[[i]])
leafsp <- owin2SP(W)
tic("gIntersection")
intersect <- rgeos::gIntersection(ow2sp, leafsp)
Sys.sleep(1)
toc()
tic("over")
res <- as.data.frame(sp::over(SP, intersect, returnList = FALSE))
Sys.sleep(1)
toc()
res[is.na(res)] <- 0
newowin <- as.owin(intersect)
circarea <- area.owin(newowin)
trichactual <- sum(res)
trichexpect <- (TC / aw) * circarea
ratio <- trichactual / trichexpect
ratiolist[[i]] <- ratio
}
If I understand you correctly you want to loop through each point and check how many points fall within a disc of radius R centered in that point.如果我理解正确,您想遍历每个点并检查有多少点落在以该点为中心的半径为 R 的圆盘内。 This is done very efficiently in spatstat with the function closepaircounts
:这在 spatstat 中使用closepaircounts
函数非常有效地完成:
closepaircounts(SP2, r = meanlength)
This simply returns a vector with the number of points contained in the disc of radius r
for each point in SP2
.这只是返回一个向量,该向量包含SP2
每个点的半径为r
的圆盘中包含的点数。
I have just tried this for 100,000 points where each point on average had almost 3000 other points in the disc around it, and it took 8 seconds on my laptop.我刚刚尝试了 100,000 个点,其中每个点在它周围的光盘上平均有近 3000 个其他点,在我的笔记本电脑上花了 8 秒。 If you have many more points or in particular if the disc radius is so big that each disc contains many more points it may become very slow to calculate this.如果你有更多的点,或者特别是如果圆盘半径太大以至于每个圆盘包含更多的点,计算这个可能会变得非常慢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.