简体   繁体   English

R - 如何围绕一个点绘制半径并使用该结果过滤其他点?

[英]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_utmPOINT来解决这个问题,这将为我们提供三个独立点的集合:

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.

相关问题 如何以plot中的点为中心画一定半径的圆? - How to draw the circle with certain radius around the point in the plot? KNN可视化 - 如何使用R在连接到N个最近点的数据点周围绘制圆 - KNN visualization - How to draw a circle around a data point connecting to N nearest points using R 如何围绕多个点绘制定义半径的圆并导出到 kml 文件 - How to draw circles of defined radius around multiple points and export to kml file 如何绘制XYZ点并将点大小取决于R中的Z值? - How can I draw XYZ points and depend point size on the Z value in R? 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 中的 gggene 包根据起始位置值绘制彼此相邻的大小相等的基因? - How do I use gggene package in R to draw genes of equal size adjacent to each other based on their start position value? 求R中数据点一定半径内的点数 - Find the Number of Points Within a Certain Radius of a Data Point in R 如何使用闪亮范围滑块在传单点地图上过滤点 - How to use shiny range slider to filter points on leaflet point map 在R中的空间点数据周围创建缓冲区,并计算缓冲区中有多少个点 - Create buffer around spatial point data in R and count how many points are in the buffer 围绕地理点(纬度,经度)在半径范围内生成均匀分布的地理点 - Generate uniform distributed geo points around a geo point (Lat,Long) in a radius
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM