在R中将路径/路线绘制为GeoTiff

4
我正在使用geom_path函数在R中绘制一些路径。 我希望将我正在绘制的数据转换为GeoTiff(其中包括投影和纬度经度角的地理空间组件),以便我可以导入到NASA WorldWind中。
我引用的工件在这里可用:
- ne_110m_admin_0_countries - HYP_LR_SR_W 我制作了一个非常简单的示例来说明我的情况和我的目标。
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)

这将产生以下图表:

enter image description here

下一步我想要做的是将生成的图形输出为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) 
1个回答

4
我认为没有一种直接的方法可以将ggplot对象(即ggproto)强制转换为RasterStack对象。我不确定以下解决方案是否满足您的要求,但您可以将其视为解决方法:
  1. 使用ggsaveggplot图保存为图像
  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格式。唯一缺失的是GeoTiff的地理空间部分。您有什么想法可以从pathPlot本身中提取它,或者使用“route”数据结构将其添加到GeoTiff中吗? - mainstringargs
你是指这个信息吗:ggplot_build(pathPlot)?你可以使用ggplot_build(pathPlot)$data[[2]]来获取x-y数据。 - Taher A. Ghaleb
我不明白你所说的“...只获取图形部分 - 而不包括所有轴”的意思。 - Taher A. Ghaleb
GeoTiff中包含了来自绘图的坐标轴和标签。我只想要绘图的图像部分(地球栅格和线条)。 - mainstringargs
@main(字符串[] args)我明白你的意思。我已经更新了我的答案。 - Taher A. Ghaleb
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接