简体   繁体   English

不能从光栅转换的 plot 图像

[英]Can't plot image which is converted from a raster

I converted a raster to an image and wanted to plot it.我将光栅转换为图像并想要 plot 它。 However, I then get the following error:但是,然后我收到以下错误:

library(raster)
r
#class      : RasterLayer 
#dimensions : 23320, 37199, 867480680 (nrow, ncol, ncell) 
#resolution : 0.02, 0.02 (x, y) 
#extent     : 341668.9, 342412.9, 5879602, 5880069 (xmin, xmax, ymin, ymax) 
#crs        : +proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
#source     : r_tmp_2022-07-21_113344_507_06340.grd 
#names      : layer 
#values     : 2.220446e-16, 0.2999999 (min, max)

x  <- maptools::as.im.Rasterlayer(r)
x
#real-valued pixel image 23320 x 37199 pixel array (ny, nx) 
#enclosing rectangle: [341670, 342410] x [5879600, 5880100] units

plot(x)
#Error in (function (x = seq(0, 1, length.out = nrow(z)), y = seq(0, 1,  :
#‘useRaster = TRUE’ can only be used with a regular grid

I've already tried to find this error with help(plot) or something but there is nothing about it anywhere.我已经尝试通过 help(plot) 或其他东西找到这个错误,但在任何地方都没有任何关于它的内容。

Yea sure.是的。 So my Raster looks like this: My Raster:所以我的光栅看起来像这样: 我的光栅:

class: RasterLayer dimensions: 23320, 37199, 867480680 (nrow, ncol, ncell) resolution: 0.02, 0.02 (x, y) extent: 341668.9, 342412.9, 5879602, 5880069 (xmin, xmax, ymin, ymax) crs: +proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 source: r_tmp_2022-07-21_113344_507_06340.grd names: layer values: 2.220446e-16, 0.2999999 (min, max) class: RasterLayer dimensions: 23320, 37199, 867480680 (nrow, ncol, ncell) resolution: 0.02, 0.02 (x, y) extent: 341668.9, 342412.9, 5879602, 5880069 (xmin, xmax, ymin, ymax) crs: +proj= utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 来源:r_tmp_2022-07-21_113344_507_06340.grd 名称:层值:2.220446e-16, 0.2999999 (min,最大限度)

I used as.im.Rasterlayer from the maptools package to transform it into a image.我使用 maptools package 中的 as.im.Rasterlayer 将其转换为图像。 after that i just wanted to plot this image.之后我只想 plot 这个图像。 After the transformation into an image it looks like this:转换成图片后是这样的:

real-valued pixel image 23320 x 37199 pixel array (ny, nx) enclosing rectangle: [341670, 342410] x [5879600, 5880100] units实值像素图像 23320 x 37199 像素阵列 (ny, nx) 包围矩形:[341670, 342410] x [5879600, 5880100] 单位

I loaded the raster with raster().我用 raster() 加载了光栅。 And im using raster, spatstat, maptools.我正在使用 raster、spatstat、maptools。

In future, please provide a minimal working example - actual data and code that generates the error.将来,请提供一个最小的工作示例 - 生成错误的实际数据和代码。

Whenever you get an error, I suggest you immediately type每当您遇到错误时,我建议您立即输入

traceback()

to see the sequence of functions that were executed.查看执行的功能序列。 (The printout is a traceback: it starts with the function that generated the error, then lists the function that called it, and so on, until it reaches the command line that you originally typed.) This will often tell you where to look. (打印输出是回溯:它以产生错误的 function 开头,然后列出调用它的 function,依此类推,直到到达您最初键入的命令行。)这通常会告诉您在哪里查看。

Since you are plotting an object of class im , and plot is a generic function, the relevant help file is for the method plot.im , ie you should type help(plot.im) or ?plot.im , not help(plot) . Since you are plotting an object of class im , and plot is a generic function, the relevant help file is for the method plot.im , ie you should type help(plot.im) or ?plot.im , not help(plot) .

Since we don't have a working example, we can't debug this for you.由于我们没有工作示例,因此我们无法为您调试。 But this particular error is probably coming from the R base graphics function image.default and it is apparently complaining that the x and y coordinates in the data do not appear to be equally spaced.但是这个特殊的错误可能来自 R 基础图形 function image.default并且它显然抱怨数据中的xy坐标似乎不是等距的。 You can try either setting useRaster=FALSE in the plot command, or fixing the data.您可以尝试在plot命令中设置useRaster=FALSE ,或者修复数据。

Some packages alter the x and y coordinates of pixels in an image, by a very small amount, in such a way that other packages "think" the coordinates are not equally spaced.一些包以非常小的量改变图像中像素的xy坐标,使得其他包“认为”坐标不是等距的。 In spatstat there is a function repair.image.xycoords that can be used to repair such data.spatstat中有一个 function repair.image.xycoords可用于修复此类数据。 So, if Z is your image, use因此,如果Z是您的图像,请使用

 ZZ <- repair.image.xycoords(Z)

to fix the coordinates, and then plot ZZ .修复坐标,然后 plot ZZ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM