[GRASS-stats] Nearest neighbour distance distribution but between polygons

Rainer M Krug r.m.krug at gmail.com
Tue May 8 06:31:49 EDT 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 07/05/12 18:30, Roger Bivand wrote:
> On Mon, 7 May 2012, Rainer M Krug wrote:
> 
> On 07/05/12 14:42, Rainer M Krug wrote:
>>>> On 07/05/12 14:25, Roger Bivand wrote:
>>>>> On Mon, 7 May 2012, Rainer M Krug wrote:
>>>> 
>>>>> Hi
>>>> 
>>>>> I have the following (presumibly simple?) question:
>>>> 
>>>>> I have a certain number of patches (islands) in a landscape of which I need to know
>>>>> the nearest neighbour distance. As the problem is related to dispersal, I would need to
>>>>> know the actual shortest distance between the actual polygons and not only the
>>>>> centroids. Is there an easy way to obtain these in R or in GRASS? I am only aware of
>>>>> nearest neighbour distance between points.
>>>> 
>>>> 
>>>> Hi Roger
>>>> 
>>>> 
>>>>>> In R, if the coordinates are projected, use gDistance(..., byid=TRUE) in the rgeos
>>>>>> package to get a matrix in the metric of the projection. If not projected, project if
>>>>>> possible.
>>>> 
>>>> Exactly what I was looking for - the geometries are projected, so absolutely no problem.
> 
> I thought that it would be no problem, but it is.
> 
> I am loading the layer from a shape file and it consists of 40 different polygons. Now I want
> to calculate the distance between all of the individual polygons in the layer, and I am stuck.
> 
> I understand the principle, but I a struggling at accessing each polygon in the 
> SpatialPolygonsDataFrame to calculate the differences. My code:
> 
> I am reading from the shapefile:
> 
> x <- readShapePoly( "shapefile" )
> 
> Now I want to do:
> 
> d <- matrix(ncol=length(x), nrow=length(x)) for (i in length(x):1) { cat (i, ": ") for (j in
> 1:i) { cat( j ) p_i <- ### Polygon i - How? p_j <- ### Polygon j - How? d[i, j] <-
> gDistance(p_i, p_j) } cat("\n") }
> 
> I tried x[i], x[[i]] and x at polygons..... but nothing worked so far - I am stuck here.
> 
>> p_i <- x[i,]

I think I understand it now - thanks.

> 
>> etc., x is a SpatialPolygonsDataFrame. I don't think that calculating the other triangle is a
>> problem.
> 
>> But isn't it easier to do:

Easier is always better...

> 
>> library(rgdal) dsn <- system.file("vectors", package = "rgdal")[1] scot_BNG <-
>> readOGR(dsn=dsn, layer="scot_BNG") is.projected(scot_BNG) library(rgeos) dmat <-
>> gDistance(scot_BNG, scot_BNG, byid=TRUE)
> 
>> # As of now, you need to include the geometries twice, will be fixed
> 
>> scot_BNG$d0 <- dmat[1,] scot_BNG$NAME[1] spplot(scot_BNG, "d0", col.regions=bpy.colors(20),
>> col="grey", sp.layout=list("sp.text", coordinates(scot_BNG), as.character(scot_BNG$NAME),
>> col="grey", cex=0.8))
> 

Great - thanks a lot.

Rainer


>> Roger
> 
> 
> Any help welcome,
> 
> Thanks,
> 
> Rainer
> 
> 
>>>> 
>>>> Thanks a lot,
>>>> 
>>>> Rainer
>>>> 
>>>> 
>>>>>> I don't think that the geosphere package provides for the unprojected case.
>>>> 
>>>>>> Roger
>>>> 
>>>> 
>>>>> Thanks,
>>>> 
>>>>> Rainer
>>>> 
>>>> 
>>>>>> _______________________________________________ grass-stats mailing list 
>>>>>> grass-stats at lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-stats
>>>>>> 
>>>> 
>>>> 
> 
>> 
> 

- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys.
(Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      Rainer at krugs.de

Skype:      RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+o9hQACgkQoYgNqgF2egqWMQCeJF6+qpXN3/L6n5aoxb0G+Wnd
AVAAniTflhEjjpoLfAU2qaZN+ZmFGNGW
=hqCa
-----END PGP SIGNATURE-----


More information about the grass-stats mailing list