[R-sig-Geo] near neighbor using decimal degree or projected coord system

Roger Bivand Roger.Bivand at nhh.no
Mon Apr 23 10:53:23 CEST 2012


On Mon, 23 Apr 2012, Gabriele Cozzi wrote:

> Dear all,
> I am using knearneigh dnearneigh {spdep} to set neighbor distances for a SAR 
> analysis. I tried to used projected coord systems (UTM 34S, units: meters) 
> and geographic coordinate systems (in decimal degrees) for what I thought 
> should give the same result but I am somehow getting different results which 
> confuses me a bit. I guess sometimes double checking is not a good idea ;-)
>
> I set:
> coord<- as.matrix(cbind(X,Y))      # where X and Y is respectively in the 
> projected and in the geographic format.
>
> k_nb<-knn2nb(knearneigh(coord, k=1, longlat= LOGIC, RANN=TRUE))     # where 
> LOGIC=FALSE when X and Y are in the projected format and
>                                                                                                                                         LOGIC=TRUE 
> when X and Y are in the decimal degrees format

If longlat=TRUE, RANN is ignored, as it cannot handle non-planar 
coordinates.

>
> distance<-unlist(nbdists(k_nb, coords))

nbdists() also takes a longlat= argument - which you haven't used here. If 
you do, you'll get the distances you want. You can forget the argument if 
you make coord into a SpatialPoints object with a CRS:

library(spdep)
set.seed(1)
xy <- cbind(x=runif(50, 0, 10), y=runif(50, 0, 10))
k1 <- knn2nb(knearneigh(xy, k=1, longlat=TRUE, RANN=TRUE))
summary(unlist(nbdists(k1, xy)))
summary(unlist(nbdists(k1, xy, longlat=TRUE)))
SP <- SpatialPoints(xy, proj4string=CRS("+proj=longlat"))
k1 <- knn2nb(knearneigh(SP, k=1))
summary(unlist(nbdists(k1, SP)))

Hope this helps,

Roger

> summary(distance)            # to get the minimal distance to ensure all 
> locations have at least one neighbor.
>
> the summary output is as follow:
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>  14.87  257.30  398.10  415.30  550.10 1429.00    # where I use the 
> projected coord system
>
>   Min.             1st Qu.         Median      Mean           3rd Qu. 
> Max.
> 0.0001414 0.0023600 0.0036880 0.0038520  0.0051070  0.0135300   # where I use 
> the decimal degree coord system
>
> I have to slight problems with these results and I would appreciate if 
> someone could help
>
> 1) I thought that setting longlat=TRUE using decimal degrees would give 
> distances between neighbors in kilometers. However here I am two orders of 
> magnitude wrong. I know I could simply using longlat=FALSE that seems to give 
> the correct distances but I was nonetheless keen to understand why there are 
> differences.
>
> 2)  I see there are some slight differences in the distances. e.g. max = 1429 
> vs. max = 1353 (*10e-5). What type of transformation is used to pass from 
> decimal degrees to euclidean distances?
>
> Any comment would be appreciated.
>
> Best,
> Gabriele
>
>
>
>
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list