# R/Converting Geographic Coordinates to Kilometers

From QERM Wiki

(Difference between revisions)

## Revision as of 21:54, 17 October 2012

This is a little piece of code I always have to go look for in my computer for converting geographic coordinates to kilometers. It takes a vector of longitudes (Long) and latitudes (Lat) and returns a projection of those points (x.km and y.km) around their mean in kilometers.

x0 <- mean(range(Long)) y0 <- mean(range(Lat)) rx <- 6371 * acos(sin(y0*pi/180)^2 + cos(y0*pi/180)^2 * cos((x0+.5)*pi/180 - (x0-.5)*pi/180)) ry <- 6371 * acos(sin((y0-.5)*pi/180) * sin((y0+.5)*pi/180) + cos((y0-.5)*pi/180) * cos((y0+.5)*pi/180)) x.km <-(Long-x0)*rx y.km <-(Lat-y0)*ry

Note that there are, probably, a dozen little functions that do this in different geographic packages on-line. This does a good enough job. The 6371 is the 'mean' radius of the earth, which ranges from 6357 at the poles to 6378 at the equator. You could presumably make things more accurate by modeling the radius as a function of latitude, but are unlikely to improve your precision by more than (6378 - 6357)/6378 = 0.3%.