简体   繁体   English

如何使用UTM坐标获取RGB光栅图像

[英]How to get RGB raster image with UTM coordinates

I have a three layer raster with red, green, and blue channel values in it. 我有一个三层栅格,其中有红色,绿色和蓝色通道值。 I can plot the image with raster::plotRGB , but I need to add axes with UTM coordinates. 我可以使用raster::plotRGB绘制图像,但是我需要添加具有UTM坐标的轴。 Coordinates can be added with axes=TRUE , but they are floating in space and look bad. 可以使用axes=TRUE添加axes=TRUE ,但是axes=TRUE在空间中浮动并且看上去很差。 I would like to get the coordinates as they show up in plots created with the raster plot method, or better yet as they appear when using rasterVis::levelplot . 我想获得坐标显示在使用raster plot方法创建的plot的坐标,或者更好的是使用rasterVis::levelplot时显示的rasterVis::levelplot

Ultimately, I need to create a raster image with UTM coordinates, a scale bar, and north arrow. 最终,我需要创建一个具有UTM坐标,比例尺和向北箭头的栅格图像。 This needs to be done using the plotRGB function in the raster package of R, or something with a similar functionality, as I need to assign the colour of each pixel by hand (no colour ramps). 这需要使用R的raster包中的plotRGB函数或具有类似功能的东西来完成,因为我需要手动分配每个像素的颜色(无色带)。

This is an ancient post, but it's a good question so I'll give it an answer. 这是一篇古老的文章,但这是一个好问题,所以我给它一个答案。

I'll give an example of how this might be done with rasterVis::levelplot , using the 3-channel R logo raster data that comes with raster . 我给这如何可能会做一个例子rasterVis::levelplot ,使用自带的3槽R标志栅格数据raster

library(rasterVis)
b <- brick(system.file("external/rlogo.grd", package="raster"))

Plotting three-channel RGB rasters with levelplot 使用levelplot绘制三通道RGB栅格

Create an empty raster with the same dimensions and extent as the brick. 创建一个具有与砖相同尺寸和范围的空raster

r <- raster(b)

Calculate the hexadecimal colours corresponding to the values of the RGB channels, and coerce to factor . 计算与RGB通道的值相对应的十六进制颜色,并强制转换为factor

cols <- factor(rgb(b[], maxColorValue=255))

Assign these factor values to the raster's cells. 将这些因子值分配给栅格像元。

r[] <- cols

Plot with levelplot , extracting the hexadecimal colours from the levels of cols and passing them to col.regions . 使用levelplot ,从cols的水平中提取十六进制颜色并将其传递到col.regions

levelplot(r, col.regions=as.character(levels(cols)), colorkey=FALSE)

在此处输入图片说明

Adding north arrow and scale bar 添加北箭头和比例尺

For the north arrow and the scale bar, we will look to @OscarPerpiñán's docs . 对于北箭头和比例尺,我们将查看@OscarPerpiñán的docs

levelplot(r, col.regions=as.character(levels(cols)), colorkey=FALSE) +
  layer(SpatialPolygonsRescale(layout.north.arrow(), offset = c(5, 10), scale = 10)) +
  layer({
    xs <- seq(5, 25, by=5)
    grid.rect(x=xs, y=5,
              width=5, height=2,
              gp=gpar(fill=rep(c('transparent', 'black'), 2)),
              default.units='native')
    grid.text(x=xs-2.5, y=8, seq(0, 400, by=100),
              gp=gpar(cex=0.7),
              default.units='native')
  })

I'll leave it up to you to calculate the true distance (passed to grid.text ) associated with the width, in map units, of the rectangles. grid.text您自己来计算与矩形的宽度(以地图单位为单位)关联的真实距离(传递给grid.text )。

在此处输入图片说明

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

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