【问题标题】:How to compute greatest distance between polygon centroid and edge如何计算多边形质心和边缘之间的最大距离
【发布时间】:2015-06-05 07:37:16
【问题描述】:

我有一个 SpatialPolygons(DataFrame) 对象,例如SpP

library(sp)    
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)

我想为每个多边形计算其质心 (coordinates(SpP)) 与同一多边形内任何其他点之间的最大距离(我假设这个最远的点将在边缘上?)。

谁能教我怎么做?

【问题讨论】:

  • 最远的点总是一个顶点,所以计算所有的质心-顶点距离并取最大值。
  • @Spacedman 你介意给我一个函数吗?
  • 对不起,当我读到这篇文章时我很忙,我想我会纠正你关于这一点处于边缘的假设。认为这可能有助于您自己编写解决方案!

标签: r geospatial spatial sp


【解决方案1】:

这是我创建的一个简单函数,它为给定的多边形计算质心,并使用基本几何来查找离质心最远的点,并返回其坐标:

library(sp)
library(rgeos)
library(ggplot2)

Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Srs2=Polygons(list(Sr2), "s2")
spPol=SpatialPolygons(list(Srs2))

find_furthest_point=function(polygon){
  coords=fortify(polygon)[,c(1:2)]  
  center=as.data.frame(gCentroid(polygon))
  longs=coords[,1]
  lats=coords[,2]

  dist_fx=function(long, lat, center=center){
    dist=sqrt((long-center[1])^2+(lat-center[2])^2)
    return(dist)
  }
  dists=mapply(dist_fx, longs, lats, MoreArgs=list(center))
  furthest_index=as.integer(which(dists==max(dists)))
  furthest=coords[furthest_index,]  
  return(furthest)
}

find_furthest_point(Sr2)

【讨论】:

  • fortify 只是一种将数据从多边形提取到数据框中的方法
  • fortify(对于Polygons对象)来自ggplot2,之前library(ggplot2)也是如此,或者将函数编辑为ggplot2::fortify(.....)。此代码也适用于您问题中的SpatialPolygons* 对象,例如SpP
  • 不!坐标的平均值是 NOT 质心!使用sp 方法coordinates(x)rgeos::gCentroid
  • 感谢您的指点,@Spacedman!我隔开,正在考虑点顶点的质心,而不是多边形......
  • @qoheleth 这回答了你的问题吗?
猜你喜欢
  • 2018-07-04
  • 2021-12-28
  • 2020-03-28
  • 2017-05-28
  • 2017-02-21
  • 1970-01-01
  • 2017-11-24
  • 2021-01-30
相关资源
最近更新 更多