我想将栅格图层转换为数据框并获取坐标。
这工作得很好,但没有给出 xy 值:
as.data.frame(raster_layer)
as.data.frame(x = naip_svm_cropped)
category 1 forest_broadleafdark 2 forest_broadleafdark 3 forest_broadleafdark 4 grassland 5 grassland 6 grassland
这会引发错误:
as.data.frame(raster_layer, xy = T)
错误是:
匹配错误(round(v),rat$ID):在为函数“匹配”选择方法时评估参数“x”时出错:v[,i]错误:维数不正确
我怀疑问题出在光栅属性表的某个地方,但不确定如何继续。我想我可以将因子转换为数字并尝试从那里开始(xy=T
适用于非因子栅格),但我想找出为什么添加xy=T
会出现此错误。所以我的问题实际上是“为什么会发生这种情况,我怎样才能让它工作(返回 xy 值和单元格值)?”
栅格图层具有作为数据的因子(我不确定如何调用它,这里是 str 语句):
str(naip_svm_cropped@data)
Formal class '.SingleLayerData' [package "raster"] with 13 slots
..@ values : logi(0)
..@ offset : num 0
..@ gain : num 1
..@ inmemory : logi FALSE
..@ fromdisk : logi TRUE
..@ isfactor : logi TRUE
..@ attributes:List of 1
.. ..$ :'data.frame': 5 obs. of 2 variables:
.. .. ..$ ID : num [1:5] 0 1 2 3 4
.. .. ..$ category: Factor w/ 5 levels "forest_broadleafdark",..: 4 1 2 3 5
..@ haveminmax: logi TRUE
..@ min : num 0
..@ max : num 4
..@ band : int 1
..@ unit : chr ""
..@ names : chr "madison_classcombine"
具有如下结构的数据帧上的行为:
str(mad_veg_cropped@data)
Formal class '.SingleLayerData' [package "raster"] with 13 slots
..@ values : logi(0)
..@ offset : num 0
..@ gain : num 1
..@ inmemory : logi FALSE
..@ fromdisk : logi TRUE
..@ isfactor : logi FALSE
..@ attributes: list()
..@ haveminmax: logi TRUE
..@ min : num -0.719
..@ max : num 1.04
..@ band : int 1
..@ unit : chr ""
..@ names : chr "PercentVeg"
head(as.data.frame(mad_veg_cropped, xy = T))
x y PercentVeg
1 291855.5 4775116 0.7182595
2 291856.5 4775116 0.7402779
3 291857.5 4775116 0.7601378
4 291858.5 4775116 0.7702084
5 291859.5 4775116 0.7774438
6 291860.5 4775116 0.7574666
我希望能够得到一列“类别”、“x”和“y”。
sessionInfo() R 版本 3.1.2 (2014-10-31) 平台:x86_64-apple-darwin13.4.0 (64-bit) 其他附加软件包:[1] raster_2.3-24 sp_1.0-17
dput(naip_svm_cropped)
new("RasterLayer"
, file = new(".RasterFile"
, name = "/private/var/folders/yj/vjkj1yyx1n510rf_rggqdb640000gr/T/R_raster_tedward/2015-06-04_122215_4840_06850.grd"
, datanotation = "INT2S"
, byteorder = structure("little", .Names = "value")
, nodatavalue = -32768
, NAchanged = FALSE
, nbands = 1L
, bandorder = structure("BIL", .Names = "value")
, offset = 0L
, toptobottom = TRUE
, blockrows = 0L
, blockcols = 0L
, driver = "raster"
, open = FALSE
)
, data = new(".SingleLayerData"
, values = logical(0)
, offset = 0
, gain = 1
, inmemory = FALSE
, fromdisk = TRUE
, isfactor = TRUE
, attributes = list(structure(list(ID = c(0, 1, 2, 3, 4), category = structure(c(4L,
1L, 2L, 3L, 5L), .Label = c("forest_broadleafdark", "grassland",
"shadow1_tree", "Unclassified", "urban_buildings"), class = "factor")), .Names = c("ID",
"category"), row.names = c(NA, -5L), class = "data.frame"))
, haveminmax = TRUE
, min = 0
, max = 4
, band = 1L
, unit = ""
, names = "madison_classcombine"
)
, legend = new(".RasterLegend"
, type = character(0)
, values = logical(0)
, color = logical(0)
, names = logical(0)
, colortable = c("#000000", "#008B00", "#FF0000", "#FFFF00", "#FF00FF")
)
, title = character(0)
, extent = new("Extent"
, xmin = 291855
, xmax = 311023
, ymin = 4768423
, ymax = 4775116
)
, rotated = FALSE
, rotation = new(".Rotation"
, geotrans = numeric(0)
, transfun = function ()
NULL
)
, ncols = 19168L
, nrows = 6693L
, crs = new("CRS"
, projargs = "+proj=utm +zone=16 +datum=NAD83 +units=m +no_defs"
)
, history = list()
, z = list()
)