问题描述
我有一个已知的点(中心),并且具有已知的纬度和经度,并且在df中有一些坐标(以纬度/经度为单位),我想查看哪些坐标在距中心点半径5公里或更小的范围内。
I have a known point (centre) with known latitude and longitude and I have some coordinates (in lat/lon) in a df and I want to see which ones are within a radius of 5km or less from the centre point.
中心
mylon <- c(-2.106472)
mylat <- c(57.14455)
坐标
longitude latitude
[1,] -2.141177 57.16278
[2,] -2.090960 57.18079
[3,] -2.118894 57.12292
[4,] -2.140090 57.13763
[5,] -2.113988 57.13855
[6,] -2.123892 57.13585
[7,] -2.144685 57.17207
[8,] -2.220046 57.19150
[9,] -2.114343 57.09301
[10,] -2.285314 57.20138
[11,] -2.092354 57.14279
[12,] -2.149571 57.15334
[13,] -2.126605 57.15615
[14,] -2.097045 57.10443
[15,] -2.183441 57.15051
[16,] -2.166915 57.15089
[17,] -2.133863 57.15201
[18,] -2.100909 57.18968
[19,] -2.106770 57.15670
[20,] -2.251495 57.19315
[21,] -2.118894 57.12292
[22,] -2.140090 57.13763
[23,] -2.123201 57.12686
[24,] -2.114343 57.09301
[25,] -2.140327 57.15676
[26,] -2.148826 57.17355
[27,] -2.120553 57.12507
[28,] -2.133902 57.16279
[29,] -2.094246 57.17180
[30,] -2.113170 57.14125
[31,] -2.251495 57.19315
[32,] -2.090960 57.18079
[33,] -2.212955 57.10941
[34,] -2.118894 57.12292
[35,] -2.183501 57.19596
[36,] -2.140090 57.13763
[37,] -2.249217 57.10063
[38,] -2.123201 57.12686
[39,] -2.114343 57.09301
[40,] -2.092354 57.14279
[41,] -2.148826 57.17355
[42,] -2.120553 57.12507
[43,] -2.117338 57.15301
[44,] -2.116486 57.14484
[45,] -2.094981 57.13614
[46,] -2.232998 57.14629
[47,] -2.118894 57.12292
[48,] -2.140090 57.13763
[49,] -2.123201 57.12686
[50,] -2.104092 57.14485
[51,] -2.114343 57.09301
[52,] -2.148826 57.17355
[53,] -2.175179 57.15079
[54,] -2.090713 57.14755
[55,] -2.090960 57.18079
[56,] -2.118894 57.12292
[57,] -2.140090 57.13763
我将不胜感激。非常感谢
I would appreciate any help. Thanks a lot
推荐答案
这可以仅通过距离公式和转换来完成,但是一旦距离开始变大,精度就会提高会变得很糟糕。可以编写自己更严格的方法,但是使用 geosphere
包要容易得多,该包提供了用于确定地理空间距离的各种功能,并具有不同的精度和所需的计算能力。 distHaversine
是一个很好的起点:
This can be done with just the distance formula and conversion, but once the distances start to get bigger, accuracy will get very bad. It's possible to code your own more rigorous approach, but it's much easier to use the geosphere
package, which offers various functions for determining geospatial distance with varying degrees of accuracy and computational power required. distHaversine
is a good starting point:
coord <- cbind("longitude" = c(-2.141177, -2.09096, -2.118894, -2.14009, -2.113988, -2.123892, -2.144685, -2.220046, -2.114343, -2.285314, -2.092354, -2.149571, -2.126605, -2.097045, -2.183441, -2.166915, -2.133863, -2.100909, -2.10677, -2.251495, -2.118894, -2.14009, -2.123201, -2.114343, -2.140327, -2.148826, -2.120553, -2.133902, -2.094246, -2.11317, -2.251495, -2.09096, -2.212955, -2.118894, -2.183501, -2.14009, -2.249217, -2.123201, -2.114343, -2.092354, -2.148826, -2.120553, -2.117338, -2.116486, -2.094981, -2.232998, -2.118894, -2.14009, -2.123201, -2.104092, -2.114343, -2.148826, -2.175179, -2.090713, -2.09096, -2.118894, -2.14009),
"latitude" = c(57.16278, 57.18079, 57.12292, 57.13763, 57.13855, 57.13585, 57.17207, 57.1915, 57.09301, 57.20138, 57.14279, 57.15334, 57.15615, 57.10443, 57.15051, 57.15089, 57.15201, 57.18968, 57.1567, 57.19315, 57.12292, 57.13763, 57.12686, 57.09301, 57.15676, 57.17355, 57.12507, 57.16279, 57.1718, 57.14125, 57.19315, 57.18079, 57.10941, 57.12292, 57.19596, 57.13763, 57.10063, 57.12686, 57.09301, 57.14279, 57.17355, 57.12507, 57.15301, 57.14484, 57.13614, 57.14629, 57.12292, 57.13763, 57.12686, 57.14485, 57.09301, 57.17355, 57.15079, 57.14755, 57.18079, 57.12292, 57.13763))
str(coord) # like data above, a matrix, not a data.frame
#> num [1:57, 1:2] -2.14 -2.09 -2.12 -2.14 -2.11 ...
#> - attr(*, "dimnames")=List of 2
#> ..$ : NULL
#> ..$ : chr [1:2] "longitude" "latitude"
# make a data.frame to hold both numeric and logical values
coord_df <- data.frame(coord,
within_5km = geosphere::distHaversine(
coord,
c(-2.106472, 57.14455)
) / 1000 < 5) # convert m to km, check < 5
str(coord_df)
#> 'data.frame': 57 obs. of 3 variables:
#> $ longitude : num -2.14 -2.09 -2.12 -2.14 -2.11 ...
#> $ latitude : num 57.2 57.2 57.1 57.1 57.1 ...
#> $ within_5km: logi TRUE TRUE TRUE TRUE TRUE TRUE ...
table(coord_df$within_5km)
#>
#> FALSE TRUE
#> 13 44
这篇关于在距R中已知中心点的半径内找到纬度/经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!