简体   繁体   English

查找相邻的多边形R

[英]Find neighbouring polygons R

I have one table containing +500k rows with coordinates x , y grouped by shapeid (289 ids in total) and forming a polygon. 我有一张表,其中包含+ 500k行,其中的xy坐标由shapeid分组(总共289个id),并形成一个多边形。

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
...          ...        ...

I want to find the coordinates which intersects or lies closest to each other, in essence finding the physical neighbours for each shapeid . 我想找到相交或彼此最接近的坐标, 从本质上来说是找到每个Shapeid的物理邻居

The results should look something like: 结果应类似于:

shapeid shapeid_neighbour1   shapeid_neighbour2

So I tried using sp and rgeos like so: 所以我尝试像这样使用sp和rgeos:

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 encounters fatal error. R Studio遇到致命错误。 Any ideas? 有任何想法吗? My plan is then to use: 我的计划是使用:

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

To find the second shortest distance, ie the closest point. 查找第二最短距离,即最近的点。 But maybe this isn't the best approach to do what I want - finding the physical neighbours for each shapeid? 但这也许不是执行我想要的最佳方法-为每个Shapeid找到物理邻居吗?

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). 假设数据shapeid的每个shapeid都可以标识多边形的顶点,则需要首先根据坐标创建一个SpatialPolygons对象,然后应用gDistance函数了解任意一对多边形之间的距离(假设这就是您要查找的) 。 In order to create a SpatialPolygons you need a Polygons and in turn a Polygon object. 为了创建SpatialPolygons您需要一个Polygons ,然后一个Polygon对象。 You can find details in the help page of the sp package under Polygon . 您可以在sp包的帮助页面Polygon下找到详细信息。

You might find soon a problem: the coordinates of each polygons must close, ie the last vertex must be the same as the first for each shapeid. 您可能很快就会发现一个问题:每个多边形的坐标必须关闭,即每个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. 因此,您应该“手动”为数据的每个子集添加一行。

You can try this (assuming that df is your starting dataframe): 您可以尝试以下操作(假设df是您的起始数据帧):

    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]])

This may require some time, since you are calculating 289*288/2 polygons distances. 这可能需要一些时间,因为您要计算289 * 288/2多边形的距离。 Eventually, you'll obtain a matrix of distances. 最终,您将获得距离矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM