R-具有相同crs,范围,尺寸和分辨率的栅格不对齐 [英] R - rasters with same crs, extent, dimension, resolution do not align

查看:302
本文介绍了R-具有相同crs,范围,尺寸和分辨率的栅格不对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查找枫糖浆每年的平均生产天数。我的枫叶分布数据在ascii文件中。我有一个从NetCDF文件创建的栅格,它名为 brick.Tmax 。我想将 brick.Tmax 的规格与我的枫叶分布数据进行匹配。

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)

我如何从 brick.Tmax中投影所有规格(尺寸,crs,分辨率和范围)。 / code>到 red_raster 上,同时仍保留 red_raster 的值吗?似乎两者是互斥的。

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.

注意:为了简化我的问题,我在原始帖子中做了很多修改,如果在当前情况下以下评论令人困惑,我深表歉意。 (我删除了像中间人一样的栅格 prodavg_rast )。

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

推荐答案

两个栅格显然没有相同的范围。实际上是在不同的宇宙中(坐标参考系统)。 brick.Tmax 具有角(经度/纬度)坐标: + proj = longlat + datum = WGS84
但是 red_raster 显然没有给出范围:-1793092、1206908,-1650894、1149106 。因此,要一起使用这些数据,需要对两者之一进行转换(投影到另一个的坐标参考系统中)。问题是我们不知道red_raster的crs是什么(esri ascii文件不存储该信息!)。因此,您需要从数据源中找出它是什么,或者通过猜测覆盖范围和约定来确定它是什么。找到后,您可以执行以下操作:

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)

投影栅格需要时间。测试您是否知道cr的一个好方法是转换一些可以显示事物是否对齐的多边形。

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)

这篇关于R-具有相同crs,范围,尺寸和分辨率的栅格不对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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