R:覆盖光栅层的xy坐标

R:覆盖光栅层的xy坐标,r,coordinates,pixel,raster,r-raster,R,Coordinates,Pixel,Raster,R Raster,我有一个带有XY像素坐标的光栅,我想将其转换为lat和long。 class : RasterLayer dimensions : 1617, 1596, 2580732 (nrow, ncol, ncell) resolution : 1, 1 (x, y) extent : 0, 1596, 0, 1617 (xmin, xmax, ymin, ymax) coord. ref. : NA data source : C:\janW1.png names

我有一个带有XY像素坐标的光栅,我想将其转换为lat和long。

class       : RasterLayer 
dimensions  : 1617, 1596, 2580732  (nrow, ncol, ncell)
resolution  : 1, 1  (x, y)
extent      : 0, 1596, 0, 1617  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : C:\janW1.png 
names       : janW1 
values      : 0, 255  (min, max)
我已经使用指定的公式计算了横向/纵向坐标

这导致了以下数据帧

heads(cords)
       lat       lon   x      y janW1
1 46.99401 -14.99122 0.5 1616.5     0
2 46.99401 -14.97367 1.5 1616.5     0
3 46.99401 -14.95611 2.5 1616.5     0
4 46.99401 -14.93856 3.5 1616.5     0
5 46.99401 -14.92100 4.5 1616.5     0
6 46.99401 -14.90345 5.5 1616.5     0
如何使用lat/long的空间范围而不是图像坐标(XY像素)重写或创建重复光栅?或者有没有更简单的方法将像素转换为lat/Lon

代码
库(光栅)

测试更改光栅数据的投影并不像更改点(和线、多边形)的投影那样简单。这是因为如果从当前单元格计算新坐标,它们将不会在常规光栅中

您可以使用函数
projectgraster
(光栅包)来处理此问题

library(raster)
test <- raster('janW1.png')

# In this case, you need to provide the correct crs to your data
# I am guessing. (this would be necessary for spatial data sets)
crs(test) <- '+proj=merc +datum=WGS84'

# you may also need to set the extent to actual coordinate values
# extent(test) <- c( , , ,) 
x <- projectRaster(test, crs='+proj=longlat +datum=WGS84') 
库(光栅)

测试您是否可以从头创建一个具有所需分辨率和空间范围的光栅,然后将您的值导入其中。要创建光栅,可以使用以下方法:

# Create a matrix of coordinates that define the limits of your raster

ex <- matrix(c(-20, -9.5, 20.5, 31.5), nrow = 2, ncol = 2, byrow = T)

# Turn those coordinates into an extent

ex <- extent(ex)

# Create a raster with the same dimensions as your original one

r <- raster(nrows = 1617, ncols = 1596)

# Set the extent of your raster

r <- setExtent(r, ex, keepres=F)
#创建一个坐标矩阵,用于定义光栅的限制

欢迎来到SO。请始终尝试提供一个可复制的示例-包括图像、您如何加载它和所需的包,以及您如何进行地理转换。对于您的问题:如果您只是按照要求更新
extent(r)会怎么样。我也尝试过更改范围,但是图像在打印时会拉伸。注意:
extent(r)不幸的是,代码没有返回经度和纬度,即
extent(x):-4.49e-05,0.01438596,-4.826575e-05,0.01466885(xmin,xmax,ymin,ymax)
但这很可能是因为分配的crs不正确,而且
r
的范围几乎肯定是错误的。您正在使用图形文件,就好像它表示空间数据一样。但图形文件确实表示空间数据,它是来自AVHRR卫星的海面温度光栅。由于该文件不存储空间参考,我们无法知道数据应该位于地球上的何处。除非您知道这一点并能提供该信息。数据的纬度/长距离为
纬度范围:47-62.999108
长距离范围:-15-13
。请参阅以了解更多详细信息。我会为范围设置四个坐标吗?此外,我似乎无法生成光栅文件?我可以将其添加到其他程序中,例如Arcmap。原始文件没有使用新的lat/long跳线过度写入。要添加coorindates,您只需将自己的坐标替换为我在代码的第一行中使用的“-20、-9.5、20.5、31.5”。在您告诉R之前,R不会对您的文件进行任何更改,因此您需要使用write.raster之类的工具将新光栅保存为可以加载到其他程序中的文件。
# Create a matrix of coordinates that define the limits of your raster

ex <- matrix(c(-20, -9.5, 20.5, 31.5), nrow = 2, ncol = 2, byrow = T)

# Turn those coordinates into an extent

ex <- extent(ex)

# Create a raster with the same dimensions as your original one

r <- raster(nrows = 1617, ncols = 1596)

# Set the extent of your raster

r <- setExtent(r, ex, keepres=F)
test <- raster('janW1.png')

# Create vector of values from test

n <- values(test)

# Give values from test to r

values(r) <- n