我试图计算某个GPS位置在该位置的日历年、上一日历年和下一日历年的USDA CropScape作物价值(例如,如果GPS位置的日期是2016年10月1日,我希望提取该位置在2015年、2016年和2017年的作物价值)。
下面是我用来准备GPS和CropScape数据的代码:
library(rgdal)
library(raster)
library(sf)
library(sp)
library(rgeos)
library(adehabitatHR)
library(lubridate)
head(data)
proj <- 32612
data <- st_as_sf(data, coords=c("easting","northing"), dim="XY", crs=proj)
cropScape_2014 <- raster("./data/_raw/cropScape/cropScape_2014.tif")
cropScape_2015 <- raster("./data/_raw/cropScape/cropScape_2015.tif")
cropScape_2016 <- raster("./data/_raw/cropScape/cropScape_2016.tif")
cropScape_2017 <- raster("./data/_raw/cropScape/cropScape_2017.tif")
cropScape_2018 <- raster("./data/_raw/cropScape/cropScape_2018.tif")
cropScape_2019 <- raster("./data/_raw/cropScape/cropScape_2019.tif")
cropScape_2020 <- raster("./data/_raw/cropScape/cropScape_2020.tif")
cropScape_2021 <- raster("./data/_raw/cropScape/cropScape_2021.tif")
cropScape <- stack(cropScape_2014, cropScape_2015, cropScape_2016,
cropScape_2017, cropScape_2018, cropScape_2019,
cropScape_2020, cropScape_2021)
data <- as(data, "Spatial")
popMCP <- mcp(data, percent = 100)
popMCPbuf <- raster::buffer(popMCP, 1000)
cropScape2 <- mask(cropScape, popMCPbuf)
cropScape_cropped <- crop(cropScape2, popMCPbuf)
tz(data$date)
data$calYr <- as.numeric(strftime(data$date, format = "%Y", tz = "MST7MDT"))
dput(data[sample(nrow(data), size = 10),])
dput(head(cropScape_cropped))
这是我的GPS数据的一个小子集,我在这一点上:
new("SpatialPointsDataFrame", data = structure(list(id = c("GT74",
"GT48", "GT52", "GT82", "GT74", "GT52", "GT82", "GT9", "GT45",
"GT43"), date = structure(c(1607320849, 1544097667, 1588500050,
1554854505, 1618632063, 1549476050, 1577080837, 1449784850, 1542376850,
1548309669), class = c("POSIXct", "POSIXt"), tzone = "MST7MDT"),
id_bioYr = c("GT74_2020", "GT48_2018", "GT52_2019", "GT82_2018",
"GT74_2020", "GT52_2018", "GT82_2019", "GT9_2015", "GT45_2018",
"GT43_2018"), state = c(1L, 1L, 3L, 2L, 1L, 1L, 2L, 2L, 2L,
1L), used = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0), landcover = c("agricultural",
"agricultural", "agricultural", "agricultural", "agricultural",
"shadow", "water", "xericGrass", "agricultural", "agricultural"
), calYr = c(2020, 2018, 2020, 2019, 2021, 2019, 2019, 2015,
2018, 2019)), row.names = c(471519L, 253950L, 310790L, 49469L,
479572L, 293620L, 573180L, 627720L, 238551L, 151426L), class = "data.frame"),
coords.nrs = numeric(0), coords = structure(c(483309.821132995,
480304.323982496, 479889.104783906, 469493, 458709.035703965,
475920.645473197, 470714.848866241, 474984.326637862, 473390.631355455,
479941.915819768, 4866298.15517148, 4867412.65105531, 4865629.72751859,
4865043, 4864209.38410713, 4863155.30805073, 4865581.17021548,
4865450.06432659, 4865829.2649267, 4861889.90054007), .Dim = c(10L,
2L), .Dimnames = list(NULL, c("coords.x1", "coords.x2"))),
bbox = structure(c(458709.035703965, 4861889.90054007, 483309.821132995,
4867412.65105531), .Dim = c(2L, 2L), .Dimnames = list(c("coords.x1",
"coords.x2"), c("min", "max"))), proj4string = new("CRS",
projargs = "+proj=utm +zone=12 +datum=WGS84 +units=m +no_defs"))
和作物数据(不确定这是否有帮助/人们会需要什么?):
structure(c(NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_), .Dim = c(20L, 8L), .Dimnames = list(NULL, c("cropScape_2014",
"cropScape_2015", "cropScape_2016", "cropScape_2017", "cropScape_2018",
"cropScape_2019", "cropScape_2020", "cropScape_2021")))
我一直在尝试编写一个循环,将我的三个值提取到数据中的新列,但我真的不知道如何开始。
理想情况下,在循环结束时,将有3个新列添加到数据(“crop_calYr-1”,“crop_calYr”,“crop_calYr+1”),并且它们将从正确的“cropScape_criped”层填充。我知道,当GPS位置是从2021年开始时,我必须将“NA”添加到“crop_calYr+1”。
2条答案
按热度按时间vxf3dgd41#
我无法重新创建栅格数据。我只是下载了几年的Fremont County,ID数据,因为它看起来像是您的点所在的位置。此处我使用了
raster
包,但我更愿意使用terra
。你需要修改我标注的年份。如果栅格中的年份和点中的年份有差异,这就不起作用了。例如,如果堆栈中有2014、2016-2019,而点覆盖了2014-2019年,你就必须修改它。
提取仍然返回数值而不是字符/因子值。但是您可以根据需要执行替换来更改这些值。
s5a0g9ez2#
正如约翰·波罗所建议的,使用"terra"(代替"raster")要容易得多。
示例数据
溶液
如果只需要匹配年份的值,则可以执行以下操作
但也要得到前后的年份:
这是几天前的一个类似(但更复杂)的问题。