Measures the distance from points x to features in layer y.
distance_to(x, y, measure = NULL)
x | points to measure distances from, to layer |
---|---|
y | feature layer to measure distance to. Expecting an |
measure | method used to measure geographic distances between longlat
|
A vector
of distances from points in x
to features in layer y
.
Uses the function nabor::knn
to determine the distance from each point in x
to the nearest feature in layer y
. If the input CRS is longlat, eg. EPSG 4326,
the distance is returned as measured by geodist::geodist
. Otherwise, if the
input CRS indicates projected coordinates, the distance returned is the
euclidean distance. Both x
and y
are expected to be sf
objects and
the distances are returned as vector, easily added to input x
with $<-
or other methods. If y
is a 'POLYGON' or 'MULTIPOLYGON' object, the
distance returned for points in x
within features in y
are set to 0.
# Load sf library(sf) # Load nc data nc <- st_read(system.file("shapes/sids.shp", package="spData")) #> Reading layer `sids' from data source #> `/home/alecr/R/x86_64-pc-linux-gnu-library/4.1/spData/shapes/sids.shp' #> using driver `ESRI Shapefile' #> Simple feature collection with 100 features and 22 fields #> Geometry type: MULTIPOLYGON #> Dimension: XY #> Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965 #> CRS: NA st_crs(nc) <- "+proj=longlat +datum=NAD27" # Set number of sampling points npts <- 1e3 # Sample points in nc ncpts <- st_sample(nc, npts) # Select first 5 of nc ncsub <- nc[1:5,] # Measure distance from ncpts to first 5 of nc, printing result distance_to(ncpts, ncsub, measure = 'geodesic') #> [1] 41840.8191 209585.1570 112725.9447 155024.1735 86644.5665 217974.2049 #> [7] 73364.8669 89418.5301 115351.8339 65144.5547 213330.6485 80711.6394 #> [13] 235752.5339 142596.0111 104931.5367 74637.7932 184203.8517 99796.4808 #> [19] 21372.6838 98022.4962 68551.3481 44442.0439 103143.2958 104389.7790 #> [25] 239309.0198 142741.4747 266849.3132 62839.8761 14449.6294 7358.9517 #> [31] 22777.7071 59724.6084 167044.6821 76844.5574 55563.7467 75906.5612 #> [37] 122278.8298 210961.7857 227079.4466 1014.3180 172515.0809 93359.7980 #> [43] 243418.2725 126853.8944 25736.9328 90840.9744 49764.4965 47161.3121 #> [49] 161132.6653 128387.4998 103343.8585 62825.7798 35532.0787 87513.4400 #> [55] 67854.3467 103161.8763 136610.4420 198925.1529 71495.4207 153510.1359 #> [61] 149076.2244 127957.7170 90800.0630 206585.3537 52170.9321 143926.9834 #> [67] 179408.2356 269515.9615 63615.1326 28005.7232 3154.7035 72087.1253 #> [73] 170795.9041 121127.1693 78512.9407 123636.7958 184029.0840 268815.5968 #> [79] 135242.0307 61103.9505 132315.5555 226547.4872 228445.5960 110706.0053 #> [85] 203489.2561 58918.5443 75668.3003 229901.4751 16222.6216 110955.9043 #> [91] 76406.4080 120486.2258 10666.5031 97023.3972 167761.8336 32009.3204 #> [97] 132589.0064 252494.4688 85421.2626 67159.6995 100307.2802 74819.5522 #> [103] 191254.1968 80639.8774 259184.5030 179187.1296 80470.7343 23360.9200 #> [109] 65420.1231 66460.8107 45713.7259 12351.6027 50643.7469 84388.4760 #> [115] 160693.9609 0.0000 161278.5034 67017.7322 137461.2297 85308.0224 #> [121] 10413.3252 220367.4154 271204.2327 143247.4312 156486.2487 238064.2541 #> [127] 71445.8127 0.0000 126347.7832 105976.9847 103854.2232 41292.6257 #> [133] 175575.7284 62693.2499 122966.2782 84785.1044 116394.8566 77940.8194 #> [139] 238142.1305 174696.8396 105385.4171 100035.9124 126730.4915 173625.2202 #> [145] 209464.6107 59834.4072 59184.0156 216546.8029 116536.3423 30769.3147 #> [151] 256500.4632 360.0762 29584.2031 105040.1378 147613.2800 64165.3479 #> [157] 54625.0882 129969.8995 129444.2817 89527.1695 29346.7155 59831.2370 #> [163] 131718.0636 17184.3692 111494.3756 26121.2074 110299.2482 150275.8879 #> [169] 155081.7890 248694.3258 82596.4382 178752.2762 120632.4422 160336.7883 #> [175] 188582.0985 256809.7711 79039.7961 234825.4847 125497.8940 144664.1989 #> [181] 11905.3421 248525.2401 94051.9950 86707.4403 164930.2405 120357.7244 #> [187] 70186.0722 917.3717 22583.2637 212427.8225 189144.6403 220913.5402 #> [193] 44800.7061 85876.2065 6827.9960 153196.5000 158648.2443 85097.9838 #> [199] 62768.1196 103624.9879 47914.2359 202573.0107 128782.4786 129606.2306 #> [205] 0.0000 88306.0428 188267.9088 72268.0681 73161.8896 127033.1921 #> [211] 25764.8973 41621.1360 153421.0500 5391.1155 36101.8036 86031.9193 #> [217] 55797.9868 52750.0244 145050.9537 119798.9456 210949.9530 117087.6744 #> [223] 25460.7624 13882.8896 168877.6027 204716.9542 11420.9159 128104.1438 #> [229] 253891.7857 226559.9250 267737.6577 68467.6159 56256.5625 123962.6979 #> [235] 61758.9782 159004.3839 121125.3115 105446.1158 229971.6007 188065.8785 #> [241] 102740.8931 165041.4939 80099.3119 135469.9079 40471.4573 8060.4021 #> [247] 245234.3659 15877.6277 25511.9525 253860.6732 91722.4151 56584.9250 #> [253] 21222.1502 173815.9999 8006.9737 61096.9263 93546.1608 110713.5543 #> [259] 0.0000 56141.0181 252192.3065 125334.1231 90098.4617 205625.4633 #> [265] 146107.0354 48259.5329 104444.7924 12542.4367 119694.3561 18418.1580 #> [271] 132523.0532 38308.0955 104486.8402 160918.8333 101040.9251 89053.4161 #> [277] 101719.4207 0.0000 202833.7864 115008.1535 147744.3721 170987.2731 #> [283] 114972.0337 0.0000 106183.6894 40459.8855 126799.6444 41556.9495 #> [289] 73746.4977 92093.8752 226716.7815 221746.6237 82363.7327 193013.2976 #> [295] 125335.5725 116347.3517 165345.9441 69096.8396 262380.3764 116362.6320 #> [301] 43133.4981 247422.3843 218877.7487 0.0000 132016.2742 51343.3099 #> [307] 268438.0461 206045.8475 145449.0331 50117.3714 0.0000 113833.8555 #> [313] 135649.7241 268182.4037 79039.4827 157099.8063 85864.5290 181113.4706 #> [319] 200166.2832 85493.1540 94605.3547 200227.3053 151259.9799 42828.2201 #> [325] 180489.5663 14238.1512 174633.3086 91621.6745 183284.8762 70354.5119 #> [331] 212375.6674 167598.5231 169502.6877 143409.1741 67435.4304 138484.6901 #> [337] 101275.3769 136299.2720 141326.6374 57988.2358 140063.8185 106146.5008 #> [343] 0.0000 38479.4089 131853.2122 92563.4679 59389.7339 85737.5882 #> [349] 112438.9722 0.0000 91513.3051 78007.3085 194817.8348 63781.3157 #> [355] 237552.3883 27847.1009 166605.8383 117661.1344 123751.2354 41880.8635 #> [361] 51106.5684 52442.5788 71412.4593 149649.9288 232840.8114 36598.6129 #> [367] 120891.1655 22976.0636 74439.5500 71842.4909 14893.8252 82022.2116 #> [373] 59358.1105 18446.6738 33497.9916 128875.6713 69852.7931 15869.8902 #> [379] 248542.2720 4613.3388 86186.3444 0.0000 241370.2525 153326.6082 #> [385] 116611.6698 163595.6404 156034.2227 178716.0787 80245.1768 118850.1183 #> [391] 53068.3675 19695.0617 56147.0650 204504.3640 91619.7682 155056.0691 #> [397] 62207.6863 115399.8868 85952.9441 96767.0904 155248.8581 122580.8119 #> [403] 139714.9934 106068.7114 54202.5496 34863.8528 236187.5604 252882.9743 #> [409] 58987.0946 155020.5503 224122.6205 268639.0827 132267.8111 220229.2342 #> [415] 123073.1510 65037.2521 23536.1431 83173.3508 19606.8194 213308.3777 #> [421] 33688.5801 109064.0115 47910.1029 122640.0121 57694.9949 105040.0796 #> [427] 104793.4584 71909.3942 59021.2335 182339.9348 43592.1426 207338.6776 #> [433] 72204.9107 55870.0378 87238.9588 16819.3599 10096.1811 21319.5677 #> [439] 42786.0544 1250.7512 77431.2402 256251.4978 108837.7718 164174.5949 #> [445] 134979.8613 8480.1995 74076.4515 112225.0614 95530.3963 162783.3410 #> [451] 24596.6906 238211.6410 236026.7200 12808.5145 92411.0392 98588.5193 #> [457] 166378.2594 103668.0571 26705.9921 34637.2193 137745.1952 8629.4301 #> [463] 96982.7077 115334.2662 151014.4870 176011.9609 113080.9271 101347.7487 #> [469] 37106.1669 52014.7670 131691.4006 39261.0266 119387.9609 199299.3828 #> [475] 0.0000 145316.9447 92548.4173 27018.3209 178219.2035 75222.7778 #> [481] 194479.1724 11711.3778 194100.6802 93687.2867 27518.6673 4751.6146 #> [487] 0.0000 172902.4040 108552.7507 123218.9700 53465.6232 213551.1590 #> [493] 0.0000 30593.0443 20993.6337 24762.1586 0.0000 112571.1779 #> [499] 53152.0645 32730.4936 26308.3662 139244.6613 125050.2764 69228.0278 #> [505] 140239.9184 27460.4661 110056.4660 91541.9049 108182.7357 106244.7498 #> [511] 96487.4938 0.0000 168725.1951 1550.0615 210334.9311 42427.4942 #> [517] 152704.7593 52314.8166 126625.1577 222776.2505 131799.3338 30119.9938 #> [523] 118709.1590 117297.0877 196738.2219 87452.2956 2620.0020 74061.3544 #> [529] 71638.8654 65027.2150 121429.6369 1133.6236 102005.0519 0.0000 #> [535] 98411.8483 31445.0010 173854.3855 157302.8723 24141.8586 79690.5761 #> [541] 12970.8025 87179.5689 154754.4627 200649.1275 224070.5966 64494.7787 #> [547] 39956.0710 46038.1854 150573.3957 124194.6931 127724.8512 224147.6673 #> [553] 97582.1685 85460.5958 175592.7755 91356.0578 117842.1475 29023.4945 #> [559] 111245.9117 152515.1575 187191.4530 268486.8009 177757.5913 146812.7451 #> [565] 100530.4263 205581.2641 24236.2140 120478.7936 204014.3931 93127.1783 #> [571] 140193.1231 85879.8284 217421.6224 103035.9014 132724.8062 16426.1715 #> [577] 31767.9854 108636.2610 17831.7044 77321.8046 100274.8683 208235.6672 #> [583] 18342.7078 100184.4776 3770.4246 140155.7834 156275.1494 119089.2750 #> [589] 81243.6545 190031.6735 91989.9637 23493.8037 50177.9503 102069.7882 #> [595] 187589.8031 134748.3833 129542.2021 48211.5691 0.0000 133244.2610 #> [601] 136095.0328 123265.5040 160713.0983 37291.1352 70214.7860 0.0000 #> [607] 54907.5763 39828.0265 162982.1345 81360.7014 271072.7643 167731.1864 #> [613] 71805.2663 241656.5279 93959.3011 29993.2646 151586.0982 53650.4816 #> [619] 50311.4211 32829.5992 46851.3698 58460.3416 107275.2494 149903.8361 #> [625] 34842.7977 107469.4342 139664.5403 58105.2297 122633.6542 108221.0975 #> [631] 252036.6656 45526.2271 138888.9775 37836.4981 90240.1354 105579.8747 #> [637] 87694.7626 243015.2364 151757.6636 94217.6628 78900.6044 116751.8865 #> [643] 35826.8470 162015.8448 181331.1224 210749.0228 32568.3678 72138.7058 #> [649] 165908.6307 59181.2237 51575.0717 117559.9360 0.0000 160274.7143 #> [655] 32782.5590 38697.3458 88891.3261 0.0000 147230.4430 0.0000 #> [661] 192459.1751 30254.8346 137016.8006 45154.2320 186911.7694 209454.2396 #> [667] 188502.0146 144243.2872 162916.3337 163621.9001 59398.8966 19802.6719 #> [673] 122031.6297 133172.2326 121395.3113 167895.1734 0.0000 74217.8395 #> [679] 121905.4653 159080.3318 173259.7958 0.0000 83605.8409 93931.0252 #> [685] 128790.7175 5796.6492 74983.8033 193439.6419 151233.7979 159146.9737 #> [691] 3108.9251 183097.6270 154603.0880 63110.2204 85970.4737 129811.0919 #> [697] 105688.8957 153477.9652 111213.1554 128834.3535 55187.5670 110372.5424 #> [703] 97504.4558 141735.4921 108436.0702 68570.0289 79958.9099 96608.2120 #> [709] 69231.1025 0.0000 79263.6266 47353.0186 126182.0500 51781.6847 #> [715] 262253.4790 67726.4091 257033.8687 144432.7463 82547.5104 176287.9511 #> [721] 66198.4903 41063.4358 159134.5518 62609.1711 190236.0335 13395.4357 #> [727] 84222.2296 136232.8953 148563.5351 175575.8708 14078.5503 218972.4954 #> [733] 226847.3313 115046.6855 23415.0592 57304.1361 0.0000 200335.2814 #> [739] 180345.7054 131290.8802 61970.2459 117851.5791 112430.5495 111443.5838 #> [745] 129782.5177 250716.5014 158152.9890 71373.8735 26761.3655 91682.9653 #> [751] 207524.5326 85665.2172 13964.7610 266460.3720 91988.6118 22351.1595 #> [757] 69591.6790 225940.7808 55700.8413 137415.3119 48051.1294 208543.5558 #> [763] 151436.0050 167206.2578 35168.3808 187049.6925 196994.5227 152111.1910 #> [769] 243050.7355 155870.0411 0.0000 226132.5614 83119.8496 226877.3195 #> [775] 33115.8568 59012.8483 164044.5914 15962.7723 43030.0701 0.0000 #> [781] 188050.4610 85475.4450 49967.7874 140118.6298 169984.4443 142213.5341 #> [787] 122479.4252 49207.9992 140897.9777 217751.7445 96958.0707 58194.6825 #> [793] 143969.8530 51177.8366 98353.9795 111676.7165 168024.4521 5025.8140 #> [799] 64499.2919 124854.4268 122722.8869 243932.3036 162520.3547 118272.9378 #> [805] 103726.3787 229106.4205 43953.9469 57816.1930 120235.7159 165238.7819 #> [811] 19675.3117 182552.3050 92502.4751 88558.8988 208922.7552 37409.4896 #> [817] 107365.4448 98480.1035 101659.2594 170408.7782 161091.4776 11304.9978 #> [823] 51185.5892 4454.2694 63972.9243 8198.0265 105923.1141 60770.0364 #> [829] 196664.7491 170318.5748 240162.3186 28500.2808 45938.8712 84521.7599 #> [835] 93869.9731 30604.8963 91718.8384 86647.2267 163329.3584 68137.3573 #> [841] 122630.6888 166231.3869 112667.3651 81048.4801 183383.5495 3951.5266 #> [847] 99810.1641 75846.4387 134341.4792 125108.9242 91684.9610 144705.9877 #> [853] 196977.4344 124550.8824 85467.4563 167150.3858 138314.9154 127275.4247 #> [859] 60463.5662 45385.2267 255859.4970 47814.6486 167436.6643 254849.7684 #> [865] 135150.9675 77151.5769 71410.3716 199922.7971 180652.9967 136830.6033 #> [871] 95684.8360 9067.4328 94166.6380 218267.7869 0.0000 146448.4913 #> [877] 45734.5275 168919.1233 60938.7279 73887.0122 50388.3361 47312.4091 #> [883] 146434.9390 0.0000 55784.7692 51286.3472 98707.2780 208494.6743 #> [889] 71067.3500 142471.3606 134140.0023 68271.6555 0.0000 102529.7115 #> [895] 48664.1783 59042.4093 5841.1077 76361.5705 42589.0683 117562.1241 #> [901] 127259.8200 24873.8857 18214.9120 96732.6626 207469.4431 175696.9072 #> [907] 80391.5299 113577.0249 56769.0336 73292.2518 192270.9830 209445.7484 #> [913] 203475.5241 46617.6130 74499.3222 25242.9951 56670.4174 113893.0704 #> [919] 59451.4255 133398.7665 92618.3019 1593.2864 48349.7030 3632.3010 #> [925] 227706.0971 174450.4631 133525.2122 160593.5497 73708.7667 31415.7076 #> [931] 30462.2210 195941.7160 127009.7160 71658.6703 93372.5326 195573.8583 #> [937] 12460.6261 249585.1623 108445.4612 217430.9484 242507.8163 69593.5267 #> [943] 26301.8122 102654.0740 15430.4594 41488.7108 203171.7308 26071.9769 #> [949] 146800.9501 9493.0625 2510.2478 96921.5675 72435.0088 57537.9133 #> [955] 43735.7621 2066.1238 24045.3908 179036.2615 41204.9595 171949.8373 #> [961] 174903.6584 0.0000 190328.6628 8850.2280 66480.2310 81988.4181 #> [967] 118565.0806 166027.9861 111089.8437 64626.3039 13707.7565 64547.1923 #> [973] 51147.0213 48117.8731 119779.0045 113872.9191 107155.0437 0.0000 #> [979] 73160.9494 109990.6447 98446.8570 56340.8535 111164.7912 210686.2669 #> [985] 81757.5320 158669.3463 121780.5505 155953.4985 41881.8971 66934.3912 #> [991] 16488.9649 156444.0492 83413.1413 0.0000 4205.0763 215521.0954 #> [997] 137786.7392 160113.0101 231842.9946 43595.3353 # or add to ncpts ncpts$dist <- distance_to(ncpts, ncsub, measure = 'geodesic')