R - rasters with same crs, extent, dimension, resolution do not align

佐手、 提交于 2019-12-20 06:27:34

问题


I am finding the average production days per year for maple syrup. My maple distribution data is in an ascii file. I have a raster (created from NetCDF files) called brick.Tmax. I want to match the specs of brick.Tmax to my maple distribution data.

##    These are the specs I want to use for my maple distribution
brick.Tmax
class       : RasterBrick 
dimensions  : 222, 462, 102564, 366  (nrow, ncol, ncell, nlayers)
resolution  : 0.125, 0.125  (x, y)
extent      : -124.75, -67, 25.125, 52.875  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : E:\all_files\gridded_obs.daily.Tmax.1980.nc 
names       : X1980.01.01, X1980.01.02, X1980.01.03, X1980.01.04,    X1980.01.05, X1980.01.06, X1980.01.07, X1980.01.08, X1980.01.09, X1980.01.10, X1980.01.11, X1980.01.12, X1980.01.13, X1980.01.14, X1980.01.15, ... 
Date        : 1980-01-01, 1980-12-31 (min, max)
varname     : Tmax 

## reading in red maple data from ascii file into rasterLayer
red_raster <- raster("E:/all_files/Maple_Data/redmaple.asc")
red_raster
class       : RasterLayer 
dimensions  : 140, 150, 21000  (nrow, ncol, ncell)
resolution  : 20000, 20000  (x, y)
extent      : -1793092, 1206908, -1650894, 1149106  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : E:\all_files\Maple_Data\redmaple.asc 
names       : redmaple 
values      : -2147483648, 2147483647  (min, max)

How can I project all specs (dimension, crs, resoluion, and extent) from brick.Tmax onto red_raster, while still preserving the values of red_raster? It seems like the two are mutually exclusive.

NOTE: In an attempt to streamline my question, I edited my question quite a bit from my original post, so apologies if the comments below are confusing in the current context. (I removed the raster prodavg_rastwhich was acting like a middleman).


回答1:


The two rasters clearly do not have the same extent. In fact are in different universes (coordinate reference systems). brick.Tmax has angular (longitude/latitude) coordinates: +proj=longlat +datum=WGS84 but red_raster clearly does not given extent : -1793092, 1206908, -1650894, 1149106. So to use these data together, one of the two needs to be transformed (projected into the the coordinate reference system of the other). The problem is that we do not know what the the crs of red_raster is (esri ascii files do not store that information!). So you need to find out what it is from your data source, or by guessing giving the area covered and conventions. After you find out, you could do something like:

library(raster)
tmax <- raster(nrow=222, ncol=462, xmn=-124.75, xmx=-67, ymn=25.125, ymx=52.875, crs="+proj=longlat +datum=WGS84")

red <- raster(nrow=140, ncol=150, xmn=-1793092, xmx=1206908, ymn=-1650894, ymx=1149106, crs=NA)
crs(red) <- "  ??????     " 

redLL <- projectRaster(red, tmax)

Projectiong rasters takes time. A good way to test whether you figured out the crs would be to transform some polygons that can show whether things align.

library(rgdal)
states <- shapefile('states.shp')
sr <- spTransform(states, crs(red)
plot(red)
plot(sr, add=TRUE)


来源:https://stackoverflow.com/questions/38135478/r-rasters-with-same-crs-extent-dimension-resolution-do-not-align

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!