st_distance generates some radical distance values for some points
Hello Edzer,
I hope you are well. I am having some issues processing distances with my data.
I have two point layers,
- sampling_points <- st_read("..../site_data_all_gck.shp")
- weather_stations <- st_read("..../gcr_weather_stations.shp")
I want to know which weather station is the closest to my sampling points. For this purpose I used st_nearest and it effectively select the row in the weather station that is the closet to my sampling point.
However when I execute the code to get the distance between those two points, for most of the sampling points it works well, but it generates unrealistic distances for some others. Particularly in row 33 of the output file
"sampling_point_nearest_weather_station"
the distance between the "sampling point code" "SF" and the "nearest weather" station "82144" is:
14362.9406 m
but the distance calculated manually using google and QGIS is ~2900m
The layers and the code to replicate the issue are available at
https://github.com/sogm/distance_points_issue
Thanks in advance for helping me figure out what could be happening.
Regards
SGM
You probably have a mistake in your code somewhere. I tested this simplified version and the result looks ok:
library(sf)
sampling_points = st_read("site_data_all_gck.shp")
weather_stations = st_read("gcr_weather_stations.shp")
idx = st_nearest_feature(sampling_points, weather_stations)
nearest = weather_stations[idx, ]
rownames(nearest) = seq_len(nrow(nearest))
sampling_points$dist = st_distance(sampling_points, nearest, by_element = TRUE)
sampling_points$station_id = nearest$field_1
sampling_points[sampling_points$field_1 == "SF", c("field_1", "dist", "station_id")]
#> field_1 dist station_id geometry
#> 33 SF 2897.709 [m] 82144 POINT (145.6225 -36.83866)
Hello kadyb,
Thanks for the input. I will check my script. It is interesting that in the iterative process it generates the "correct" value for most of the rows but it fails when it comes to this particular pair of points.
Regards