简体   繁体   English

R:覆盖光栅层的xy坐标

[英]R: over-write xy coordinates of raster layer

I have a raster with XY pixel coordinates which I want to convert to lat and long.我有一个带有 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)

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)?如何覆盖或创建具有纬度/经度空间范围而不是图像坐标(XY 像素)的重复栅格? 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 janW1.png

Changing the projection of raster data is not as simple as for points (and lines, polygons).更改栅格数据的投影并不像点(和线、多边形)那样简单。 This is because if you compute the new coordinates from the current cells, they won't be in a regular raster.这是因为如果您从当前单元格计算新坐标,它们将不在常规栅格中。

You can use function projectRaster (raster package) to deal with this.您可以使用函数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') 

Alternatively, you can interpolate values you computed to a new raster.或者,您可以将计算出的值插入到新栅格中。 See ?raster::interpolate for examples.有关示例,请参阅?raster::interpolate

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.您创建的空白栅格会自动在 WGS84 中,因此您可能需要在输入数据后重新投影它。

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

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