移除水体上的部分光栅图像

Removing part of a raster image over a body of water

我正在尝试用 leaflet 制作一张地图,最终将用于显示在某个地理区域上插值的各种估计值。

对于 link 访问数据,我深表歉意。我不够聪明,不知道如何从 Google Docs 导入 .RData 文件。我正在使用的栅格形状可以从 https://drive.google.com/file/d/0Bw3leA1Ef_e5RmdKVDNYX0xmS2s/view?usp=sharing 下载 (这是更新后的 link,其中包含下面使用的 Coordrnewcleveland 对象。

我可以通过以下方式获得我想要的地图:

library(tigris) 
library(leaflet)
library(raster)
library(magrittr)
library(dplyr)

load("Coord.Rdata")

rnew <- 
  rasterFromXYZ(
    Coord,
    crs = "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
  )

pal <- colorNumeric(c("#0C2C84", "#41B6C4", "#FFFFCC"), values(rnew),
                    na.color = "transparent")

leaflet() %>% addTiles() %>%
  addRasterImage(rnew, colors = pal, opacity = 0.4)

产生

现在,尽管如此美丽,但在伊利湖上显示估计值对我来说没有多大意义。我想删除湖上光栅图像的部分,或者将其设置为透明...不会给人留下我正在计算海洋野生动物风险的印象的东西。

我想如果我能找到俄亥俄州的纬度和经度,我也许可以只使用交叉点,从而删除湖上的点。我试着用

把它放在一起
ohio <- tracts(state = "39")

ohio_raster <- 
  attributes(ohio)$data[, c("INTPTLON", "INTPTLAT")] %>%
  setNames(c("x", "y")) %>%
  mutate(x = sub("^[-]", "", x) %>%
           as.numeric(),
         y = sub("^[+]", "", x) %>%
           as.numeric(),
         layer = 1) %>%
  as.matrix() %>%
  rasterFromXYZ(crs = "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")

但出现错误

Error in rasterFromXYZ(., crs = "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0") : x cell sizes are not regular

这种作品,但是没有颜色,看不到交集

ohio_raster <- 
  raster(ohio, crs = "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")

leaflet() %>% addTiles() %>%
  addRasterImage(rnew, colors = pal, opacity = 0.4) %>%
  addRasterImage(ohio_raster, colors = "red", opacity = .4)

Warning message: In raster::projectRaster(x, raster::projectExtent(x, crs = sp::CRS(epsg3857))) :
'from' has no cell values

(注意:地图已缩小以包括整个俄亥俄州)

我还认为如果我使用空间包可能会计算出交叉点:

spdf <- 
  SpatialPointsDataFrame(Coord[, 1:2], 
                         Coord,
                         proj = CRS("+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

ohio_coord <- attributes(ohio)$data[, c("INTPTLON", "INTPTLAT")] %>%
  setNames(c("x", "y")) %>%
  mutate(x = sub("^[-]", "", x) %>%
           as.numeric(),
         y = sub("^[+]", "", x) %>%
           as.numeric(),
         layer = 1) %>%
  as.matrix() %>%
  as.data.frame()

spdf_ohio <- 
  SpatialPointsDataFrame(ohio_coord[, 1:2], 
                         ohio_coord,
                         proj = CRS("+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

spdf_merge <- merge(spdf, spdf_ohio)

leaflet() %>% addTiles() %>%
  addRasterImage(raster(spdf_merge), colors = pal, opacity = 0.4) 

但我得到

Warning message: In raster::projectRaster(x, raster::projectExtent(x, crs = sp::CRS(epsg3857))) : 'from' has no cell values

因此,实际问题:

  1. 这是解决这个问题的合理方法吗?有没有我遗漏的更好的方法?
  2. 如何从纬度和经度转换为带有阴影值的光栅图像,以便我可以看到我正在处理的内容?

即使我没有得到手头问题的直接答案,我也会欣喜若狂地收到一些关于在哪里可以找到类似问题示例的指导。我还没有找到任何类似的东西,这可能是不知道正确的搜索词的标志。

编辑:

我觉得我可能更近了一步,但还差得远。

我根据@IvanSanchez 的推荐下载了克利夫兰地理资料。简单地绘制多边形提供。

这看起来像是我想要保留的土地。这是很好的一步。

现在我尝试通过

将其与我当前的光栅相交
cleveland <- 
  readOGR(dsn = "cleveland_ohio.osm2pgsql-shapefiles",
          layer = "cleveland_ohio_osm_polygon",
          p4s = "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")

leaflet() %>% addTiles() %>%
  addRasterImage(raster::intersect(rnew, cleveland), 
                 colors = pal, 
                 opacity = 0.4)

但我似乎没有区域的交集,而是联合。或者不同的东西。

我已将上面的 link 更新为包含 Coordrnewcleveland 对象的文件。

我建议您从 MapZen metro extract for Cleveland 下载该地区的地理数据。这将为您提供一些适合陆地区域和水域的多边形 - 然后您可以将您的形状与陆地多边形相交(或从您的形状中减去水域多边形)。

您可以使用表示陆地区域的 shapefile 来掩盖栅格。您可以使用 Ohio tract shapefile,但 Natural Earth 的 lakes data 可能会简化事情。例如:

library(leaflet)
library(raster)
library(magrittr)
library(rgdal)
library(rgeos)

load('Coord.Rdata')  
rnew <- rasterFromXYZ(Coord, crs='+init=epsg:4326')
pal <- colorNumeric(c('#0C2C84', '#41B6C4', '#FFFFCC'), values(rnew),
                    na.color = 'transparent')

# Download and extract the Natural Earth data
download.file(
  file.path('http://www.naturalearthdata.com/http/',
            'www.naturalearthdata.com/download/10m/physical/ne_10m_lakes.zip',
            f <- tempfile())
unzip(f, exdir=tempdir())

# Read in the lakes shapefile    
lakes <- readOGR(tempdir(), 'ne_10m_lakes')

# Create a "land" shapefile by taking the difference between the 
# raster's extent and the lakes polys
land <- gDifference(as(extent(rnew), 'SpatialPolygons'), lakes)

# Mask rnew
rnew2 <- mask(rnew, land)

leaflet() %>% addTiles() %>%
  addRasterImage(rnew2, colors = pal, opacity = 0.4)

它并不完全符合海岸线,但还算不错。