Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating distance between two GPS locations in a data frame using distm () in R

Tags:

r

geosphere

This question has been asked previously but never with the following arrangement of the data. Below is a sample of it:

> head(datagps)
   Date & Time [Local]  Latitude Longitude
1: 2018-06-18 03:01:00 -2.434901  34.85359
2: 2018-06-18 03:06:00 -2.434598  34.85387
3: 2018-06-18 03:08:00 -2.434726  34.85382
4: 2018-06-18 03:12:00 -2.434816  34.85371
5: 2018-06-18 03:16:00 -2.434613  34.85372
6: 2018-06-18 03:20:00 -2.434511  34.85376

As you can see, I've a Date & Time [Local] column where GPS positions are registered every 4 min on average. I would like to calculate the distance (in meters) between two consecutive recordings and store this measure in a new column Step. I've been trying to implement distm() to my data:

> datagps$Step<-distm(c(datagps$Longitude, datagps$Latitude), c(datagps$Longitude+1, datagps$Latitude+1), fun = distHaversine)
Error in .pointsToMatrix(x) : Wrong length for a vector, should be 2

Although I'm very unsure about the syntax and if this is the right way to fill the arguments of the function. I'm very new to R so I hope I can get some help.

Any input is appreciated!

like image 953
juansalix Avatar asked Mar 18 '19 08:03

juansalix


People also ask

How do you find the distance between two points on a GPS?

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.

How do you find the distance between two points in a 2D array?

This length can be computed with the help of Pythagora's theorem: dist = sqrt((x2-x1)^2 + (y2-y1)^2) . This is known as the Euclidian distance between the points.

How do you find the distance between two locations in react native?

Normal distance calculation can be done using getDistance() function which calculates the distance between two geo coordinates. This function takes up to 3 arguments. The first 2 arguments are mandatory.


3 Answers

I think you're almost there already. Assuming you want store the distance between the previous recording (n) and the current recording (n+1) at n+1, you can use:

library(geosphere)
date <- c("2018-06-18 03:01.00","2018-06-18 03:06.00","2018-06-18 03:08.00","2018-06-18 03:12.00","2018-06-18 03:16.00","2018-06-18 03:20.00")
latitude <- c(-2.434901,-2.434598,-2.434726,-2.434816,-2.434613,-2.434511)  
longitude <- c(34.85359,34.85387,34.85382,34.85371,34.85372,34.85376)
datagps <- data.frame(date,lat,lon)

datagps$length <- distm(x=datagps[,2:3], fun = distHaversine)[,1]

That gives the first result 0, the rest as the distance between the consecutive points

like image 189
CIAndrews Avatar answered Sep 18 '22 04:09

CIAndrews


If you take a look at the function's documentation you'll see:

library(geosphere)
?distm

x longitude/latitude of point(s). Can be a vector of two numbers, a matrix of 2 columns (first one is longitude, second is latitude) or a SpatialPoints* object

y Same as x. If missing, y is the same as x

This means that you can use both a matrix or a vector.

One approach could be:

res <- distm(as.matrix(df1[,c("Longitude","Latitude")]), fun = distHaversine)

res

#         [,1]     [,2]     [,3]     [,4]     [,5]     [,6]
#[1,]  0.00000 45.90731 32.15371 16.36018 35.16947 47.35305
#[2,] 45.90731  0.00000 15.29559 30.09289 16.76621 15.60347
#[3,] 32.15371 15.29559  0.00000 15.81292 16.79079 24.84658
#[4,] 16.36018 30.09289 15.81292  0.00000 22.62521 34.40483
#[5,] 35.16947 16.76621 16.79079 22.62521  0.00000 12.19500
#[6,] 47.35305 15.60347 24.84658 34.40483 12.19500  0.00000
like image 24
patL Avatar answered Sep 22 '22 04:09

patL


solution using sf-package

sample data

library(data.table)
dt1 <- data.table::fread( 'DateTime, Latitude, Longitude
2018-06-18 03:01:00, -2.434901,  34.85359
2018-06-18 03:06:00, -2.434598,  34.85387
2018-06-18 03:08:00, -2.434726,  34.85382
2018-06-18 03:12:00, -2.434816,  34.85371
2018-06-18 03:16:00, -2.434613,  34.85372
2018-06-18 03:20:00, -2.434511,  34.85376')

setDF(dt1)

code

library(sf)
#create spatial points object
dt1.sf <- st_as_sf( x= dt1, 
                    coords = c("Longitude", "Latitude"),
                    crs = "+proj=longlat +datum=WGS84")
#calculate distances
st_distance(dt1.sf)

output

# Units: [m]
#          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]
# [1,]  0.00000 45.74224 32.07520 16.32379 34.97450 47.08749
# [2,] 45.74224  0.00000 15.20702 29.96245 16.76520 15.56348
# [3,] 32.07520 15.20702  0.00000 15.77068 16.72801 24.69270
# [4,] 16.32379 29.96245 15.77068  0.00000 22.47452 34.18116
# [5,] 34.97450 16.76520 16.72801 22.47452  0.00000 12.12446
# [6,] 47.08749 15.56348 24.69270 34.18116 12.12446  0.00000
like image 45
Wimpel Avatar answered Sep 21 '22 04:09

Wimpel