# How I can calculate the distance between two points based on their longitudes and latitudes?

Hello,
I have a dataset as following :

``````         id      x.from   y.from   x.to    y.to
1   573346934   340391   510136  340361  510250
2   573347081   342842   508754  342797  508778
3   573347082   342646   508813  342797  508778
...     ...        ...      ...     ...     ...
``````

'x.from' and 'x.to' here are longitude, and 'y.from' and 'y.to' are latitude.

Thus, there is an 'origin point'(from) and 'destination point'(to) in each 'id'. I want to calculate the distance between two points of each ids.

I have major radius of the ellipsoid = 6378137, and flattening = 1/298.257222101.

All the examples using geospatial related functions give output as matrix, but what I have to get is not in that kind of form. I need a column that matches the values of each id's distance with the ids.

I would be very appreciate if you tell me how to solve this problem...

Note that we have two points on a sphere, not on a plane, so we are finding the distance along a great circle.

We need to convert latitude and longitude values from decimal degrees to radians by dividing by 180/pi, or 57.29577951.

We need the radius of the earth, which is 3963.0 miles (or 6378.8 km).
If point A is (long1, lat1) in decimals and point B is (long2, lat2) in decimals, then

d <- 3963.0 * acos((sin(lat1/57.29577951)*sin(lat2/57.29577951)) +
cos(lat1/57.29577951)*cos(lat2/57.29577951)*cos(long2/57.29577951 - long1/57.29577951))

Example:
long1= 2.3522
lat1=48.8566
long2=19.9450
lat2=50.0647

d = 793.4522

Thank you! And I have a question. Since the outcome was abnormal compare to my expectation. On the while I found that my data is based on Transverse Mercator coordinates system.
Does this matter to my value?

Hi. I googled Transverse Mercator coordinates. It looks like this system assumes the earth is an ellipsoid, not a sphere.

You can convert Transverse Mercator ellipsoid coordinates to the more common spherical coordinates ( Universal Transverse Mercator coordinate system - Wikipedia ) . but this looks awfully messy. Or, you can search for a formula to find the distance between two points on an ellipsoid.

By the way, if you use 6378.8 km as the first term instead of 3963.0 miles , then the result is in km.

If your coordinates are Transverse Mercator, then they are not lat longs, they are X-Y coordinates on a projected map. For small distances (less than a few hundred km ), just treating the coordinates as regular X-Y's should be reasonably accurate. I would recommend reading the Wikipedia page Map projection - Wikipedia to better understand the issues. To do geodetics correctly is not easy, there are many pitfalls.
BTW - UTM are not spherical coordinates, they are projections from some ellipsoidal model of the earth's surface (there are many) to a cylinder. I'm afraid the other answers you were given are far too simplistic. The earth is not a sphere.