点要素到R中最近的多边形的距离 [英] Distance of point feature to nearest polygon in R

查看:152
本文介绍了点要素到R中最近的多边形的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我在一个项目中工作,这里有一个点要素-该点要素包括142个点-和多个多边形(大约10个).我想计算R中每个点与最近的多边形要素之间的距离.

I working on a project at the moment, where I have a point feature -- the point feature includes a 142 points -- and multiple polygon (around 10). I want to calculate the distance between every single point and the nearest polygon feature in R.

我目前的方法很乏味,有点冗长.我目前正计划计算每个单点和每个多边形之间的距离.例如,我将计算142个点与多边形A之间的距离,142个点与多边形B之间的距离,142个点与多边形C之间的距离,等等.这是这些距离计算之一的示例代码:

My current approach is tedious and a bit long winded. I am currently planning to calculate the distance between every single point and every single polygon. For example, I would calculate the distance between the 142 points and Polygon A, the distance between the 142 points and Polygon B, the distance between 142 points and Polygon C, etc. Here is a sample code of one of these distance calculations:

dist_cen_polya <- dist2Line(centroids_coor, polygonA_shp)

完成这些计算后,我将编写代码以选择每个单点与最近的多边形之间的最小/最近距离.问题在于此过程很繁琐.

After doing these calculations, I would write a code to choose the minimum/nearest distance between every single point and the closest polygon. The issue is that this procedure is tedious.

有人知道一个可以最大程度地减少计算工作量/计算时间的软件包/代码吗?我真的很想使用一个软件包,将单个点与最近的多边形要素进行比较,或者计算一个点与所有感兴趣的多边形之间的距离?

Does anyone know a package/code which would minimize the effort/computational time of the calculation? I would really like to use a package that compare a single to point to the nearest polygon feature or calculates the distance between a point and all polygons of interest?

谢谢.

推荐答案

在这里,我正在使用 rgeos拓扑库中的gDistance 函数.我正在使用强力双循环,但速度出奇的快. 142个点和10个多边形所需的时间不到2秒.我敢肯定,还有一种更精致的方法可以执行循环.

Here I am using the gDistance function in the rgeos topology library. I am using a brute force double loop but it is surprisingly fast. It takes less than 2 seconds for 142 points and 10 polygons. I am sure that there is a more elgant way to perform the looping.

   require(rgeos)

    # CREATE SOME DATA USING meuse DATASET
    data(meuse)
      coordinates(meuse) <- ~x+y
        pts <- meuse[sample(1:dim(meuse)[1],142),]  
    data(meuse.grid) 
      coordinates(meuse.grid) = c("x", "y") 
        gridded(meuse.grid) <- TRUE 
          meuse.grid[["idist"]] = 1 - meuse.grid[["dist"]]    
        polys <- as(meuse.grid, "SpatialPolygonsDataFrame")
          polys <- polys[sample(1:dim(polys)[1],10),]   
    plot(polys)
      plot(pts,pch=19,cex=1.25,add=TRUE)      

    # LOOP USING gDistance, DISTANCES STORED IN LIST OBJECT
    Fdist <- list()
      for(i in 1:dim(pts)[1]) {
        pDist <- vector()
          for(j in 1:dim(polys)[1]) { 
            pDist <- append(pDist, gDistance(pts[i,],polys[j,])) 
          }
        Fdist[[i]] <- pDist
      } 

    # RETURN POLYGON (NUMBER) WITH THE SMALLEST DISTANCE FOR EACH POINT  
    ( min.dist <- unlist(lapply(Fdist, FUN=function(x) which(x == min(x))[1])) ) 

    # RETURN DISTANCE TO NEAREST POLYGON
    ( PolyDist <- unlist(lapply(Fdist, FUN=function(x) min(x)[1])) ) 

    # CREATE POLYGON-ID AND MINIMUM DISTANCE COLUMNS IN POINT FEATURE CLASS
    pts@data <- data.frame(pts@data, PolyID=min.dist, PDist=PolyDist)

    # PLOT RESULTS
    require(classInt)
    ( cuts <- classIntervals(pts@data$PDist, 10, style="quantile") )
       plotclr <- colorRampPalette(c("cyan", "yellow", "red"))( 20 )
         colcode <- findColours(cuts, plotclr)
    plot(polys,col="black")
      plot(pts, col=colcode, pch=19, add=TRUE)

min.dist向量表示多边形的行号.例如,您可以这样使用向量来对最近的多边形进行子集化.

The min.dist vector represents the row number of the polygon. For instance you could subset the nearest polygons by using this vector as such.

near.polys <- polys[unique(min.dist),]

PolyDist向量包含以要素的投影单位表示的实际笛卡尔最小距离.

The PolyDist vector contain the actual Cartesian minimum distances in the projection units of the features.

这篇关于点要素到R中最近的多边形的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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