I've been trying to use R to download google maps images as rasters, then convert them into GeoTIFF files which can be imported as a geo-referenced background layer in GIS programs.

I've been searching for hours now, but I can't seem to figure out how to save a colored geotiff file. I can make Geotiff files which contain only the raster data with no saved color maps, but I've been unable to figure out how to add color maps to the GeoTIFFs.

Here's my code so far:

## Install and load required packages

## Get region of interest
region <- c(11.03,12,-1.1,-0.04)
e = extent(region)

## Download google maps image of region
map <- gmap(e,rgb = T)

## Plot map

## Write a geotiff raster (does not contain RGB info!)
writeRaster(x = map,
            filename = 'GoogleMap.tif',
            format = 'GTiff',
            overwrite = T

The resulting raster can by imported by other programs as a raster layer, but the colormap is undefined.

And ideas on how to add RGB info to the resulting geoTIFF?

Heres a link to an example GeoTIFF which works in the GIS software I'm using

Perhaps I am misunderstanding what a geotiff is. I assumed a geotiff was just a TIFF file with georeferencing metadata... but perhaps is there a difference between geotiffs and just normal tiff with georeferenced metadata?

I'm thinking this because the dismo::gmap function outputs georeferenced gif files which do exactly what I need, as they can be read by normal image viewers and they also can be plotted correctly in GIS software, unlike the geotiffs which I made, which cannot be read by image viewers.

  • Welcome. This post from SE may be somewhat helpful: stackoverflow.com/questions/33537599/…
    – MaryBeth
    Commented Mar 21, 2016 at 16:11
  • What do you mean by "colormap is undefined"? If I load the 'GoogleMap.tif' into ArcGIS and set the strech of the 3 bands to min-max, I have a perfect RGB map.
    – Iris
    Commented Mar 21, 2016 at 16:18
  • Viewing the image in Ubuntu's default image viewer comes up with a transparent rectangle, and importing the image into the GIS program SMART (a conservation management tool), yield's no image. However, I've seen other GeoTIFF images which do display correctly in both. I'll post a link to a geotiff which I'm trying to reproduce right now which also displays in both.
    – user57443
    Commented Mar 21, 2016 at 16:25
  • Check the class of "map", if it is not a stack or brick then there will not be multiple bands representing the RGB composite. The geotiff format supports single band and multiband. For something like aerial photography you need an RGB composite and not a colormap. The colormap model is for nominal data using a single band or an multiband composite where the classes need to mapped across bands. Commented Nov 8, 2018 at 21:59

1 Answer 1


I wasn't able either to export with raster, but an alternative would be to use writeJPEG() from package jpeg. Note that:

  • writeJPEG() expects an array as input: using as.array(r) does the trick
  • writeJPEG() expects RGB values between 0-1. In my case, reading with raster, they were 0-255, so just rescale!

Your example didn't work for me, but here is one, using the R logo, importing it with raster() adding some (truly beautiful) art, and exporting to jpg:


r <- stack(system.file("img","Rlogo.jpg",package="jpeg"))

r$Rlogo.1 <- getValues(r$Rlogo.1)/2


writeJPEG(as.array(r)/255, "out.jpeg")
  • You do not need to rescale the data. This is where looking at function arguments is useful. The scale argument in raster::plotRGB let's you define what the expected scale range is within the specific 3 band composite. The default is 8-bit (255) but, if your data is in a different bit range you can just define the expected maximum value (eg., OLI unsigned 16-bit scale=65535). You could also avoid coercing to a matrix by just writing out a tiff format. It supports multiband, LZW compression and is less "lossless" than jpeg. See raster::writeRaster. Commented Apr 12, 2021 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.