# R/Converting Geographic Coordinates to Kilometers

From QERM Wiki

(Difference between revisions)

(Converting Lat/Long to km) |
m |
||

Line 1: | Line 1: | ||

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. | 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. | ||

<pre> | <pre> | ||

− | + | lon0 <- mean(range(Long)) | |

− | + | lat0 <- mean(range(Lat)) | |

− | rx <- 6371 * acos(sin( | + | rx <- 6371 * acos(sin(lat0 *pi/180)^2 + cos(lat0*pi/180)^2 * cos((lon0+.5)*pi/180 - (lon0-.5)*pi/180)) |

− | ry <- 6371 * acos(sin(( | + | ry <- 6371 * acos(sin((lat0 -.5)*pi/180) * sin((lat0+.5)*pi/180) + cos((lat0-.5)*pi/180) * cos((lat0+.5)*pi/180)) |

− | x.km <-(Long- | + | x.km <-(Long-lon0)*rx |

− | y.km <-(Lat- | + | y.km <-(Lat-lat0)*ry |

</pre> | </pre> | ||

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%. | 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%. | ||

[[Category: R tips]] | [[Category: R tips]] |

## Revision as of 21:55, 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.

lon0 <- mean(range(Long)) lat0 <- mean(range(Lat)) rx <- 6371 * acos(sin(lat0 *pi/180)^2 + cos(lat0*pi/180)^2 * cos((lon0+.5)*pi/180 - (lon0-.5)*pi/180)) ry <- 6371 * acos(sin((lat0 -.5)*pi/180) * sin((lat0+.5)*pi/180) + cos((lat0-.5)*pi/180) * cos((lat0+.5)*pi/180)) x.km <-(Long-lon0)*rx y.km <-(Lat-lat0)*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%.