# R/Converting Geographic Coordinates to Kilometers

(Difference between revisions)
 Revision as of 21:54, 17 October 2012 (view source)Eli (Talk | contribs) (Converting Lat/Long to km) Revision as of 21:55, 17 October 2012 (view source)Eli (Talk | contribs) mNewer edit → 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.

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