本文介绍了在R(相邻)中查找相邻的多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先从SpatialPolygonsDataFrame开始,该数据具有创建加纳地区地图的数据(可在 http://www.diva-gis.org/datadown ).我正在尝试创建一个矩阵,其中区域的名称为行和列名称,内部为0s/1s,以指示两个区域是否相邻(相邻).

我在spdep中发现了一些看似有希望的函数,但是我不知道如何为此目的使用它们.我能够使用poly2nb用数据创建一个"nb"文件,但是不确定如何从此处继续,即使我走对了路.

非常感谢您的帮助!谢谢!

解决方案

我认为您正在寻找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

乍一看,它看起来是正确的:

我不确定您使用的是哪个加纳行政区文件,这只是一个猜测,而这些文件是按多边形顺序排列的,因此您需要拨动ghana@data并将条目映射到管理区名称. /p>

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 create a matrix with the names of the districts as row and column names and 0s/1s in the interior to indicate if two districts are adjacent (neighboring) or not.

I've found several functions in spdep that seem promising, but I can't figure out how to use them for this purpose. I was able to create a "nb" file with the data using poly2nb, but am unsure how to proceed from here or even if I'm on the right track.

I'd really appreciate any help! Thank you!

解决方案

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:

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.

这篇关于在R(相邻)中查找相邻的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:43