在 R 中围绕空间点数据创建缓冲区并计算缓冲区中有多少点

Create buffer around spatial point data in R and count how many points are in the buffer

我正在尝试使用 R 分析加油站点的空间密度。我需要在加油站周围创建一个缓冲区(圆圈)并计算缓冲区内加油站的数量。然后,我需要尝试调整缓冲距离,看看什么是合理的缓冲,才能看到有趣的东西。这些是我正在使用的文件:https://dl.dropboxusercontent.com/u/45095175/sbc_gas.shp; https://dl.dropboxusercontent.com/u/45095175/sbc_gas.shx; https://dl.dropboxusercontent.com/u/45095175/sbc_gas.dbf

# Install packages 
x <- c("ggmap", "rgdal", "rgeos", "maptools", "ks")
lapply(x, library, character.only = TRUE)
all <- readShapePoints("sbc_gas.shp") 
all.df <- as(all, "data.frame")
locs <- subset(all.df, select = c("OBJECTID", "Latitude", "Longitude"))
head(locs)  # a simple data frame with coordinates
coordinates(locs) <- c("Longitude", "Latitude")  # set spatial  coordinates
plot(locs)

非常感谢任何帮助!!

我们无法使用您提供的数据,因为仅 .shp 文件是不够的。至少,您还必须提供 .shx 和 .dbf 文件才能加载此数据。

但是,应该可以获取包 geosphere。它包含一个名为 distGeo 的函数。您可以使用它来获取从每个加油站到所有其他加油站的距离。从距离矩阵来看,你应该可以select指定距离内的所有站点。

我找到了问题的答案:fivekm <- cbind(coordinates(locs), X=rowSums(distm (coordinates(locs)[,1:2], fun = distHaversine) / 1000 <= 5)) # number of points within 5 km