hugh-allan
hugh-allan

Reputation: 1370

R - Import then export multiband RGB aerial image .tif

I want to import an aerial image in .tif format, and then export in the same colour format. I imagine this is a simple task but I can't find a way to do it. The original files are coloured when viewed in Windows Photo Viewer and Google Earth, but the exports are black and white.

Ultimately, I want to crop/merge multiple images to create a single aerial image map from approx 6 tiles. Of the 6 tiles, I am only interested in about 30% of the area (the bits over the water, plus a ~20 m buffer), so the idea is to cut down on image size by keeping the parts I want as a single file, rather than importing all 6 tiles. I can open one .tif in Google Earth as a coloured aerial image, which is what I want for my custom-boundary map/image.

At the moment, I am struggling to import then export a single .tif in the same Google Earth-readable coloured image format. I'm importing the files into R using raster, and have tried exporting using writeRaster, but the images are black and white when viewed in GE. I believe this might mean the image is rendering only a single (RGB) layer of the image? However, plotRGB() in R is able to plot it in colour.

You can download the file I'm working with from my Google Drive, or search for it on Elvis (satellite image data from inside the Australian Capital Territory, Australia, approx -35.467437, 148.824043). Thanks for any help or direction to some instructions.

This is where I'm at so far...

# import and plot the tif - plots nicely in colour
brick('ACT2017-RGB-10cm_6656073_55_0001_0001.tif') %>% 
  plotRGB

This is what I see from plotRGB(), and also when I open the original in Google Earth (this is the desired output colour). enter image description here

# export
brick('ACT2017-RGB-10cm_6656073_55_0001_0001.tif') %>% 
  writeRaster('my_output.tif')

# then import the export
brick('my_output.tif') %>% 
  plotRGB

my_export.tif plots in colour in R, but black and white in Google Earth.

Upvotes: 1

Views: 906

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47156

Here is how you can do that with terra (the replacement for raster). For this example to work well, you need version 1.4-1 or higher; currently the development version, that you can install with install.packages('terra', repos='https://rspatial.r-universe.dev')

library(terra)
f <- 'ACT2017-RGB-10cm_6656073_55_0001_0001.tif'
r <- rast(f)
plot(r)

Since this is a "RGB" image, there is no need to call plotRGB explicitly

I create two sub-images for this example

ex <- ext(665000, 665500, 6073000, 6073500)
x <- crop(r, ex)
ey <- ext(665500, 666000, 6073500, 6074000)
y <- crop(r, ey)

They still look good with

plot(x)
plot(y)

Now merge them

m <- merge(x, y)

After merge, the RGB, channels are lost and need to be redeclared

RGB(m) <- 1:3

And you can write to disk

z <- writeRaster(m, "test.tif", overwrite=TRUE)
plot(z)

enter image description here

Upvotes: 3

Related Questions