R:覆盖栅格图层的xy坐标 [英] R: over-write xy coordinates of raster layer

查看:168
本文介绍了R:覆盖栅格图层的xy坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有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)

我已经使用此处指定的公式计算了纬度/经度坐标. /p>

这导致了以下数据框

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

解决方案

是否可以从头开始创建具有所需分辨率和空间范围的栅格,然后将值导入其中.要创建栅格,您可以使用类似以下内容的方法:

# 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

我认为我从您的代码中获得了正确的分辨率,但是您将需要根据自己的程度输入四个坐标.空白栅格必须具有与原始栅格完全相同的分辨率,否则它将无法正常工作.您创建的空白栅格将自动在WGS84中显示,因此您可能需要在输入数据后重新投影它.

I have a raster with XY pixel coordinates which I want to convert to lat and 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)

I have calculated the lat/long coords using the formula specified here.

This has resulted in the following dataframe

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

How can I over-write or create a duplicate raster with the spatial extent in lat/long instead of image coordinates (XY pixels)? Or is there an easier way to convert the pixels to lat/Lon?

Code

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

解决方案

Could you create a raster from scratch with the resolution and spatial extent that you want and then import your values into it. To create a raster you could use something like:

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

To get the values from your previous raster into the raster you've just created you can use:

test <- raster('janW1.png')

# Create vector of values from test

n <- values(test)

# Give values from test to r

values(r) <- n

I think I got the correct resolution from your code but you will need to put the four coordinates for your extent in yourself. The blank raster will have to have exactly the same resolution as your original raster or it won't work. The blank raster you create is automatically in WGS84 so you might want to reproject it once you've got your data in.

这篇关于R:覆盖栅格图层的xy坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆