简体   繁体   English

与原始栅格相比,tmap 正在为裁剪的栅格图层绘制不同的图例(值范围?)

[英]tmap is plotting a different legend (range of values?) for a cropped rasterlayer compared to original raster

I am extremely new to working with spatial data and so most of what I'm about to say is me trying to speak a foreign language.我对处理空间数据非常陌生,所以我要说的大部分内容都是我试图说一门外语。 Right now I am trying to learn how to do this all in R (I am slightly more capable with this data in QGIS but for this solution, I am looking for R only).现在我正在尝试学习如何在 R 中完成这一切(我在 QGIS 中对这些数据的能力稍强,但对于这个解决方案,我只在寻找 R)。

My research involves ecological data in Pennsylvania (PA) and so I am playing around with cropping the US NLCD dataset to PA.我的研究涉及宾夕法尼亚州 (PA) 的生态数据,因此我正在尝试将美国 NLCD 数据集裁剪到 PA。 I have a raster layer for the NLCD and a shapefile for the boundary of Pennsylvania.我有一个用于 NLCD 的栅格图层和一个用于宾夕法尼亚州边界的 shapefile。 I am able to successfully crop the larger US raster down to PA as follows:我能够成功地将较大的美国栅格裁剪为 PA,如下所示:

library(raster)
library(rgdal)
pabound <- readOGR(dsn="...",
                   layer="PAbound")

nlcdRast <- raster(".../NLCD_2016_Land_Cover_L48_20190424.img")

pabound <- spTransform(pabound,CRS(proj4string(nlcdRast)))

PAnlcd <- raster::crop(nlcdRast,pabound)

If I run the simple plot command for both nlcdRast and PAnlcd (ie plot(nlcdRast) they maintain the same color scheme. But when I run it through tmap it seems to look at the cropped data differently and I am not exactly sure how to figure this out. Please see the plots below:如果我为nlcdRastPAnlcd运行简单的 plot 命令(即plot(nlcdRast)它们保持相同的配色方案。但是当我通过tmap运行它时,它似乎以不同的方式查看裁剪数据,我不确定如何计算这个了。请看下面的图:

library(tmap)

tm_shape(nlcdRast) +
  tm_raster()

在此处输入图像描述

And then when I plot the cropped version in tmap :然后当我tmap在 tmap 裁剪版本:

tm_shape(PAnlcd) +
  tm_raster()

在此处输入图像描述

As you can see, it is not simply the color palette that is changing (I am confident I could figure that out) but the real problem is I'm losing the important information as seen in the legend.正如您所看到的,不仅仅是调色板正在发生变化(我相信我可以弄清楚),但真正的问题是我正在丢失图例中看到的重要信息。 Whereas the full plot actually shows the categorical values for the raster NLCD, the cropped version now seems to show just some unknown numerical range.尽管完整的 plot 实际上显示了光栅 NLCD 的分类值,但裁剪后的版本现在似乎只显示了一些未知的数值范围。 Even though it looks bad at the moment, I'd like to have the same legend/information as seen in the full US map.尽管目前看起来很糟糕,但我希望拥有与完整的美国 map 中相同的图例/信息。

I apologize for not having a more reproducible example but I am completely lost on what is happening here so I can't quite replicate it.我很抱歉没有一个更可重复的例子,但我完全迷失了这里发生的事情,所以我不能完全复制它。 I suppose right now I'm just looking for where to look to try and figure out what changed.我想现在我只是在寻找可以尝试找出发生了什么变化的地方。 Thank you in advance.先感谢您。

Cropping is changing the way the pixels are represented.裁剪正在改变像素的表示方式。 To maintain your values use the stars package (also note I'm using the sf package for the shapefile):要保持您的值,请使用stars package(另请注意,我使用sf package 作为 shapefile):

library(stars)
library(sf)



  # load in NLCD
    nlcdRast <- read_stars(".../NLCD_2016_Land_Cover_L48_20190424.img")
  
  # read in study area
   pabound <- st_read(dsn="...", layer="PAbound")
  
  # reproject pabound to match NLCD
   pabound <- st_transform(pabound, CRSobj = crs(nlcdRast))
  
  # now crop
   panlcd <- st_crop(nlcdRast, pabound)

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

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