calculating distance using latitude & longitude - r

I am calculating the distance covered by bike riders. I have the latitude and longitude info for their starting point and end point. I looked up earlier solutions but using distHaversine with Geosphere package did not work for me. Adding that code here as well.
DF <- data.frame(Start.Lat = c(40.74178, 40.72132, 40.79948, 40.75020, 40.71413, 40.74312, 40.72904, 40.74780, 40.69779, 40.72955), Start.Lon = c(-74.00150, -74.01006, -73.95561, -73.99093, -73.95234, -73.98215, -73.99405, -73.97344, -73.97374, -73.98057), End.Lat =c(40.74174, 40.70380, 40.79377, 40.76133, 40.71335, 40.76096, 40.71894, 40.73705, 40.75110, 40.73493), End.Lon = c(-73.99416, -74.00839, -73.97189, -73.97982, -73.94910, -73.96724, -73.99266 , -73.99009, -73.94074, -73.99201))
Code where I tried to use geosphere:
install.packages("geosphere")
library(geosphere)
distm(cbind(DF$Start.Lon, DF$Start.Lat), cbind(DF$End.Lon, DF$End.Lat), fun = distHaversine)
Error message: (only get this error when I use my full dataset consisting of 30,000 data points)
Error: cannot allocate vector of size 6.7 Gb

Related

Calculating minimum and maximum distance between points in meters, R

I have a data frame with two columns including coordinates in meters (about 45.000 locations).
What I want to do is to calculate the minimum and maximum distances between the locations. I have tried to calculate the minimum distance as follow:
library(sf)
xco<-c(320963.6,421813.6,315423.6,405733.6,365603.6)
yco<-c(172137.7,165287.7,232197.7,138917.7,183697.7)
mydata<-data.frame(xco,yco)
mydata_sf<-st_as_sf(mydata, coords = c("coords.x1", "coords.x2"), crs = 2100)
dist_df<-as.data.frame(st_distance(mydata_sf))
min(dist_df[dist_df> 0])
However, that gives me a value which I can not see in my data.
Can anyone suggest a faster and better way to do that?
Thank you!
You have an error in your code. The fifth line should be
mydata_sf <- st_as_sf(mydata, coords = c("xco", "yco"), crs = 2100)
Then
dist_df <- as.data.frame(st_distance(mydata_sf))
Gives you a labeled distance matrix. You just need the upper or lower triangle:
range(dist_df[lower.tri(dist_df)])
# [1] 30885.97 129834.72
The first value is the minimum distance and the second is the maximum.

Calculating the distance between points and the closest raster cell of a certain value in R

I am currently trying to calculate the distance a set of points and their closes raster cell of a certain value. So far I have tried to convert the raster file into Points, but I keep getting an error message:
Cannot allot Vector of this size. Is there any other way how I can go around this. My datasets are very large (20.000 Points and a raster layer of an entire country).
so far I have tried:
library(raster)
water_points <- rasterToPoints(land_cover , fun = function(x) {x == 405}) #405 are cells that contain water
then I would continue like this:
df$water_dist <- gDistance(df, water_points)
I have also tried to use rastertoPolygons but it seems to show the same problem
Thank you very much

Computing the Great Circle Distance in R

In the past, in MATLAB, I've calculated the index of a point given its lat and lon using a great circle distance calculation. I'll share my code with you. I'm fairly stumped as to what the equivalent function in R would look like or whether on exists? I've found some code that shows the distance between two points, but none that help me to index my data.
This is my MATLAB code!
%% Define latlon grid and coordinates (lon follows lat)
lon_grid = transpose([40.1 40.12 40.14; 40.3 40.32 40.34; 40.5 40.52 40.54]);
lat_grid = transpose([30 30.2 30.4;30.02 30.22 30.42; 30.04 30.24 30.44]);
coord = [30.4125 40.4043];
%% Compute great circle distance
dist = distance('gc',coord(1),coord(2),lat_grid,lon_grid);
%% Retrieve index of minimum distance
[value,array_index] = min(distance(:));
[i,j] = ind2sub(size(dist),array_index);
The "dist" calculation is sort of the party piece here. You should be able to use the provided code to reproduce the results and see what I'm hoping to achieve in R.
Again, what might a comparable function in R be given I have the following:
A grid of latitude points
A grid of longitude points
Two points, in degrees, for the latitude and longitude of my position.
maybe this works:
library(geoshpere)
dist<-apply(coord, 1, FUN=function(p) distHaversine(p, lonlat_matrix))

Using distm function in R to calculate distance between two coordinates gives a different answer than when calculating the same thing in excel

I'm trying to calculate the distance between two coordinates (40.777250, -73.872610) and (40.6895, -74.1745).
Using distm in R gives the following result:
> distm (c(40.777250, -73.872610), c(40.6895, -74.1745), fun = distHaversine)
33713.61
When I use Excel to calculate the distance with the following function
6378134 * ACOS(COS(RADIANS(90-40.777250)) *COS(RADIANS(90-40.6895)) +SIN(RADIANS(90-40.777250)) *SIN(RADIANS(90-40.6895)) *COS(RADIANS(-73.872610-(-74.1745))))
the answer is 27274.49199.
I'm wondering why these two methods give a different answer and whether I'm doing something wrong. I tried an online coordinate distance calculator and it gives the same answer as my excel function.
You have to change the order of longitude and latitude , because (see vignette):
Geographic locations must be specified in longitude and latitude (and
in that order!)
result <- distm (c(-73.872610,40.777250), c(-74.1745, 40.6895), fun = distHaversine)
# [,1]
# [1,] 27274.5
or:
distHaversine(c(-73.872610,40.777250), c(-74.1745, 40.6895))
# [1] 27274.5

Get summary vectors of raster cell centers in R

I want to extract summary vectors that contain the coordinates for the centers of the different cells in a raster. The following code works but I believe involves an n-squared comparison operation. Is there a more efficient method? Not seeing anything obvious in {raster}'s guidance.
require(raster)
r = raster(volcano)
pts = rasterToPoints(r)
x_centroids = unique(pts[,1])
y_centroids = unique(pts[,2])
To get the centers of the raster cells, you should use the functions xFromCol, yFromRow and friends (see also the help pages)
In this case, you get exactly the same result as follows:
require(raster)
r <- raster(volcano)
x_centers <- xFromCol(r)
y_centers <- yFromRow(r)
Note that these functions actually don't do much else but check the minimum value of the coordinates and the resolution of the raster. From these two values, they calculate the sequence of centers as follows:
xmin(r) + (seq_len(ncol(r)) - 0.5) * xres(r)
ymin(r) + (seq_len(nrow(r)) - 0.5) * xres(r)
But you better use the functions mentioned above, as these do a bit more safety checks.

Resources