Extracting values from inside polygons raster r

三世轮回 提交于 2019-12-13 04:17:28

问题


I'm trying to find the mean daily temperature for counties in South Dakota from raster grids ('bil' files) found at http://prism.oregonstate.edu/. I am getting county boundaries from the 'maps' package.

library(maps)
library(raster)
sd_counties <- map('county','south dakota')
sd_raster <- raster('file_path')

How do I extract the grid cells within each county? I think I need to turn each county into it's own polygon to do this, but how? Then, I should be able to do something like the following. Any help would be greatly appreciated.

values <- extract(raster, list of polygons)  
polygon_means <- unlist(lapply(values, FUN=mean))

回答1:


I'm not familiar with the maps package or the map function, but it looks like it's solely for visualization, rather than geospatial operations.

While there might be a way to convert the map object to actual polygons, here's an easy way sing raster's getData function that works:

library(raster)

usa_adm2 <- getData(country='USA',level=2)

sd_counties <- usa_adm2[grepl('South Dakota',usa_adm2$NAME_1),]

plot(sd_counties)

Now you can extract pixels for each county using extract(r,sd_counties), where r is your desired raster.

Note, that depending on the number of pixels (and layers) you need to extract, that can take some time.



来源:https://stackoverflow.com/questions/54565713/extracting-values-from-inside-polygons-raster-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!