简体   繁体   English

如何遍历 R 中单个 shapefile 中的多个重叠多边形以获取每个多边形的区域统计信息?

[英]How can I iterate through multiple overlapping polygons in a single shapefile in R to get zonal statistics of each polygon?

I want to use R to loop through multiple polygons in one shapefile and obtain a zonal statistic (mean) for each polygon from a single raster.我想使用 R 循环遍历一个 shapefile 中的多个多边形,并从单个栅格中获取每个多边形的区域统计(平均值)。 The polygons are buffered points of villages (about 84 buffers), and the raster the Hansen GFW data.多边形是村庄的缓冲点(大约 84 个缓冲区),栅格是 Hansen GFW 数据。

I've imported spatialEco and used zonal.stats to obtain some statistics and also extract, but I want to make sure that these are correct and there is no data loss due to the overlap, so creating an iteration of some variety will enable me to be sure that the statistics are representative of each polygon.我已经导入了 spatialEco 并使用 zonal.stats 来获取一些统计信息并进行提取,但我想确保这些是正确的并且没有由于重叠而导致的数据丢失,因此创建某种类型的迭代将使我能够确保统计数据代表每个多边形。

ex <- extract(hansen_forest,village_buffer,fun='mean',na.rm=TRUE,df=TRUE,weights=TRUE)

zs <- zonal.stats(village_buffer, hansen_forest, trace = TRUE, stat = MEAN)

Iterating over polygons in a shapefile isn't too hard, First: lets load in a shapefile which contains all polygons:在 shapefile 中迭代多边形并不难,首先:让我们加载一个包含所有多边形的 shapefile:

# Sample shp, with multiple polygons (218)
shp = readOGR('C:\\SHP\\ecoregions.shp')

As we can see this shapefile has 218 polygons (features)正如我们所见,这个 shapefile 有 218 个多边形(特征)

class       : SpatialPolygonsDataFrame 
features    : 218 
extent      : -140.9994, -52.36458, 41.67354, 83.63315  (xmin, xmax, ymin, ymax)

To loop over every polygon, we can make a for loop:要遍历每个多边形,我们可以创建一个 for 循环:

for(i in 1:nrow(shp)) { 
polygon <- shp[i,] 
# Do zonal statistics here

}

This will go through every single polygon in the shapefile, Hopefully this helps!这将通过 shapefile 中的每个多边形 go,希望这会有所帮助!

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

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