简体   繁体   English

即使使用相同的 CRS,多边形也会从栅格向北移动

[英]Polygons shifted north of raster even with same CRS

I am having trouble.我遇到了麻烦。 I am unable to identify the issue when plotting a SpatialPixelDataframe and a SpatialPolygonDataframe with the same CRS in tmaps.在 tmap 中绘制具有相同 CRS 的SpatialPixelDataframeSpatialPolygonDataframe时,我无法识别问题。

The spatialpixels object can be found here saved as RDS, and the polygons shapefile here , zipped. spatialpixels可以在这里找到,保存为 RDS,多边形 shapefile在这里,压缩。

Here is my attempt with base functions:这是我对基本功能的尝试:

library(sf)
library(sp)
ireland <- st_read("Counties.shp") 
sp_pred <- readRDS("sppred_range100_sd2.RDS")

#transform polygons into the pixels CRS
ireland_proj <- st_transform(ireland, sp_pred@proj4string)

#turn into sp object
ireland_sp <- as_Spatial(ireland_proj)

#plot with base functions
plot(sp_pred['mean'])
plot(ireland_sp, add = T)

在此处输入图像描述

Here is my attempt with tmap这是我对tmap的尝试

library(tmap)
tm_shape(sp_pred) +
  tm_raster("mean", palette = terrain.colors(10)) +
  tm_shape(ireland_sp) +
  tm_borders("black", lwd = .5) +
  tm_legend(show = FALSE)

在此处输入图像描述

This is so simple and I can't see where I might have gone wrong, but also I can't see how it can be an error in how tmap works!这太简单了,我看不出哪里出了问题,但我也看不出tmap的工作方式是如何出错的!

As @krenz mentioned you're using different classes here together, however, I'm not fully sure what causes the problem.正如@krenz 提到的那样,您在这里一起使用了不同的类,但是,我不完全确定导致问题的原因。

Here's a workaround in which your data is first converted to a sf object which is then rasterized using st_rasterize .这是一种解决方法,其中您的数据首先转换为 sf object,然后使用st_rasterize进行光栅化。 The result only differs slightly from what you showed.结果仅与您显示的略有不同。 Maybe you have to play around a little bit with the resolution parameters:也许你必须玩一下分辨率参数:

library(tmap)
library(sf)

ireland <- st_read("counties/counties.shp") 
sp_pred <- readRDS("sppred_range100_sd2.RDS")

sp_pred_proc <- sp_pred %>% st_as_sf()
sp_pred_proc <- st_rasterize(sp_pred_proc["mean"], dx = 5000, dy = 5000)

tm_shape(sp_pred_proc) +
  tm_raster("mean", palette = terrain.colors(10)) +
  tm_shape(ireland) +
  tm_borders("black", lwd = .5) +
  tm_legend(show = FALSE)

The left plot was generated with the settings given above, the right one with dx = 6000, dy = 6000 .左边的 plot 是使用上面给出的设置生成的,右边的dx = 6000, dy = 6000

在此处输入图像描述

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

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