Finding adjacent polygons in R (neighbors)

后端 未结 2 1526
难免孤独
难免孤独 2020-12-23 17:39

I\'m starting with a SpatialPolygonsDataFrame which has the data to create a map of the districts of Ghana (available at http://www.diva-gis.org/datadown). I\'m trying to c

2条回答
  •  爱一瞬间的悲伤
    2020-12-23 18:04

    I think you're looking for gTouches:

    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 a quick glance, it looks right:

    enter image description here

    I'm not sure which Ghana administrative district file you are using, so that was a guess and those are in polygon order, so you'll need to poke at ghana@data and map the entries to admin district names.

提交回复
热议问题