Calculating weighted polygon centroids in R

前端 未结 4 1471
小鲜肉
小鲜肉 2021-01-02 11:55

I need to calculate the centroids of a set of spatial zones based on a separate population grid dataset. Grateful for a steer on how to achieve this for the example below.<

4条回答
  •  旧巷少年郎
    2021-01-02 12:29

    Another alternative.

    I like it for its compactness, but it will likely only make sense if you're fairly familiar with the full family of raster functions:

    ## Convert polygons to a raster layer
    z <- rasterize(polys, dat)
    
    ## Compute weighted x and y coordinates within each rasterized region
    xx <- zonal(init(dat, v="x")*dat, z) / zonal(dat,z)
    yy <- zonal(init(dat, v="y")*dat, z) / zonal(dat,z)
    
    ## Combine results in a matrix
    res <- cbind(xx[,2],yy[,2])
    head(res)
    #          [,1]     [,2]
    # [1,] 8.816277 14.35309
    # [2,] 8.327463 14.02354
    # [3,] 8.993655 13.82518
    # [4,] 8.467312 13.71929
    # [5,] 9.011808 13.28719
    # [6,] 9.745000 13.47444
    

提交回复
热议问题