以栅格格式放置模型预测时出现错误消息

问题描述 投票:0回答:1

我正在尝试 R 和栅格包将模型预测放回栅格格式,但我收到一条错误消息。我的代码:

# get any raster from the dataset to use as a template for the predictions 
r2 <- Ras1[[2]] 

# assign the predicted values (map_real2) to it
values(r2) <- map_real2 

错误信息:

Error in setValues(x, value) : 
length(values) is not equal to ncell(x), or to 1

我觉得这个错误是关于名字和名字顺序的,但如果是这样,我不知道如何解决它。

我试图使光栅堆栈中的名称与模型的顺序相同:

m <- xgb_train_1$finalModel
name <- m$feature_names
Ras1 <- Ras[[name]]
Ras1[names]
raster r-raster
1个回答
0
投票

我写了一大堆但老实说在这里https://www.neonscience.org/resources/learning-hub/tutorials/raster-data-rhttps://rspatial.org/spatial/8-rastermanip.html# 在这里,您将学习完成所需任务所需的一切。它们很清晰,而不仅仅是一堵文字墙。

如果你想看看我的想法,那就仔细看看,但我是从这些以及在 R 之前在 ArcGIS 中工作的这些年开始学习的。

我怀疑你的挣扎是由于处理栅格列表的列表,这很难让你全神贯注。

如果您添加有关如何存储栅格和预测数据以及预测数据格式的详细信息,我绝对可以帮助您。

您的问题确实没有提供回答您的具体问题所需的信息。但是,我建议您编辑问题以包含预测数据的输出格式。它是否已经是具有 CRS、原点、范围、ymin、ymax 等的空间数据?它是 Geotiff 还是类似的格式?它是数组还是矩阵? ASCII?

你用的是什么包?光栅? GDAL? SP?星星

    m <- xgb_train_1$finalModel # What is xgb_train_1? What is final model?  
                              Are these raster objects already? 
name <- m$feature_names # are you just pulling the list of names from the 
                           model results and this is a list of names for 
                           the rasters?
Ras1 <- Ras[[name]] # what is Ras? This is how I would create a raster out 
                       of a list of rasters called Ras and name was one of 
                       the rasters in the list
Ras1[names] #what is names?

我会告诉你我是怎么做的,也许它会有所帮助,但也许我们的数据完全不同,所以对你没有帮助。

请记住,我使用非常大的栅格,例如每个栅格 9g,所以我无法使用存储在 Rs 工作内存中的东西。这可能会使这比您的过程复杂得多。

我不会创建模板然后为该模板分配新值。我会简单地将模型的结果分配给一个新的栅格对象。我广泛致力于基于以栅格作为协变量的模型构建新的栅格和预测。根据建模程序和方法,有很多方法可以做到这一点,因此没有一种方法。实际上,创建与现有栅格具有相同信息的新栅格对象并不特定于预测数据,因此它真的无关紧要。

我从已经具有匹配空间参数的栅格和空间数据开始。在一个项目中,我所有的栅格和 shapefile 等都来自同一地理区域,我通过确保我所有的输入栅格和空间数据文件具有相同的 CRS、像素大小、范围、原点等来开始每个项目,然后做什么如有必要,需要使它们完全相同。

如果您有一个栅格具有所有这些您希望所有其他栅格匹配的东西,您可以使用它来设置所有其余部分,以便它们相同。

这是我的空间数据的 crs 的一些代码

crs = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +nodefs"), remove = F)

如果你有一个你想要所有东西都匹配的光栅,那么你可以找到它

template_crs <-  CRS(Ras2[[2]]
crs

这将打印出类似于我上面的 crs 的内容

您还可以安装 GDAL - 无论如何这都是必需的 - 然后运行它会为您提供大部分您需要了解的模板信息,以确保您的预测图匹配。

GDALinfo("raster or path to raster") 

这里值得一提的是我的一些代码,用于启动程序以确保栅格在空间上匹配

 setwd("F:/GRASS/Cara") 
# setting where all file paths start or go
    GDALinfo("PTCT/chjv_01071.tif")
#check raster info - this was in "PTCT" folder in "F:/GRASS/Cara/
    test_tif <- raster("PTCT/chjv_01071.tif")
# first I tested the GDAL info to make sure it matched everything else and then I create raster object by reading in an existing raster that was in the GeoTiff format

我正在运行您必须指定栅格以确保输出符合空间需求的功能。我将其命名为 test_tiff

因为我确保我所做的一切总是在同一个 CRS 中,所以我不必花时间在模板上。我把工作放在首位,它与我的工作相得益彰。

要从你的输出中创建一个栅格,我会简单地写这个

r <- raster (map_real2) #creates raster from data appropriate for rasterization
writeRaster (r, file.path("folder/raster_name,.), format = "GTiff, overwrite = True or False depending on what you want) #Writes the raster into a location outside of the R environment, to the file path you specify, the last thing is the name and the format determines the file extension.

这将在 F:/GRASS/Cara/folder/ 中创建一个名为 raster_name.tiff 的栅格,因为我在开头设置了工作目录并在栅格名称前添加了“folder/”。

© www.soinside.com 2019 - 2024. All rights reserved.