[英]How to conduct spatial data analysis with netCDF land use data and Key Biodiversity Areas (.shp) data
I have a.netCDF file ("SSP119.nc") containing information on land-use change from 2015 to 2100, inclusive (source: https://luh.umd.edu/data.shtml ).我有一个 .netCDF 文件(“SSP119.nc”),其中包含 2015 年到 2100 年的土地利用变化信息,包括在内(来源: https://luh.umd.edu/data.shtml )。 I also have a shapefile containing polygons of global Key Biodiversity Areas (KBA).
我还有一个包含全球关键生物多样性区域 (KBA) 多边形的 shapefile。 I want to know how much land area overlaps with the KBA polygons for certain land-use types in certain years (yep, it's complicated... but I hope my code makes it easier to understand).
我想知道在某些年份有多少土地面积与某些土地利用类型的 KBA 多边形重叠(是的,这很复杂……但我希望我的代码能让它更容易理解)。
As an aside, the land-use data contains information for 14 land-use types.另外,土地利用数据包含 14 种土地利用类型的信息。 The resolution is at 0.25 x 0.25 degree grid-cells.
分辨率为 0.25 x 0.25 度网格单元。 Each grid-cell contains the fraction of each land-use type within it, eg, if one land-use type covers 60% of the grid-cell, its value will be 0.6.
每个网格单元包含其中每种土地利用类型的分数,例如,如果一种土地利用类型覆盖网格单元的 60%,则其值为 0.6。
Here is my code, so far:到目前为止,这是我的代码:
SSP119_r_primf <- stack("SSP119.nc", varname = "primf") # this creates a raster stack of the
# land-use "primary forested area" from the netCDF file
SSP119_r_primf
`class : RasterStack
dimensions : 720, 1440, 1036800, 86 (nrow, ncol, ncell, nlayers)
resolution : 0.25, 0.25 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
names : X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, ...
years since 2015-01-01 0:0:0: 0 - 85 (range)
`
proj4string(SSP119_r_primf) = CRS("+init=EPSG:4326")
plot(SSP119_r_primf) # will produce 86 plots of primary forested areas,
# annually between 2015 and 2100
primf_2100 <- subset(SSP119_r_primf, 86) # creates a subset of SSP119_r_primf
# specifically for the year 2100
plot(primf_2100) # to give you a visual idea of the data
[Primary forested land in year 2100](https://i.stack.imgur.com/TsVo7.png)
KBA <- readOGR("KBAsGlobal_2022_September_02_POL.shp") # to read in the KBA
# shapefile polygon data
Any ideas on the next steps?对接下来的步骤有什么想法吗? Thanks a million, appreciate any help!
感谢一百万,感谢任何帮助!
It would have been useful if you had been specific about the file you use.如果您具体说明了您使用的文件,那将会很有用。 I am using another file from the same webpage (this one has added tree cover fraction over time).
我正在使用来自同一网页的另一个文件(这个文件随着时间的推移增加了树木覆盖率)。
url <- "https://luh.umd.edu/LUH2/LUH2_v2f/added_tree_cover/added_tree_cover_input4MIPs_landState_ScenarioMIP_UofMD-IMAGE-ssp119-2-1-f_gn_2015-2100.nc"
f <- basename(url)
if (!file.exists(f)) download.file(url, f, mode="wb")
You are using obsolete R packages ("raster", "sp", "rgdal").您正在使用过时的 R 包(“raster”、“sp”、“rgdal”)。 Let's use "terra" instead.
让我们改用“terra”。
library(terra)
x <- rast(f)
x
#class : SpatRaster
#dimensions : 720, 1440, 85 (nrow, ncol, nlyr)
#resolution : 0.25, 0.25 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84
#source : added_tree_cover_input4MIPs_landState_ScenarioMIP_UofMD-IMAGE-ssp119-2-1-f_gn_2015-2100.nc:added_tree_cover
#varname : added_tree_cover (area_fraction)
#names : added~ver_1, added~ver_2, added~ver_3, added~ver_4, added~ver_5, added~ver_6, ...
#unit : 1, 1, 1, 1, 1, 1, ...
#time (years): 2015 to 2099
You could read your polygon data with你可以阅读你的多边形数据
v <- vect("KBAsGlobal_2022_September_02_POL.shp")
But since we do not have that file, I will use polygons for the countries of the world但由于我们没有该文件,我将使用多边形表示世界各国
library(geodata)
v <- geodata::world(path=".")
# extract raster values, and compute the mean for each polygon
e <- extract(x, v, mean, ID=FALSE)
e <- cbind(country=v$COUNTRY, e)
e[1:5, 1:5]
# country added_tree_cover_1 added_tree_cover_2 added_tree_cover_3 added_tree_cover_4
#1 Aruba 0.0053531881 0.000000e+00 0.000000e+00 0.000000e+00
#2 Afghanistan 0.0000498043 4.980157e-05 4.979897e-05 4.979643e-05
#3 Angola 0.0011926381 1.192639e-03 1.199086e-03 1.199087e-03
#4 Anguilla 0.0000000000 0.000000e+00 0.000000e+00 0.000000e+00
#5 Åland 0.0000000000 0.000000e+00 0.000000e+00 0.000000e+00
If your polygons are very small relative to the grid cell size, that is when you only have a few cells per polygon, you may want to use argument exact=TRUE
to better account for cells that are partly inside of a polygon.如果您的多边形相对于网格单元格大小非常小,即每个多边形只有几个单元格,您可能需要使用参数
exact=TRUE
来更好地说明部分位于多边形内部的单元格。
For future reference, to ask a question like this it is better to use data generated by code, or with files that ship with R. See the examples in the R help file for how this is done (eg ?rast
, ?vect
, ?extract
, ?zonal
.为了将来参考,要问这样的问题,最好使用代码生成的数据,或者使用 R 附带的文件。请参阅 R 帮助文件中的示例,了解如何完成此操作(例如
?rast
, ?vect
?extract
, ?zonal
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.