R:覆盖栅格图层的xy坐标

标签 r coordinates pixel raster r-raster

我有一个带有 XY 像素坐标的栅格,我想将其转换为纬度和经度。

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)

我已经使用指定的公式 here 计算了纬度/经度坐标.

这导致了以下数据框

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

如何使用纬度/经度空间范围而不是图像坐标(XY 像素)覆盖或创建重复栅格?或者是否有更简单的方法将像素转换为纬度/经度?

代码

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

#  Calculate longitude.

lonfract = data_matrix[,"x"] / (1596 - 1)
lon = -15 + (lonfract * (13 - -15))

#  Calculate latitude.

latfract = 1.0 - (data_matrix[,"y"] / (1617 - 1))  
Ymin = log(tan ((pi/180.0) * (45.0 + (47 / 2.0))))
Ymax = log(tan ((pi/180.0) * (45.0 + (62.999108 / 2.0))))
Yint = Ymin + (latfract * (Ymax - Ymin))
lat = 2.0 * ((180.0/pi) * (atan (exp (Yint))) - 45.0)

# Make single dataframe with XY pixels and latlon coords.
latlon <- data.frame(lat,lon)
tmp <- data.frame(data_matrix)
cords <- cbind(latlon, tmp)

janW1.png

最佳答案

更改栅格数据的投影并不像点(以及线、多边形)的投影那么简单。这是因为,如果您根据当前像元计算新坐标,它们将不会位于常规栅格中。

您可以使用函数projectRaster(光栅包)来处理这个问题。

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') 

或者,您可以将计算的值插入到新栅格中。有关示例,请参阅 ?raster::interpolate

关于R:覆盖栅格图层的xy坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36005925/

相关文章:

r - 动态 Rstudio 代码片段

c - 在 C 中使用两组坐标(纬度和经度)的轴承

javascript - Google map Javascript API 查看位置

python - 过滤给定范围内的坐标

java - 像素变化监听器java

javascript - 使用 P5 javascript 从图像生成调色板

r - 我不断收到此错误 “docker: invalid reference format: repository name must be lowercase.”

r - 如何从 Amelia 包中提取完整的数据集

binary - 解释 PNG 像素数据

r - 更改ggplot2中构面图的每一行的y轴限制