# 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:

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)

> 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

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

```--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of