[R-sig-Geo] polygon neighbor matrix

Roger Bivand Roger.Bivand at nhh.no
Mon Aug 31 20:29:09 CEST 2015


On Mon, 31 Aug 2015, boB Rudis wrote:

> I second the `gTouches` suggestion. There's a ref here:
> http://stackoverflow.com/a/26499134/1457051 but the gist of it is
> (this user needed adjacent polygons in ghana):

If this answered the question, it would be OK. But it doesn't, as reading 
the link provided to the FlexScan documentation showed (not a matrix, 
but a ragged right-edged text array).

Using gTouches can work, especially if the returnDense=FALSE argument is 
used (ie. don't return a matrix), but can fail if snapping is needed. 
spdep::poly2nb is the preferred route, because it makes snapping possible, 
and because the nb S3 class is much more flexible than matrices.

Roger

>
> library(rgeos)
> library(rgdal)
>
> # using http://data.biogeo.ucdavis.edu/data/diva/adm/GHA_adm.zip
> ghana <- readOGR("GHA_adm", "GHA_adm1")
>
> gTouches(ghana, byid=TRUE)
>
> ##       0     1     2     3     4     5     6     7     8     9
> ## 0 FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE
> ## 1  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE
> ## 2  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE
> ## 3  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
> ## 4 FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE
> ## 5 FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE
> ## 6 FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE
> ## 7 FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE
> ## 8 FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE
> ## 9  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>
> On Mon, Aug 31, 2015 at 2:19 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>> On Mon, 31 Aug 2015, Paul Lantos wrote:
>>
>>> Hi,
>>>
>>> I'm trying to use the cluster analysis program Flexscan, which I gather is
>>> similar to Satscan but it computes irregular clusters. This program requires
>>> a polygon adjacency matrix (termed a 'matrix definition file'). The example
>>> they provide has a row for every polygon, and for each row every neighboring
>>> polygon is listed in successive columns. So for instance if row 1 / polygon
>>> 1 has 3 neighbors there will be three polygons listed in columns 2-4.
>>>
>>> I have tried some techniques like the spatial weights matrix tools in
>>> ArcGIS and in Spatstat, but I cannot get the output in this kind of format.
>>> Was wondering if there is some obvious way to do this in R.
>>>
>>> The documentation for Flexscan is here:
>>>
>>>
>>> https://sites.google.com/site/flexscansoftware/download_e/FleXScan%20User%20Guide_e31.pdf?attredirects=0&d=1
>>
>>
>> Something like:
>>
>> library(spdep)
>> example(nc.sids)
>> out <- sapply(seq(along=ncCR85_nb), function(i) paste(attr(ncCR85_nb,
>>   "region.id")[i], paste(attr(ncCR85_nb, "region.id")[ncCR85_nb[[i]]],
>>   collapse=" ")))
>> writeLines(out, tf)
>> file.show(tf)
>>
>> See also:
>>
>> vignette("nb")
>>
>> Roger
>>
>>>
>>> Thanks for any help,
>>> Paul
>>>
>>> _______________________________________
>>> Paul M. Lantos, MD, FIDSA, FAAP, FACP
>>> Departments of Internal Medicine and Pediatrics
>>>  Pediatric Infectious Diseases
>>>  Hospital Medicine Program
>>> Duke University School of Medicine
>>> _______________________________________
>>>
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>> --
>> Roger Bivand
>> Department of Economics, Norwegian School of Economics,
>> Helleveien 30, N-5045 Bergen, Norway.
>> voice: +47 55 95 93 55; fax +47 55 95 91 00
>> e-mail: Roger.Bivand at nhh.no
>>
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

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



More information about the R-sig-Geo mailing list