Reta
Reta

Reputation: 383

Find the coordinates of mid point of 4 coordinates in R

First thanks for all the help I get from all of you on my previous questions.

I tried to look for a function that can find the centre coordinates for my dataset below:

df <- read.table(sep=",", col.names=c("Start_Latitude","Start_Longitude","End_Latitude", "End_Longitude"),text="43.9567343,  -78.8571382, 43.9399364, -78.8497342")

Start_Latitude    Start_Longitude   End_Latitude    End_Longitude      
  43.9567343       -78.8571382       43.9399364      -78.8497342

I tried this code but it only calculates the distance from the starting coordinates to the ending coordinates, and I want to find the mid point coordinates not only the distance. I mean I need to find the long and lat of the mid point.

#dd the distance as column in the dataframe
df1$dist <- distm(x = df[, c('Start_Longitude', 'Start_Longitude')], 
                y = df[, c('End_Longitude', 'End_Latitude')],
                fun = distHaversine
)

And I had this dataset:

 Start_Latitude    Start_Longitude   End_Latitude    End_Longitude       dist
      43.9567343       -78.8571382       43.9399364      -78.8497342     13669708

Is there anyway to find the lat and the long of the mid point? Also, how can I calculate the distance in kilometre.

Thank you in advance!

Upvotes: 0

Views: 1327

Answers (2)

Len Greski
Len Greski

Reputation: 10855

One can calculate the midpoints by averaging the lat / long combinations. Using data from the original post, we use dplyr::mutate() to calculate the midpoint.

textFile <- "Start_Latitude    Start_Longitude   End_Latitude    End_Longitude      
  43.9567343       -78.8571382       43.9399364      -78.8497342
41.947460   -87.654730   41.890620   -87.624480"

data <- read.table(text=textFile,header=TRUE)

library(dplyr)
data %>% rowwise(.) %>% 
    mutate(midpoint_lat = sum(Start_Latitude,End_Latitude)/2,
           midpoint_long = sum(Start_Longitude,End_Longitude)/2) %>%
    as.data.frame(.) # print as df, not tibble

...and the output:

  Start_Latitude Start_Longitude End_Latitude End_Longitude id midpoint_lat
1       43.95673       -78.85714     43.93994     -78.84973  1     43.94834
2       41.94746       -87.65473     41.89062     -87.62448  2     41.91904
  midpoint_long
1     -78.85344
2     -87.63961

Calculating distance in kilometres

As explained in the help for the distHaversine() function from the geosphere package, the unit of measure in the result is returned based on the argument given for the radius of the earth. The default value is r = 6378137 metres, so the unit of measure is returned in meters.

To convert to kilometres, one has two choices:

  1. Divide the result by 1,000 (as illustrated in the other answer to this question), or
  2. Change the argument r to 6378.137

Upvotes: 2

Ian Campbell
Ian Campbell

Reputation: 24790

Here's an approach with geosphere::midPoint:

library(geosphere)
data.frame(df,
           dist = distHaversine(df[, c('Start_Longitude', 'Start_Latitude')],
                                df[, c('End_Longitude', 'End_Latitude')]) / 1000, 
           midPoint(p1 = df[, c('Start_Longitude', 'Start_Latitude')],
                    p2 = df[, c('End_Longitude', 'End_Latitude')]))
#  Start_Latitude Start_Longitude End_Latitude End_Longitude    dist       lon      lat
#1       43.95673       -78.85714     43.93994     -78.84973 1.96183 -78.85344 43.94834

Upvotes: 2

Related Questions