简体   繁体   English

R 函数,用于在模式中的每个点周围创建圆盘,然后计算每个圆盘中的点数 [空间]

[英]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.

相关问题 R | sf:我在每个点周围都有点和 2 个缓冲区。 如果较大的缓冲区重叠(但较小的缓冲区不重叠),如何将这些点组合成单个单元? - R | sf: I have points and 2 buffers around each point. How to combine the points into single units if larger buffer overlaps (but smaller does not)? 计算每行在 R 中重复的次数 - Counting Number of Times Each Row is Duplicated in R 计算 [R] 中每行的大写字母数 - Counting the Number of Capital Letters in Each Row in [R] 在R中的空间点数据周围创建缓冲区,并计算缓冲区中有多少个点 - Create buffer around spatial point data in R and count how many points are in the buffer 如何高效统计R中栅格像元周围一定距离内的空间点数? - How to efficiently count the number of spatial points within a certain distance around raster cells in R? 在R中创建时间序列图,每天对实例进行分箱,并根据分箱中的实例数绘制点大小 - creating a time series plot in R, binning instances in each day, and plotting point size by number of instances in the bin R-为每个组有效地计算二进制变量中的开关数量 - R - Efficiently counting number of switches in binary variable for each group 计算R中数据帧的每一列中用逗号“,”分隔的单词数 - Counting number of words, seperated by comma “,”, in each column of a data frame in R 是否可以在 R 中给定另一个潜在的空间点模式来分析空间点模式 - Is it possible to analyse a spatial point pattern given another, underlying, spatial point pattern in R 创建一个 function 循环遍历 R 中的每一行 - Creating a function looping over each row in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM