【问题标题】:Writing a Path/Route Plot as a GeoTiff in R在 R 中将路径/路线图编写为 GeoTiff
【发布时间】:2019-05-15 05:29:56
【问题描述】:

我正在使用 geom_path 函数在 R 中绘制一些路线。我希望做的是将我正在绘制的数据转换为 GeoTiff(其中包括用于投影和纬度长角的 GeoSpatial 组件)我可以导入到NASA WorldWind

我引用的工件可在此处获得:

我做了一个非常简单的例子来说明我所拥有的以及我正在尝试做的事情:

library(rgdal)
library(ggplot2)
library(png)
library(raster)
library(tiff)


wrld <- readOGR("data" , "ne_110m_admin_0_countries")
base <- ggplot(wrld, aes(x = long, y = lat))


myDataFrame <- data.frame(Name=c("Object1","Object1","Object1","Object2","Object2","Object2"), lat=c(34,30,25,65,32,16), long=c(-118,-120,-114,-63,-108,-110)) 


route <- c(geom_path(aes(long, lat, group = myDataFrame$Name), colour = "#ffff00", size = 2, data =
myDataFrame, alpha = 0.75,
lineend = "round"))


earth <- readTIFF("HYP_LR_SR_W.tif")


pathPlot <- base +  annotation_raster(earth, -180, 180, -90, 90) + route
plot(pathPlot)

这会产生这个情节:

我想做的下一步是将结果图输出为 GeoTIFF(我可以将其导入 WorldWind)。

一旦我有一个堆叠的栅格,我想知道以我想要的格式创建 GeoTIFF 的命令,但我不知道如何将它们连接在一起以从路由到仅包含图像本身的 GeoTIFF其中包括地理空间组件:

ggsave(plot=pathPlot, "pathPlot.tiff", device = "tiff")
stackedRaster <- stack("pathPlot.tiff")
xRange <- ggplot_build(pathPlot)$layout$panel_params[[1]][c("x.range")] 
yRange <- ggplot_build(pathPlot)$layout$panel_params[[1]][c("y.range")]     
extent(stackedRaster) <- extent(xRange$x.range[1],xRange$x.range[2], yRange$y.range[1],yRange$y.range[2]) 
projection(stackedRaster) <- CRS("+proj=longlat +datum=WGS84") 
writeRaster(stackedRaster, "myGeoTiff.tiff", options="PHOTOMETRIC=RGB", datatype="INT1U", overwrite=TRUE) 

【问题讨论】:

    标签: r gis raster tiff geotiff


    【解决方案1】:

    我认为没有一种直接的方法可以将ggplot 对象(即ggproto)强制转换为RasterStack 对象。我不确定以下解决方案是否能满足您的要求,但您可以将其视为一种解决方法:

    1. 使用ggsaveggplot 绘图保存到tiff 图像中
    2. 使用stack为保存的图像创建一个RasterStack对象
    3. 使用writeRasterRasterStack 对象保存为GeoTiff 图像

    以上步骤的实现如下:

    # This is your pathPlot
    pathPlot <- base +  annotation_raster(earth, -180, 180, -90, 90) + route
    
    # Remove the margins from the plot (i.e., keep the earth raster and the routes only)
    pathPlot <- pathPlot + 
      theme(    
            axis.ticks=element_blank(), 
            axis.text.x=element_blank(), 
            axis.text.y=element_blank(), 
            axis.title.x=element_blank(), 
            axis.title.y=element_blank(),
            plot.margin = unit(c(0, 0, 0, 0), "null"),
            legend.position = 'none'
           ) +
           labs(x=NULL, y=NULL)
    
    # Save the plot
    ggsave(plot=pathPlot, "pathPlot.tiff", device = "tiff")
    
    # Create a StackedRaster object from the saved plot
    stackedRaster <- stack("pathPlot.tiff")
    
    # Get the GeoSpatial Components
    lat_long <- ggplot_build(pathPlot)$layout$panel_params[[1]][c("x.range","y.range")] 
    
    # Supply GeoSpatial  data to the StackedRaster 
    extent(stackedRaster) <- c(lat_long$x.range,lat_long$y.range)
    projection(stackedRaster) <- CRS("+proj=longlat +datum=WGS84")
    
    # Create the GeoTiff
    writeRaster(stackedRaster, "myGeoTiff.tif", options="PHOTOMETRIC=RGB", datatype="INT1U")
    

    这是生成的 GeoTiff 图像:

    希望对您有所帮助。

    【讨论】:

    • 这确实有助于将文件本身输出为 geotiff。缺少的一大块是 GeoTiff 的 GeoSpatial 部分。关于如何从 pathPlot 本身或使用“路线”数据结构将其添加到 GeoTiff 中的任何想法?
    • 您的意思是这个信息:ggplot_build(pathPlot) 吗?您可以使用ggplot_build(pathPlot)$data[[2]] 获取 x-y 数据
    • 啊。是的。很棒的小费。我想我得到了纬度/经度范围: xRange
    • 嗯,粘贴得不是很好。我会将它添加到主帖中——我认为突出的问题是只抓住情节的“图像”部分——没有所有的轴。知道该怎么做吗?
    • 我没明白你所说的 的意思......只抓住情节的“图像”部分——没有所有的轴”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2012-04-05
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多