Home > Blockchain >  Calculate distance with multiple starting coordinates
Calculate distance with multiple starting coordinates

Time:06-24

I calculated the travelled distance called distance_m per date with the function points_in_circle for two ID's called HEX_Tag_ID. The coordinates that I put in the function are based on the Datum "2022-04-21" of the ID "3D6.15341BBB4F". However, the remaining ID has a different set of coordinates at the date "2022-04-21". That means that computations are wrongfully executed for "3D6.15341BC60F".

How can I automatise this function in order to calculate the travelled distance with the corresponding coordinates? especially if I want to add more ID's in the future. Key points: coordinates are based on Datum "2022-04-21" of every unique `HEX_Tag_ID.

library(spatialrisk)
Distance_ind <- points_in_circle(Distance_ind, lat_center = 51.93349, lon_center = 4.70912, lon = Longitude, lat = Latitude, radius = 1e6)

df = Distance_ind

structure(list(Datum_Tijd = structure(c(1650535073, 1653637570, 
1653285342, 1654242563, 1654578739, 1654837567, 1653899310, 1655108033, 
1653900853, 1653286920, 1653639081, 1654838913, 1653032146, 1655110494, 
1654580434, 1654244601, 1650533634), tzone = "", class = c("POSIXct", 
"POSIXt")), Datum = structure(c(19103, 19139, 19135, 19146, 19150, 
19153, 19142, 19156, 19142, 19135, 19139, 19153, 19132, 19156, 
19150, 19146, 19103), class = "Date"), Tijd = c("11:57:53", "09:46:10", 
"07:55:42", "09:49:23", "07:12:19", "07:06:07", "10:28:30", "10:13:53", 
"10:54:13", "08:22:00", "10:11:21", "07:28:33", "09:35:46", "10:54:54", 
"07:40:34", "10:23:21", "11:33:54"), Reader_ID = c("A0", "A0", 
"A0", "A0", "A0", "A0", "A0", "A0", "A0", "A0", "A0", "A0", "A0", 
"A0", "A0", "A0", "A0"), HEX_Tag_ID = c("3D6.15341BBB4F", "3D6.15341BBB4F", 
"3D6.15341BBB4F", "3D6.15341BBB4F", "3D6.15341BBB4F", "3D6.15341BBB4F", 
"3D6.15341BBB4F", "3D6.15341BBB4F", "3D6.15341BC60F", "3D6.15341BC60F", 
"3D6.15341BC60F", "3D6.15341BC60F", "3D6.15341BC60F", "3D6.15341BC60F", 
"3D6.15341BC60F", "3D6.15341BC60F", "3D6.15341BC60F"), Longitude = c(4.70912, 
4.70917, 4.70918, 4.70918, 4.70914, 4.70914, 4.70927, 4.70921, 
4.70904, 4.70903, 4.70906, 4.709, 4.70901, 4.70903, 4.70902, 
4.70902, 4.70925), Latitude = c(51.933491, 51.934189, 51.9342, 
51.934269, 51.93428, 51.934292, 51.934341, 51.934441, 51.932499, 
51.932491, 51.932468, 51.932369, 51.93235, 51.932339, 51.932331, 
51.932308, 51.934891), x = c(108366.11, 108370.273, 108370.972, 
108371.043, 108368.304, 108368.317, 108377.308, 108373.285, 108359.578, 
108358.882, 108360.921, 108356.692, 108357.36, 108358.724, 108358.028, 
108358.004, 108376.503), y = c(438553.585, 438631.208, 438632.425, 
438640.102, 438641.351, 438642.686, 438648.054, 438659.218, 438443.273, 
438442.39, 438439.812, 438428.836, 438426.716, 438425.479, 438424.595, 
438422.037, 438709.256), `Lengte_(cm)` = c(9.7, 9.7, 9.7, 9.7, 
9.7, 9.7, 9.7, 9.7, 11.2, 11.2, 11.2, 11.2, 11.2, 11.2, 11.2, 
11.2, 11.2), Geslacht = c("vrouw", "vrouw", "vrouw", "vrouw", 
"vrouw", "vrouw", "vrouw", "vrouw", "vrouw", "vrouw", "vrouw", 
"vrouw", "vrouw", "vrouw", "vrouw", "vrouw", "vrouw"), Sloot = c("22", 
"22", "22", "22", "22", "22", "22", "22", "22", "22", "22", "22", 
"22", "22", "22", "22", "22"), Lengte_8e_lichting = c(NA, NA, 
NA, NA, NA, NA, NA, 10.3, NA, NA, NA, NA, NA, 12.5, NA, NA, NA
), Lengteklasse = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("6", "7", "8", 
"9", "10", "11", "12", "13"), class = "factor"), distance_m = c(0.111319490512219, 
77.8879654012696, 79.1440538153195, 86.8156131287763, 87.953110772786, 
89.2887843834125, 95.2906913528804, 106.044905287657, 110.454187269549, 
111.379609951795, 113.843032832766, 125.060674080157, 127.12861901825, 
128.277561285803, 129.201736097579, 131.75853920649, 156.213638336975
)), row.names = c(NA, -17L), class = c("tbl_df", "tbl", "data.frame"
))

CodePudding user response:

Here is a data.table geosphere approach... It uses the distGeo to calculate distance from the first coordinate-pair (i.e. c(Longitude[1], Latitude[1])) of each HEX_Tag_ID with the current Longitude-Latitude of the row (matrix(c(Longitude, Latitude), ncol = 2)).

library(data.table)
library(geosphere)
# set to data.table format, sort by ID and Datumtijd
setDT(mydata, key = c("HEX_Tag_ID", "Datum_Tijd"))
# calculate distance
mydata[, distance := distGeo(
    c(Longitude[1], Latitude[1]), 
    matrix(c(Longitude, Latitude), ncol = 2)
  ),
  by = .(HEX_Tag_ID)]
  • Related