在一个地理点周围创建一个缓冲区,然后检查坐标列表是否在该缓冲区内?

Creating a buffer around a geographic point and then checking whether a list of coordinates is inside that buffer?

关于 R 中的空间数据,我有以下困惑:

我有一个包含街段的数据集(具有各自的起点和终点坐标)。我想在这些点周围创建一个 X 米的缓冲区,然后检查 lat/lon 个点的列表是否在该缓冲区内。有没有办法在 R 中做到这一点?

我能够使用各种包的组合来映射点和映射缓冲区:maptools、ggmap、rgdal、sp 和 rgeos。但是这个过程似乎只映射点和缓冲区,不允许我检查我拥有的其他坐标是否在缓冲区内。理想情况下,我想生成一个 1 和 0 的向量,描述 lat/lon 点列表是否在街道段周围的缓冲区内。

有什么想法吗?

这是我一直在使用的代码,但我得到了所有缺失值(而且我知道情况不应该如此)。我也试过使用 rgeos 的 gContains 函数,但它使 R 崩溃了。

#Load shapefile in R and transform to appropriate CRS
shp <- readOGR(dsn="/Users/Maps/shapes", layer="shp")
shp_transf <- spTransform(shp_transf, CRS( "+init=epsg:21897" ))

#Create buffer around polygons
shp_buff <- gBuffer(shp_transf, width=40, byid=TRUE, quadsegs=10)

#Make my dataframe of lat/lon points into same projection as buffers
points <- SpatialPoints(points,proj4string=CRS(proj4string(shp_buff)))

#Use over function from SP pacakge
result <- as.integer(over(points, shp_buff)$OBJECTID)

事实证明,over() 函数之前不起作用,因为带有缓冲区的 shapefile 和 GPS 坐标列表位于不同的 CRS 中。我是这样修复的:

shp <- spTransform(shp, CRS("+proj=longlat +datum=WGS84")) proj4string(points) <- CRS("+proj=longlat +datum=WGS84")

然后,你 运行 over() 函数,它会给你每个点在每个缓冲区中的次数:

x <- over(points, shp) table(x$ID)

181 304

118 8

因此,一个特定的 GPS 脉冲来自段 ID 181 总共 118 次,来自段 ID 304 总共 8 次。