Hi, I have a set of locations defined by longitude and latitude (in degrees), and want to calculate the spatial (or geographic) distance among all locations. I did not find such a function in the spatial-related packages. (I *cannot* use 'dist', as I have geographic, not cartesian coordinates). thanks! Robert Robert Ptacnik Norwegian Institute for Water Research (NIVA) Gaustadall?en 21 NO-0349 Oslo mobile +47 982 277 81 FON NIVA +47 22 18 51 00 FAX +47 22 18 52 00 ---------------------------------------------------------------------------------------------------------------------- NIVAs hovedkontor har flyttet til nye lokaler i CIENS - Forskningssenter for milj? og samfunn; Gaustadall?en 21, 0349 Oslo. Meld deg p? v?rt nyhetsbrev p? www.niva.no
I don't know of such a function, but you have several options: 1. Assume that Euclidean distance is "good enough" - probably reasonable over a limited geographic region (and commonly done). 2. Use your GIS software to do the calculations since it already "understands" projections and distances on a sphere. 3. Write a function to do this yourself and make it available to other researchers who need this capability. Assuming that great circle distances on a perfect sphere are good enough, it isn't that hard to do. Eh, I talked myself into it (runs, but otherwise untested): geogdist <- function(lat1, lon1, lat2, lon2) { # takes latitude and longitue for points 1 and 2 # returns great circle distances in km # south and west are negative rad <- function(x) { (x/360) * 2 * pi } deg <- function(x) { (x/(2 * pi)) * 360 } gd <- (sin(rad(lat1)) * sin(rad(lat2))) + (cos(rad(lat1)) * cos(rad(lat2)) * cos(rad(abs(lon2-lon1)))) gd <- deg(acos(gd)) 111.23 * gd } On Oct 30, 2007 11:49 AM, <robert.ptacnik at niva.no> wrote:> Hi, > I have a set of locations defined by longitude and latitude (in degrees), > and want to calculate the spatial (or geographic) distance among all > locations. > I did not find such a function in the spatial-related packages. (I *cannot* > use 'dist', as I have geographic, not cartesian coordinates). > thanks! > Robert >-- Sarah Goslee http://www.functionaldiversity.org
Check out rdist.earth in the fields package. On Oct 30, 2007 11:49 AM, <robert.ptacnik at niva.no> wrote:> Hi, > I have a set of locations defined by longitude and latitude (in degrees), > and want to calculate the spatial (or geographic) distance among all > locations. > I did not find such a function in the spatial-related packages. (I *cannot* > use 'dist', as I have geographic, not cartesian coordinates). > thanks! > Robert > > > Robert Ptacnik > Norwegian Institute for Water Research (NIVA) > Gaustadall?en 21 > NO-0349 Oslo > mobile +47 982 277 81 > FON NIVA +47 22 18 51 00 > FAX +47 22 18 52 00 > > > > ---------------------------------------------------------------------------------------------------------------------- > NIVAs hovedkontor har flyttet til nye lokaler i CIENS - Forskningssenter > for milj? og samfunn; Gaustadall?en 21, 0349 Oslo. Meld deg p? v?rt > nyhetsbrev p? www.niva.no > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >