查找相邻的多边形R [英] Find neighbouring polygons R

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

问题描述

我有一张表,其中包含+ 500k行,其坐标为 x y (按 shapeid 分组(总共289个id)),并形成一个多边形

I have one table containing +500k rows with coordinates x, y grouped by shapeid (289 ids in total) and forming a polygon.

shapeid      x           y
1            679400.3   6600354
1            679367.9   6600348
1            679313.3   6600340
1            679259.5   6600331
1            679087.5   6600201
0            661116.3   6606615
0            661171.5   6606604
0            661182.7   6606605
0            661198.9   6606606
0            661205.9   6606605
...          ...        ...

我想找到相交或彼此最接近的坐标,本质上是为每个shapeid找到物理邻居.

I want to find the coordinates which intersects or lies closest to each other, in essence finding the physical neighbours for each shapeid.

结果应类似于:

shapeid shapeid_neighbour1   shapeid_neighbour2

所以我尝试像这样使用sp和rgeos:

So I tried using sp and rgeos like so:

library(sp)
library(rgeos)

mydata <- read.delim('d:/temp/testfile.txt', header=T, sep=",")

sp.mydata <- mydata
coordinates(sp.mydata) <- ~x+y

我上课时,一切看起来都很好:

When I run class, everything looks fine:

class(sp.mydata)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

我现在尝试计算每个点的距离:

I now try calculating the distance by each point:

d <- gDistance(sp.mydata, byid=T)

R Studio遇到致命错误.有什么想法吗?然后我的计划是使用:

R Studio encounters fatal error. Any ideas? My plan is then to use:

min.d <- apply(d, 1, function(x) order(x, decreasing=F)[2])

查找第二个最短距离,即最近的点.但这也许不是执行我想要的最佳方法-为每个Shapeid找到物理邻居吗?

To find the second shortest distance, i.e. the closest point. But maybe this isn't the best approach to do what I want - finding the physical neighbours for each shapeid?

推荐答案

假设数据帧中​​的每个 shapeid 均标识多边形的顶点,则需要首先创建一个 SpatialPolygons 对象,然后应用功能 gDistance 来了解任意一对多边形之间的距离(假设这就是您要查找的).为了创建 SpatialPolygons ,您需要一个 Polygons ,然后一个 Polygon 对象.您可以在 Polygon 下的 sp 包的帮助页面中找到详细信息.

Assuming that each shapeid of your dataframe identifies the vertices of a polygon, you need first to create a SpatialPolygons object from the coordinates and then apply the function gDistance to know the distance between any pair of polygons (assuming that is what you are looking for). In order to create a SpatialPolygons you need a Polygons and in turn a Polygon object. You can find details in the help page of the sp package under Polygon.

您很快就会发现一个问题:每个多边形的坐标必须关闭,即每个Shapeid的最后一个顶点必须与第一个顶点相同.从您的数据来看,对于您而言似乎并非如此.因此,您应该手动"为数据的每个子集添加一行.

You might find soon a problem: the coordinates of each polygons must close, i.e. the last vertex must be the same as the first for each shapeid. As far as I can see from your data, that seems not to be the case for you. So you should "manually" add a row for each subset of your data.

您可以尝试执行此操作(假设 df 是您的起始数据帧):

You can try this (assuming that df is your starting dataframe):

    require(rgeos)
    #split the dataframe for each shapeid and coerce to matrix
    coordlist<-lapply(split(df[,2:3],df$shapeid),as.matrix)
    #apply the following command only if the polygons don't close
    #coordlist<-lapply(coordilist, function(x) rbind(x,x[1,]))
    #create a SpatialPolygons for each shapeid
    SPList<-lapply(coordlist,function(x) SpatialPolygons(list(Polygons(list(Polygon(x)),1))))
    #initialize a matrix of distances
    distances<-matrix(0,ncol=length(SPList),nrow=length(SPList))
    #calculate the distances
    for (i in 1:(length(SPList)-1))
      for (j in (i+1):length(SPList))
        distances[i,j]<-gDistance(SPList[[i]],SPList[[j]])

这可能需要一些时间,因为您要计算289 * 288/2多边形的距离.最终,您将获得距离矩阵.

This may require some time, since you are calculating 289*288/2 polygons distances. Eventually, you'll obtain a matrix of distances.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆