Re: [R] Converting coordinates to actual distances

From: Roger Bivand <Roger.Bivand_at_nhh.no>
Date: Thu 15 Sep 2005 - 07:05:38 EST


On Wed, 14 Sep 2005, Paul Brewin wrote:

> Hello,
>
> I've been searching for a method of converting Lat/Lon decimal
> coordinates into actual distances between points, and taking into
> account the curvature of the earth. Is there such a package in R? I've
> looked at the GeoR package, but this does not seem to contain what I am
> looking for. Ideally the output would be a triangular matrix of
> distances.
>

Using C code in the sp package (which will be exposed at the R level shortly) and sp > 0.8 and maptools > 0.5:

> library(maptools)

Loading required package: foreign
Loading required package: sp
> xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1])
> ll <- getSpPPolygonsLabptSlots(xx)
# ll is a matrix of long-lat centroids of North Carolina county polygons
> str(ll)

 num [1:100, 1:2] -81.5 -81.1 -79.3 -79.8 -78.7 ...
> plot(ll)
> x <- as.double(ll[,1])
> y <- as.double(ll[,2])
> n <- as.integer(length(x))
> dists <- vector(mode="double", length=n)
> lonlat <- as.integer(1)
> res <- matrix(as.double(NA), 100, 100)
> for (i in 1:100) res[i,] <- .C("sp_dists", x, y, x[i], y[i], n, dists,
+ lonlat)[[6]]

gives a full matrix measured in kilometers for the WGS-84 ellipsoid. Accessing the C function like this puts the responsibility for checking the argument modes on the user.

If this seems scary, rdist.earth() in the fields package has an R version of this. But maybe you need the actual functions to use great circle distance instead of Euclidean, rather than just to generate a distance matrix?

Hope this helps,

Roger Bivand

> Thanks in advance,
> Paul Brewin
>
>
>
> Paul E Brewin (PhD)
> Center for Research in Biological Systems
> University of California San Diego
> 9500 Gilman Drive MC 0505
> La Jolla CA, 92093-0505
> USA
>
> Ph: 858-822-0871
> Fax: 858-822-3631
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand@nhh.no

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed Sep 21 01:33:55 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 17:02:26 EST