【问题标题】:Polygons shifted north of raster even with same CRS即使使用相同的 CRS,多边形也会从栅格向北移动
【发布时间】:2022-01-01 22:21:17
【问题描述】:

我遇到了麻烦。在 tmap 中使用相同的 CRS 绘制 SpatialPixelDataframeSpatialPolygonDataframe 时,我无法识别问题。

spatialpixels 对象可以在保存为 RDS 的here 中找到,多边形 shapefile here 已压缩。

这是我对基本函数的尝试:

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)

这是我对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)

这太简单了,我看不出哪里出了问题,但我也看不出tmap的工作方式有什么错误!

【问题讨论】:

  • 我自己无法找到错误,但这看起来很可能是投影问题。 NB。您还在 tm_shape 中使用 sp 类 SpatialPolygonsDataFrame 和 SpatialPixelDataFrame,虽然不鼓励使用这些类。请参阅?tm_shape:“shp – 形状对象,它是来自 'sf' 或 'stars' 包定义的类的对象。也支持来自包 'sp' 和 'raster' 的对象,但不鼓励。”

标签: r spatial geo tmap proj


【解决方案1】:

正如@krenz 提到的,您在这里一起使用了不同的类,但是,我不完全确定导致问题的原因。

这是一种解决方法,首先将您的数据转换为 sf 对象,然后使用st_rasterize 进行光栅化。结果仅与您显示的略有不同。也许您必须稍微调整一下分辨率参数:

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)

左图是使用上面给出的设置生成的,右图是使用dx = 6000, dy = 6000 生成的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多