【问题标题】:Converting a raster to a csv in R在R中将栅格转换为csv
【发布时间】:2017-07-12 17:47:39
【问题描述】:

我希望将栅格转换为 csv 文件。我试图将栅格转换为一个文件上的数据框,以查看它是否有效。我试过使用:

as.data.frame( rasterToPoints(species) )

但是当我尝试将“物种”写入 csv 时出现错误:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
cannot coerce class "structure("RasterLayer", package = "raster")"   to a data.frame

这是我的代码(我需要将多个栅格转换为 csv(参见循环))

#start loop
file.names <- dir(path, pattern=".csv")

for(i in 1:length(file.names)){
  file<- read.csv(file.name[i], header = TRUE, stringsAsFactors=FALSE)

#subsetting each file and renaming column header names
sub.file<-subset(file, select = c('Matched.Scientific.Name',  'Vernacular.Name...matched', 'Latitude...processed', 'Longitude...processed'))
names(sub.file) <- c('species', 'name', 'Lat','Lon')

#turn into a SpatialPointsDataFrame 
coordinates(sub.file) <- ~ Lon + Lat 
proj4string(sub.file) <- '+init=EPSG:4326'
plot(sub.file, axes=TRUE)

#converting to BNG
sub.file.BNG <- spTransform(sub.file, '+init=EPSG:27700')
plot(sub.file.BNG, axes=TRUE)

#creating template raster
template <- raster(xmn=400000, xmx=600000, ymn=200000, ymx=800000,  res=25000, crs='+init=EPSG:27700')

#point data > presence grid
species <- rasterize(sub.file.BNG, template, field=1)
plot(species)

# UK wide
template <- raster(xmn=-200000, xmx=700000, ymn=0, ymx=1250000, res=25000, crs='+init=EPSG:27700')

# use that to turn species point data into a presence grid
species <- rasterize(sub.file, template, field=1)
plot(species)

#converting a raster>dataframe>csv?????
as.data.frame( rasterToPoints(species) )
}

【问题讨论】:

  • 您需要将as.data.frame( rasterToPoints(species) ) 分配给一个对象,然后将该对象另存为CSV。
  • 光栅对象通常可以转换为矩阵。也许尝试这样做,看看矩阵是否可以转换为您需要的数据框。转到此小插图的第 5.4 节:cran.r-project.org/web/packages/raster/vignettes/Raster.pdf

标签: r csv dataframe r-raster


【解决方案1】:

在提问时始终提供一些示例数据。

library(raster)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)

获取单元格值

x <- as.data.frame(r)
head(x, 2)
#  test
#1   NA
#2   NA

获取单元格坐标和值,只针对非NA的单元格

x <- rasterToPoints(r) 
head(x, 2)
#         x      y    test
#[1,] 181180 333740 633.686
#[2,] 181140 333700 712.545

获取单元格坐标和值,只针对所有单元格(包括NA)

x <- cbind(coordinates(r), v=values(r))
head(x, 2)
#          x      y  v
#[1,] 178420 333980 NA
#[2,] 178460 333980 NA

你选择哪一个,然后你就可以做

write.csv(x, "test.csv")

你犯的错误是你没有将as.data.frame的结果赋值给一个变量,然后尝试用write.csv写RasterLayer。这是一个错误,你会得到 ​​p>

write.csv(r)
#Error in as.data.frame.default(x[[i]], optional = TRUE) : 
# cannot coerce class ‘structure("RasterLayer", package = "raster")’ to a 
# data.frame

顺便说一句,如果您有多个栅格,您可能需要先将它们组合起来

s <- stack(r, r, r)
x <- rasterToPoints(s) 
head(x, 2)
#          x      y  test.1  test.2  test.3
#[1,] 181180 333740 633.686 633.686 633.686
#[2,] 181140 333700 712.545 712.545 712.545
write.csv(x, "test.csv")

【讨论】:

    【解决方案2】:

    假设您的栅格是“物种”

     species<- raster("C:/.../species.tif")
    

    要进行这种转换,需要获取每个像素的值:X 坐标 (1)、Y 坐标 (2) 和每个单元格的自身值 (3)。

     # don't run these lines
     #(1) = coordinates (species) [, 1]
     #(2) = coordinates (species) [, 2]
     #(3) = values (species)
    

    有了这些表达式,我们可以将它们添加到数据框中,如下所示

    dat<- data.frame("X"=coordinates(species)[,1],"Y"=coordinates(species) 
    [,2],"Values"=values(species))
    

    【讨论】:

    • 解释你的建议。
    • 请不要只发布代码作为答案,而是说明您的代码的作用以及它如何解决问题的问题。带有解释的答案通常质量更高,更有可能吸引投票。
    猜你喜欢
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2016-06-07
    • 2021-03-26
    • 2011-03-08
    • 1970-01-01
    • 2018-09-03
    相关资源
    最近更新 更多