【问题标题】:Calculte the whole center of gravity/geometric center of a polygon list计算多边形列表的整个重心/几何中心
【发布时间】:2014-12-19 19:07:06
【问题描述】:

我正在寻找一种方法来计算空间多边形列表中每个多边形的重心:

我以为使用了循环,但他让我获得了第一个多边形,我不知道路,我是 R 新手,有人可以帮我吗 代码:

for ( i in 1:length(polys1_T)) { 
  xx=mean(coordinates(polys1_T[[i]])[,1])
  yy=mean(coordinates(polys1_T[[i]])[,2])
  aa<-as.data.frame(cbind(xx,yy))
}

编辑:

代码:

 inter1 <- read.table("c:/inter1.csv", header=TRUE)

# add a category (required for later rasterizing/polygonizing)
inter1 <- cbind(inter1, 
                cat
                = rep(1L, nrow(inter1)), stringsAsFactors = FALSE)

# convert to spatial points
coordinates(inter1) <- ~long + lat

# gridify your set of points
gridded(inter1) <- TRUE

# convert to raster
r <- raster(inter1)

# convert raster to polygons
sp <- rasterToPolygons(r, dissolve = T)
plot(sp)
# addition transformation to distinguish well the set of polygons
polys <- slot(sp@polygons[[1]], "Polygons")
# plot
plot(sp, border = "gray", lwd = 2) # polygonize result

inter1.csv 结果:

Polys 是 9 个多边形的列表:是否可以计算每个多边形的重心?

【问题讨论】:

    标签: r polygon


    【解决方案1】:

    rgeos::gCentroid看看。您可以通过多种方式应用它。如果你有一个 SpatialPolygons 对象,比如说,来自对 readOGR 的调用,你可以这样做:

    map <- readOGR(dsn, layer)
    centers <- data.frame(gCentroid(map, byid=TRUE))
    

    从中获取所有质心。

    顺便说一句:虽然准确,但更常见的术语是“几何中心”/“质心”与“重心”

    编辑

    对于普通的,ol Polygons(“硬”的方式,但稍微更准确):

    library(rgdal)
    library(sp)
    library(PBSmapping)
    library(maptools)
    
    do.call("rbind", lapply(polys, function(x) {
      calcCentroid(SpatialPolygons2PolySet(SpatialPolygons(list(Polygons(list(x), ID=1)))))
    }))[,3:4]
    
    ##            X        Y
    ## 1  5.8108434 20.16466
    ## 2 -3.2619048 29.38095
    ## 3  5.5600000 34.72000
    ## 4  3.8000000 32.57037
    ## 5  6.3608108 32.49189
    ## 6 -2.2500000 31.60000
    ## 7 -8.1733333 27.61333
    ## 8  0.3082011 27.44444
    ## 9  8.6685714 26.78286
    

    并且,使用您几乎等效的手动方法:

    do.call("rbind", lapply(polys, function(x) {
      data.frame(mean(coordinates(x)[,1]), mean(coordinates(x)[,2]))  
    }))
    
    ##   mean.coordinates.x....1.. mean.coordinates.x....2..
    ## 1                  5.819892                  20.15484
    ## 2                 -3.242593                  29.37778
    ## 3                  5.539474                  34.71579
    ## 4                  3.815517                  32.56552
    ## 5                  6.323034                  32.47191
    ## 6                 -2.230952                  31.60000
    ## 7                 -8.140476                  27.61905
    ## 8                  0.350000                  27.40885
    ## 9                  8.746825                  26.92063
    

    每种方法都会为您提供每个列表元素的质心(在您提供的示例中,有 9 个,而不是 5 个)。

    如果您有大量此类列表,请考虑使用 data.table 包中的 rbindlist(速度更快 + 内存效率更高)。

    【讨论】:

    • 感谢您的回复,但我正在寻找每个“插槽坐标”对象列表的重心,也就是说在示例中我有 5 个插槽,所以我必须获得一个数据框5 观察 2 变量
    • 你能发布生成你的对象的代码吗?或者带有它的 RData 文件的链接。以这种方式向您展示会更容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 2014-06-28
    • 2022-12-22
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多