Geographic / geospatial distance between 2 lists of lat/lon points (coordinates)

后端 未结 3 1960
情话喂你
情话喂你 2020-11-22 07:49

I have 2 lists (list1, list2) with latitude / longitudes of various locations. One list (list2) has locality names that list1

3条回答
  •  佛祖请我去吃肉
    2020-11-22 08:18

    I add below a solution using the spatialrisk package. The key functions in this package are written in C++ (Rcpp), and are therefore very fast.

    The function spatialrisk::points_in_circle() calculates the observations within radius from a center point. Note that distances are calculated using the Haversine formula. Since each element of the output is a data frame, purrr::map_dfr is used to row-bind them together:

    ans <- purrr::map2_dfr(list1$longitude, 
                           list1$latitude, 
                           ~spatialrisk::points_in_circle(list2, .x, .y, 
                                                          lon = longitude, 
                                                          lat = latitude, 
                                                          radius = 2000000)[1,])
    cbind(list1, ans)
    
        longitude latitude longitude latitude locality  distance_m
    1   80.15998 12.90524  77.76180 13.02212        D 260484.0591
    2   72.89125 19.08120  72.89537 19.07726        A    616.6369
    3   77.65032 12.97238  77.64214 13.00954        C   4230.7216
    4   77.60599 12.90927  77.58415 12.92079        D   2694.4566
    5   72.88120 19.08225  72.89537 19.07726        A   1590.8723
    6   76.65460 12.81447  76.65460 12.81447        E      0.0000
    7   72.88232 19.08241  72.89537 19.07726        A   1487.8028
    8   77.49186 13.00984  77.58415 12.92079        D  14089.1051
    9   72.82228 18.99347  72.89537 19.07726        A  12089.6454
    10  72.88871 19.07990  72.89537 19.07726        A    759.8012
    

提交回复
热议问题