问题
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
回答1:
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.
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.
回答2:
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.
来源:https://stackoverflow.com/questions/36005925/r-over-write-xy-coordinates-of-raster-layer