首页 > 解决方案 > R:使用 Shift 函数移动光栅时出错:它不会移动。为什么?

问题描述

我有一个 nc 文件(我可以在 R 和 ArcGIS 中将其作为栅格读取)和加拿大的 shapefile(多边形)。

当我在 ArcMap 中打开这两个文件时,它会即时重新投影它们,并且多边形将位于正确的位置:

在此处输入图像描述

然后,我使用 R 打开并使用代码绘制它们:

## set the working directory:
setwd("C:/Users/Desktop/Data")

## required libraries
library(raster)
library(rgdal)
library(ncdf4)

## reading the polygon:
My_study_area <- readOGR(dsn = ".", layer = "Canada_AB")

## create a connection to the downloaded NC file:
fn <- "tasmin_day_CanESM5_ssp126_r1i1p1f1_gn_21010101-23001231_cliped_AB.nc"
ncin <- nc_open(fn) 
ncin

## create a raster from the nc file:
fn_brick <- brick(fn) 
fn_brick
extent(fn_brick)

这是输出:

class      : Extent
xmin       : 229.2188 
xmax       : 271.4062
ymin       : 39.06842
ymax       : 64.18258


## since it has 73,000 layers, let us only work on one layer: fn_brick[[1]]

我的目标:

## I want to shift this raster in the x direction for -177.1876 units:  
## why -177.1876 units ?
## because I know if I shift it for -177.1876 units, the raster and the polygon will be in the right place 

fn_brick_shifted <- shift(fn_brick[[1]], dx= -177.1876, dy= 0, filename="new_nc_file", format="GTiff", datatype='FLT4S', overwrite=T)

extent(fn_brick_shifted)

尽管移动了栅格,但其范围仍然保持不变:

class      : Extent
xmin       : 229.2188 
xmax       : 271.4062
ymin       : 39.06842
ymax       : 64.18258

但是,让我们绘制它们:

plot(fn_brick_shifted)
lines(My_study_area)

R中的结果是这样的:

在此处输入图像描述

可以看出,栅格没有移动,因此多边形没有绘制在正确的位置。

但是,我们可以看到文件的坐标信息:

对于栅格:

crs(fn_brick_shifted)
CRS arguments:
 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 

对于多边形:

crs(My_study_area)
CRS arguments:
 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 

我在为栅格使用移位功能时犯了任何错误吗?

我是否需要使用其他功能来实现我的目标?

任何评论或帮助将不胜感激。

如果您认为拥有栅格和多边形文件可能会有所帮助,您可以在此处以 zip 文件的形式下载它们:

nc 和多边形.zip

标签: rpolygonrasternetcdfshift

解决方案


推荐阅读