Skip to contents

Calculates distances between two vectors of geometries. There are a number of different distance methods that can be utilized.

Usage

distance_euclidean_pairwise(x, y)

distance_hausdorff_pairwise(x, y)

distance_vicenty_pairwise(x, y)

distance_geodesic_pairwise(x, y)

distance_haversine_pairwise(x, y)

distance_euclidean_matrix(x, y)

distance_hausdorff_matrix(x, y)

distance_vicenty_matrix(x, y)

distance_geodesic_matrix(x, y)

distance_haversine_matrix(x, y)

Arguments

x

and object of class rsgeo

y

and object of class rsgeo

Value

For _matrix functions, returns a dense matrix of distances whereas _pairwise

functions return a numeric vector.

Details

There are _pairwise() and _matrix() suffixed functions to generate distances pairwise or as a dense matrix respectively. The pairwise functions calculate distances between the ith element of each vector. Whereas the matrix functions calculate the distance between each and every geometry.

Euclidean distance should be used for planar geometries. Haversine, Geodesic, and Vicenty are all methods of calculating distance based on spherical geometries. There is no concept of spherical geometries in rsgeo, so choose your distance measure appropriately.

Notes

  • Hausdorff distance is calculated using Euclidean distance.

  • Haversine, Geodesic, and Vicenty distances only work with rs_POINT geometries.

Examples

set.seed(1)
x <- geom_point(runif(10, -1, 1), runif(10, -1, 1))
y <- rev(x)

distance_euclidean_matrix(x, y)
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>  [1,] 1.2133944 0.8062406 1.7594793 1.7006432 1.3937599 1.1349355 1.3338548
#>  [2,] 1.3525922 0.6555834 1.7298869 1.5755163 1.2330258 1.2345642 1.1497159
#>  [3,] 1.0380092 0.6242009 0.6346275 0.7461572 0.7531728 0.7605975 0.9038199
#>  [4,] 1.8667050 0.5582468 1.3124564 0.6710052 0.2280381 1.6099346 0.0000000
#>  [5,] 0.2802043 1.1569738 1.0200002 1.4896528 1.4959453 0.0000000 1.6099346
#>  [6,] 1.7642711 0.5877217 1.0967047 0.4494745 0.0000000 1.4959453 0.2280381
#>  [7,] 1.7698274 0.9242108 0.7894811 0.0000000 0.4494745 1.4896528 0.6710052
#>  [8,] 1.2724909 1.2253814 0.0000000 0.7894811 1.0967047 1.0200002 1.3124564
#>  [9,] 1.3853455 0.0000000 1.2253814 0.9242108 0.5877217 1.1569738 0.5582468
#> [10,] 0.0000000 1.3853455 1.2724909 1.7698274 1.7642711 0.2802043 1.8667050
#>            [,8]      [,9]     [,10]
#>  [1,] 1.1416973 0.2211987 0.0000000
#>  [2,] 1.0970286 0.0000000 0.2211987
#>  [3,] 0.0000000 1.0970286 1.1416973
#>  [4,] 0.9038199 1.1497159 1.3338548
#>  [5,] 0.7605975 1.2345642 1.1349355
#>  [6,] 0.7531728 1.2330258 1.3937599
#>  [7,] 0.7461572 1.5755163 1.7006432
#>  [8,] 0.6346275 1.7298869 1.7594793
#>  [9,] 0.6242009 0.6555834 0.8062406
#> [10,] 1.0380092 1.3525922 1.2133944
#> attr(,"class")
#> [1] "matrix" "array" 
distance_hausdorff_matrix(x, y)
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>  [1,] 1.2133944 0.8062406 1.7594793 1.7006432 1.3937599 1.1349355 1.3338548
#>  [2,] 1.3525922 0.6555834 1.7298869 1.5755163 1.2330258 1.2345642 1.1497159
#>  [3,] 1.0380092 0.6242009 0.6346275 0.7461572 0.7531728 0.7605975 0.9038199
#>  [4,] 1.8667050 0.5582468 1.3124564 0.6710052 0.2280381 1.6099346 0.0000000
#>  [5,] 0.2802043 1.1569738 1.0200002 1.4896528 1.4959453 0.0000000 1.6099346
#>  [6,] 1.7642711 0.5877217 1.0967047 0.4494745 0.0000000 1.4959453 0.2280381
#>  [7,] 1.7698274 0.9242108 0.7894811 0.0000000 0.4494745 1.4896528 0.6710052
#>  [8,] 1.2724909 1.2253814 0.0000000 0.7894811 1.0967047 1.0200002 1.3124564
#>  [9,] 1.3853455 0.0000000 1.2253814 0.9242108 0.5877217 1.1569738 0.5582468
#> [10,] 0.0000000 1.3853455 1.2724909 1.7698274 1.7642711 0.2802043 1.8667050
#>            [,8]      [,9]     [,10]
#>  [1,] 1.1416973 0.2211987 0.0000000
#>  [2,] 1.0970286 0.0000000 0.2211987
#>  [3,] 0.0000000 1.0970286 1.1416973
#>  [4,] 0.9038199 1.1497159 1.3338548
#>  [5,] 0.7605975 1.2345642 1.1349355
#>  [6,] 0.7531728 1.2330258 1.3937599
#>  [7,] 0.7461572 1.5755163 1.7006432
#>  [8,] 0.6346275 1.7298869 1.7594793
#>  [9,] 0.6242009 0.6555834 0.8062406
#> [10,] 1.0380092 1.3525922 1.2133944
#> attr(,"class")
#> [1] "matrix" "array" 
distance_vicenty_matrix(x, y)
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>  [1,] 134272.26  89636.58 194817.22 188855.18 154968.89 125505.43 148409.63
#>  [2,] 149774.17  72789.93 191424.33 174831.28 137008.98 136581.25 127870.95
#>  [3,] 115523.55  69035.72  70209.61  83056.06  83700.88  84639.96 100310.40
#>  [4,] 207552.27  62143.14 145262.60  74201.85  25216.41 178940.79      0.00
#>  [5,]  31190.18 128402.20 113393.90 165815.99 166378.62      0.00 178940.79
#>  [6,] 196263.22  65354.70 121420.16  49714.56      0.00 166378.62  25216.41
#>  [7,] 197002.96 102515.35  87597.49      0.00  49714.56 165815.99  74201.85
#>  [8,] 141534.10 135498.20      0.00  87597.49 121420.16 113393.90 145262.60
#>  [9,] 153875.69      0.00 135498.20 102515.35  65354.70 128402.20  62143.14
#> [10,]      0.00 153875.69 141534.10 197002.96 196263.22  31190.18 207552.27
#>            [,8]      [,9]     [,10]
#>  [1,] 126489.10  24610.78      0.00
#>  [2,] 121412.72      0.00  24610.78
#>  [3,]      0.00 121412.72 126489.10
#>  [4,] 100310.40 127870.95 148409.63
#>  [5,]  84639.96 136581.25 125505.43
#>  [6,]  83700.88 137008.98 154968.89
#>  [7,]  83056.06 174831.28 188855.18
#>  [8,]  70209.61 191424.33 194817.22
#>  [9,]  69035.72  72789.93  89636.58
#> [10,] 115523.55 149774.17 134272.26
#> attr(,"class")
#> [1] "matrix" "array" 
distance_geodesic_matrix(x, y)
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>  [1,] 134272.26  89636.58 194817.22 188855.18 154968.89 125505.43 148409.63
#>  [2,] 149774.17  72789.93 191424.33 174831.28 137008.98 136581.25 127870.95
#>  [3,] 115523.55  69035.72  70209.61  83056.06  83700.88  84639.96 100310.40
#>  [4,] 207552.27  62143.14 145262.60  74201.85  25216.41 178940.79      0.00
#>  [5,]  31190.18 128402.20 113393.90 165815.99 166378.62      0.00 178940.79
#>  [6,] 196263.22  65354.70 121420.16  49714.56      0.00 166378.62  25216.41
#>  [7,] 197002.96 102515.35  87597.49      0.00  49714.56 165815.99  74201.85
#>  [8,] 141534.10 135498.20      0.00  87597.49 121420.16 113393.90 145262.60
#>  [9,] 153875.69      0.00 135498.20 102515.35  65354.70 128402.20  62143.14
#> [10,]      0.00 153875.69 141534.10 197002.96 196263.22  31190.18 207552.27
#>            [,8]      [,9]     [,10]
#>  [1,] 126489.10  24610.78      0.00
#>  [2,] 121412.72      0.00  24610.78
#>  [3,]      0.00 121412.72 126489.10
#>  [4,] 100310.40 127870.95 148409.63
#>  [5,]  84639.96 136581.25 125505.43
#>  [6,]  83700.88 137008.98 154968.89
#>  [7,]  83056.06 174831.28 188855.18
#>  [8,]  70209.61 191424.33 194817.22
#>  [9,]  69035.72  72789.93  89636.58
#> [10,] 115523.55 149774.17 134272.26
#> attr(,"class")
#> [1] "matrix" "array" 
distance_haversine_matrix(x, y)
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>  [1,] 134923.24  89647.97 195643.97 189101.44 154976.98 126199.22 148314.34
#>  [2,] 150401.00  72896.21 192354.10 175188.13 137104.27 137277.27 127839.24
#>  [3,] 115417.79  69408.06  70567.05  82966.95  83748.67  84572.11 100500.02
#>  [4,] 207566.40  62073.77 145937.86  74612.47  25356.71 179015.27      0.00
#>  [5,]  31155.92 128649.02 113410.65 165636.07 166339.64      0.00 179015.27
#>  [6,] 196175.53  65351.60 121947.05  49979.34      0.00 166339.64  25356.71
#>  [7,] 196788.75 102767.34  87782.76      0.00  49979.34 165636.07  74612.47
#>  [8,] 141483.13 136256.37      0.00  87782.76 121947.05 113410.65 145937.86
#>  [9,] 154042.39      0.00 136256.37 102767.34  65351.60 128649.02  62073.77
#> [10,]      0.00 154042.39 141483.13 196788.75 196175.53  31155.92 207566.40
#>            [,8]      [,9]     [,10]
#>  [1,] 126950.63  24594.88      0.00
#>  [2,] 121983.92      0.00  24594.88
#>  [3,]      0.00 121983.92 126950.63
#>  [4,] 100500.02 127839.24 148314.34
#>  [5,]  84572.11 137277.27 126199.22
#>  [6,]  83748.67 137104.27 154976.98
#>  [7,]  82966.95 175188.13 189101.44
#>  [8,]  70567.05 192354.10 195643.97
#>  [9,]  69408.06  72896.21  89647.97
#> [10,] 115417.79 150401.00 134923.24
#> attr(,"class")
#> [1] "matrix" "array" 

distance_euclidean_pairwise(x, y)
#>  [1] 1.2133944 0.6555834 0.6346275 0.6710052 1.4959453 1.4959453 0.6710052
#>  [8] 0.6346275 0.6555834 1.2133944
distance_hausdorff_pairwise(x, y)
#>  [1] 1.2133944 0.6555834 0.6346275 0.6710052 1.4959453 1.4959453 0.6710052
#>  [8] 0.6346275 0.6555834 1.2133944
distance_vicenty_pairwise(x, y)
#>  [1] 134272.26  72789.93  70209.61  74201.85 166378.62 166378.62  74201.85
#>  [8]  70209.61  72789.93 134272.26
distance_geodesic_pairwise(x, y)
#>  [1] 134272.26  72789.93  70209.61  74201.85 166378.62 166378.62  74201.85
#>  [8]  70209.61  72789.93 134272.26
distance_haversine_pairwise(x, y)
#>  [1] 134923.24  72896.21  70567.05  74612.47 166339.64 166339.64  74612.47
#>  [8]  70567.05  72896.21 134923.24