[英]R - How do I draw a radius around a point and use that result to filter other points?
I am looking to draw a radius around a lat long point, then use that buffer to filter other points that fit within it.我希望在一个经纬度点周围绘制一个半径,然后使用该缓冲区来过滤适合其中的其他点。 For example:
例如:
#stores datasets
stores = data.frame(store_id = 1:3,
lat = c("40.7505","40.7502","40.6045"),
long = c("-73.8456","-73.8453","-73.8012")
)
#my location
me = data.frame(lat = "40.7504", long = "-73.8456")
#draw a 100 meter radius around me
#use the above result to check which points in dataset stores are within that buffer
Not sure how to approach this.不知道如何处理这个。 I've worked with
over
before to intersect points and polygons but not sure how to run a similar scenario on lone points.我以前曾使用
over
交叉点和多边形,但不确定如何在孤立点上运行类似的场景。
You could hypothetically try to do geometric calculations on the surface of a sphere or ellipsoid, but usually when one is performing geometric map operations, one uses a map projection which projects the lon-lat coordinates onto a flat surface.假设您可以尝试在球体或椭球体的表面上进行几何计算,但通常在执行几何地图操作时,人们使用地图投影将经纬度坐标投影到平面上。
Here is how this can be done using the sf package.以下是如何使用sf包完成此操作。 First, create your points in lon-lat coordinates:
首先,在 lon-lat 坐标中创建点:
library(sf)
lat <- c(40.7505, 40.7502, 40.6045)
lon <- c(-73.8456, -73.8453, -73.8012)
stores <- st_sfc(st_multipoint(cbind(lon, lat)), crs = 4326)
me <- st_sfc(st_point(c(-73.8456, 40.7504)), crs = 4326)
The crs = 4326
argument specifies the EPSG code for the lon-lat coordinate system. crs = 4326
参数指定经纬度坐标系的 EPSG 代码。 Next we need to pick a map projecton.接下来我们需要选择一个地图投影。 For this example I will use UTM zone 18, which contains the above points:
对于这个例子,我将使用 UTM zone 18,其中包含以上几点:
stores_utm <- st_transform(stores, "+proj=utm +zone=18")
me_utm <- st_transform(me, "+proj=utm +zone=18")
Now we can buffer the point representing ourself by 100 meters to produce a circle with radius 100 meters:现在我们可以缓冲代表我们自己 100 米的点以生成一个半径为 100 米的圆:
circle <- st_buffer(me_utm, 100)
Now, we're almost ready to use a geometric predicate to test which points are in the circle.现在,我们几乎准备好使用几何谓词来测试圆中的哪些点。 However,
stores_utm
is currently a MULTIPOINT
, so geometric predicates will treat it like one geometric entity.然而,
stores_utm
目前是一个MULTIPOINT
,所以几何谓词会将它视为一个几何实体。 We can fix this by casting stores_utm
as a POINT
, which will give us a collection of three separate points:我们可以通过将
stores_utm
为POINT
来解决这个问题,这将为我们提供三个独立点的集合:
stores_utm_column <- st_cast(stores_utm, "POINT")
stores_utm_column
# Geometry set for 3 features
# geometry type: POINT
# dimension: XY
# bbox: xmin: 597453 ymin: 4495545 xmax: 601422.3 ymax: 4511702
# epsg (SRID): 32618
# proj4string: +proj=utm +zone=18 +ellps=WGS84 +units=m +no_defs
# POINT (597453 4511702)
# POINT (597478.7 4511669)
# POINT (601422.3 4495545)
Now we can test which points are in the circle:现在我们可以测试哪些点在圆圈中:
> st_contains(circle, stores_utm_column, sparse = FALSE)
# [,1] [,2] [,3]
# [1,] TRUE TRUE FALSE
which shows that the first two points are in the circle and the third one is not.这表明前两个点在圆圈内,而第三个不在圆圈内。
Of course, every map projection introduces some distortions.当然,每个地图投影都会引入一些失真。 Your choice of projection will depend on the nature of your problem.
您选择的投影将取决于您的问题的性质。
The function points_in_circle() from the spatialrisk package handles this.空间风险包中的函数 points_in_circle() 处理这个。
For example, with your data:例如,使用您的数据:
library(spatialrisk)
# Stores
stores <- data.frame(store_id = 1:3,
lat = c(40.7505, 40.7502, 40.6045),
long = c(-73.8456, -73.8453, -73.8012))
# My location
me <- data.frame(lat = 40.7504, long = -73.8456)
> spatialrisk::points_in_circle(stores, me$long[1], me$lat[1], radius = 100, lon = long)
# store_id lat long distance_m
# 1 40.7505 -73.8456 11.13195
# 2 40.7502 -73.8453 33.70076
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.