如何计算多边形质心和边之间的最大距离
How to compute greatest distance between polygon centroid and edge
我有一个 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)
) 与同一多边形内任何其他点之间的最大距离(我假设这个最远的点将在边缘上?) .
谁能告诉我怎么做?
这是我创建的一个简单函数,它针对给定的多边形计算质心,并使用基本几何来查找距离质心最远的点,并返回其坐标:
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)
我有一个 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)
) 与同一多边形内任何其他点之间的最大距离(我假设这个最远的点将在边缘上?) .
谁能告诉我怎么做?
这是我创建的一个简单函数,它针对给定的多边形计算质心,并使用基本几何来查找距离质心最远的点,并返回其坐标:
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)