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

标签 r leaflet gis raster

我正在尝试使用 leaflet 制作 map 最终将用于显示在一个地理区域内插值的各种估计值。

我很抱歉链接到数据。我不够聪明,无法弄清楚如何导入 .RData来自 Google 文档的文件。我正在使用的光栅形状可以从 https://drive.google.com/file/d/0Bw3leA1Ef_e5RmdKVDNYX0xmS2s/view?usp=sharing 下载
(这是与下面使用的 Coordrnewcleveland 对象的更新链接)。

我能够得到我想要的 map :

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)

其中产生
enter image description here

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

我想如果我能找到俄亥俄州的纬度和经度,我也许可以只使用交叉点,从而删除湖上的点。我试着把它放在一起使用
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



(注意: map 被缩小以包括整个俄亥俄州)
enter image description here

我还认为如果我使用空间包,可能可以计算出交叉点:
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



enter image description here

所以,实际问题:
  • 这甚至是解决这个问题的合理方法吗?有没有更好的方法我错过了?
  • 我怎样才能从纬度和经度转到带有值的光栅图像以提供阴影,以便我可以看到我正在处理的内容?

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

    编辑:

    我觉得我可能更近了一步,但仍然不够。

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

    enter image description here

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

    现在我尝试将它与我当前的栅格相交
    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)
    

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

    enter image description here

    我已将上面的链接更新为包含 Coord 的文件, rnew , 和 cleveland对象。

    最佳答案

    您可以使用表示土地区域的 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)
    

    enter image description here

    它并没有完美地沿着海岸线,但也不算太糟糕。

    关于在水体上移除部分光栅图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36938382/

    相关文章:

    r - 使用 caret 包应用 k 折交叉验证模型

    cors - L.TileLayer如何加载图 block ,为什么它不抛出CORS错误?

    javascript - 传单弹出更新closeOnClick

    javascript - 创建 Leaflet 自定义复选框控件

    python - 如何遍历 numpy 数组并选择相邻的单元格

    python - 列出具有几何类型的 map 图层名称

    c - R 的 C API 中的 SEXP 数据类型到底是什么,为什么要使用它?

    r - 如何在不绘制原始数据的情况下向 ggplot 添加图例?

    python - python (Scikit-Learn) 和 R (e1071) 的精度不同

    c# - 计算给定距离、方位、起点的端点