简体   繁体   English

R用于计算多边形的面积和质心

[英]R for compute the area and centroid of polygons

Create functions to compute the area and centroid of a polygon list (as in the format of georgia.polys). 创建函数来计算多边形列表的面积和质心(格式为georgia.polys)。 The formula for the area of a polygon is 多边形区域的公式是

在此输入图像描述

where A is the polygon area, xi is the i th x-coordinate of the polygon boundary (x[i] in R), yi is the i th y-coordinate of the polygon boundary (y[i] in R) - and n is the number of points used to specify the polygon boundary. 其中A是多边形区域,xi是多边形边界的第i个x坐标(R中的x [i]),yi是多边形边界的第i个y坐标(R中的y [i]) - 和n是用于指定多边形边界的点数。 The polygon is assumed to be in closed form so that the xi and yi take the same value as xn and yn. 假设多边形是闭合形式,因此xi和yi取与xn和yn相同的值。 The centroid has coordinates (Cx, Cy) where : 质心有坐标(Cx,Cy),其中:

在此输入图像描述

Here is the code that already created, but im not sure the centroid coordinate is correct 这是已经创建的代码,但我不确定质心坐标是否正确

library(GISTools)
data("georgia")


polyn<-function(x){

  poly.df<-data.frame()

  for(d in 1:159){
    poly.d<-x[[d]]
    n<-length(poly.d[,1])

    i<-1
    A.sum<-0
    C.xsum<-0
    C.ysum<-0

    while(i<n){

      A.area<-0.5*(poly.d[i,2]*poly.d[i+1,1]-poly.d[i+1,2]*poly.d[i,1])
      A.sum<-A.sum+A.area

      C.x<-(1/(6*A.sum))*(poly.d[i,2]+poly.d[i+1,2])*(poly.d[i,2]*poly.d[i+1,1]-poly.d[i+1,2]*poly.d[i,1])
      C.xsum<-C.xsum+C.x

      C.y<-(1/(6*A.sum))*(poly.d[i,1]+poly.d[i+1,1])*(poly.d[i,2]*poly.d[i+1,1]-poly.d[i+1,2]*poly.d[i,1])
      C.ysum<-C.ysum+C.y

      i<-i+1
    }

    poly.df<-rbind(poly.df, c(A.sum,C.xsum,C.ysum))
    colnames(poly.df) <- c("Area", "Cx", "Cy")
  }

  poly.df

}

polyn(georgia.polys)

This is some result of that function, 这是该功能的一些结果,

          Area           Cx            Cy
1   1326077000    4044403.4    4855396.03 
2    891511462   -2237689.5   -2962558.41 
3    740601936   10709355.7   12996988.27 

Is there anyone can help me with the code? 有没有人可以帮我代码?

The area A.sum in C.ysum and C.xsum should be the total area but not an area that depends on your iterator i . C.ysumA.sum中的C.ysum C.xsum应该是总面积,而不是依赖于迭代器i The easiest way is the put the division after the calculation of area. 最简单的方法是在计算面积后放置除法。

Also, the equations should loop over the indices 1,2,...,n+1 , with the last vertex the same as the first vertex. 此外,方程应循环索引1,2,...,n+1 ,最后一个顶点与第一个顶点相同。 Therefore, you should also modify your code so that it loops over the last case in the summations of the equations. 因此,您还应该修改代码,使其循环遍历方程式的求和中的最后一个案例。

....

while(i<n+1){

  j <- ifelse(i+1==n+1,1,i+1) # j=i+1 and j=1 for the last iteration

  A.area<-0.5*(poly.d[i,2]*poly.d[j,1]-poly.d[j,2]*poly.d[i,1])
  A.sum<-A.sum+A.area

  C.x<-(poly.d[i,2]+poly.d[j,2])*(poly.d[i,2]*poly.d[j,1]-poly.d[j,2]*poly.d[i,1])
  C.xsum<-C.xsum+C.x

  C.y<-(poly.d[i,1]+poly.d[j,1])*(poly.d[i,2]*poly.d[j,1]-poly.d[j,2]*poly.d[i,1])
  C.ysum<-C.ysum+C.y

  i<-i+1
}

C.ysum<-C.ysum/(6*A.sum)
C.xsum<-C.xsum/(6*A.sum)
....

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

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