How to get an RGB bitmap with UTM coordinates

I have a three layer raster with red, green and blue channel values. I can build the image using raster::plotRGB , but I need to add axes with UTM coordinates. Coordinates can be added using axes=TRUE , but they float in space and look bad. I would like to get the coordinates, because they are displayed on graphs created using the raster plot method, or even better when they appear when using rasterVis::levelplot .

Ultimately, I need to create a bitmap with UTM coordinates, a scale bar, and a north arrow. This needs to be done using the plotRGB function in the raster R package or something with similar functionality, since I need to assign the color of each pixel manually (without color ramps).

+6
source share
1 answer

This is an ancient post, but it is a good question, so I will give him an answer.

I will give an example of how this can be done using rasterVis::levelplot using the 3-channel R-raster data that comes with raster .

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

Building 3-channel RGB rasters using levelplot

Create an empty raster with the same dimensions and degree as the brick.

 r <- raster(b) 

Calculate the hexadecimal colors corresponding to the values ​​of the RGB channels, and force to factor .

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

Assign these factor values ​​to raster cells.

 r[] <- cols 

Plot with levelplot , extracting hexadecimal colors from cols levels and passing them to col.regions .

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

enter image description here

Adding the north arrow and scale

For the north arrow and scale, we will look at @ 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 will leave this for you to calculate the true distance (passed to grid.text ) associated with the width in map units of the rectangles.

enter image description here

+1
source

All Articles