sf
请使用、terra
和库找到一种可能的解决exactextractr
方案dplyr
代表
library(sf)
library(terra)
library(exactextractr)
# Read the .shp file as a 'sf' object
shape_sf <- sf::st_read("GDL Shapefiles V4.shp")
# Read the raster as a 'Spatraster' object
Spatrast <- terra::rast("ras_light_1992.tif")
# Compute the mean value of the raster cells that intersect each multipolygon of the 'sf' object
# (this computation is weighted by the fraction of cells that are covered by the multipolygon).
mean_temp <- exactextractr::exact_extract(Spatrast, shape_sf, "mean", force_df = TRUE)
# To what the result (i.e. dataframe) looks like:
head(mean_temp)
#> mean
#> 1 0.164738640
#> 2 0.000000000
#> 3 0.053336937
#> 4 0.123192482
#> 5 0.011922332
#> 6 0.007129772
- 第 2 步:将生成
dataframe
的 'mean_temp' 添加到sf
对象 'shape_sf' 作为一个名为 'mean_temp' 的新列
names(mean_temp) <- "mean_temp"
shape_sf <- cbind(shape_sf, mean_temp)
# To what the 'sf' object looks like:
shape_sf
#> Simple feature collection with 1745 features and 7 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -180 ymin: -55.98403 xmax: 180 ymax: 83.11042
#> Geodetic CRS: WGS 84
#> First 10 features:
#> GDLcode constant iso_code country
#> 1 AFGr101 World AFG Afghanistan
#> 2 AFGr102 World AFG Afghanistan
#> 3 AFGr103 World AFG Afghanistan
#> 4 AFGr104 World AFG Afghanistan
#> 5 AFGr105 World AFG Afghanistan
#> 6 AFGr106 World AFG Afghanistan
#> 7 AFGr107 World AFG Afghanistan
#> 8 AFGr108 World AFG Afghanistan
#> 9 AGOr201 World AGO Angola
#> 10 AGOr202 World AGO Angola
#> region shdi mean_temp
#> 1 Central (Kabul Wardak Kapisa Logar Parwan Panjsher) 0.5739148 0.1647386402
#> 2 Central Highlands (Bamyan Daikundi) 0.4840758 0.0000000000
#> 3 East (Nangarhar Kunar Laghman Nooristan) 0.4776733 0.0533369370
#> 4 North (Samangan Sar-e-Pul Balkh Jawzjan Faryab) 0.5148334 0.1231924817
#> 5 North East (Baghlan Takhar Badakhshan Kunduz) 0.4603950 0.0119223315
#> 6 South (Uruzgan Helmand Zabul Nimroz Kandahar) 0.4241354 0.0071297721
#> 7 South East (Ghazni Paktya Paktika Khost) 0.4891695 0.0005833496
#> 8 West (Ghor Herat Badghis Farah) 0.4617800 0.0027360055
#> 9 Cabinda 0.6649382 1.9035018682
#> 10 Zaire 0.6010896 0.3108430207
#> geometry
#> 1 MULTIPOLYGON (((69.41367 33...
#> 2 MULTIPOLYGON (((65.32353 33...
#> 3 MULTIPOLYGON (((70.46561 33...
#> 4 MULTIPOLYGON (((66.38873 34...
#> 5 MULTIPOLYGON (((67.35538 34...
#> 6 MULTIPOLYGON (((60.89944 29...
#> 7 MULTIPOLYGON (((68.52644 31...
#> 8 MULTIPOLYGON (((61.12394 31...
#> 9 MULTIPOLYGON (((12.21127 -5...
#> 10 MULTIPOLYGON (((13.33589 -7...
# If you need a dataframe with only the names of countries and mean temperatures
Requested_df <- shape_sf %>%
dplyr::select(.,country, mean_temp) %>%
sf::st_drop_geometry() %>%
dplyr::group_by(country) %>%
dplyr::summarise(mean_temp = round(mean(mean_temp),2)) %>%
as.data.frame()
head(Requested_df)
#> country mean_temp
#> 1 Afghanistan 0.05
#> 2 Albania 0.34
#> 3 Algeria 2.01
#> 4 Andorra 15.62
#> 5 Angola 0.40
#> 6 Antigua and Barbuda 7.47
由reprex 包于 2022-01-13 创建(v2.0.1)
编辑基于@dbaston 评论
由于 Dan Baston 的评论,这里是更轻的版本。
library(sf)
library(terra)
library(exactextractr)
# Read the .shp file as a 'sf' object
shape_sf <- sf::st_read("GDL Shapefiles V4.shp")
# Read the raster as a 'Spatraster' object
Spatrast <- terra::rast("ras_light_1992.tif")
# Compute the mean value of the raster cells that intersect each multipolygon of the 'sf' object
# (this computation is weighted by the fraction of cells that are covered by the multipolygon).
# And include the 'country' column in the returned data frame.
mean_temp <- exactextractr::exact_extract(Spatrast, shape_sf, "mean", append_cols = "country")
# Rename 'mean' column into 'mean_temp' column
colnames(mean_temp)[2] <- "mean_temp"
# To what the result (i.e. dataframe) looks like:
head(mean_temp)
#> country mean_temp
#> 1 Afghanistan 0.164738640
#> 2 Afghanistan 0.000000000
#> 3 Afghanistan 0.053336937
#> 4 Afghanistan 0.123192482
#> 5 Afghanistan 0.011922332
#> 6 Afghanistan 0.007129772
# If you need a dataframe with only the names of countries and mean temperatures
Requested_df <- mean_temp %>%
dplyr::group_by(country) %>%
dplyr::summarise(mean_temp = round(mean(mean_temp),2)) %>%
as.data.frame()
head(Requested_df)
#> country mean_temp
#> 1 Afghanistan 0.05
#> 2 Albania 0.34
#> 3 Algeria 2.01
#> 4 Andorra 15.62
#> 5 Angola 0.40
#> 6 Antigua and Barbuda 7.47
由reprex 包于 2022-01-23 创建(v2.0.1)