1

I am trying to create a map using tm_shape on r

While the process is relatively easy with regular .tif rasters the process seems more complicated when I try to use a raster from a digitized historic map to be the background (historic.tif).

When I load the historic.tif file on QGIS I can then plot my other shapefiles (roads, cities) and make the map. However on r I can't load the file. I get the following message :

This historic.tif is an old map of Liberia that I found in the archives and digitized using QGIS and then saved as a .tif file.

Error in .local(.Object, ...) :

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file. (file does not exist)

library(sf)
library(raster)
library(dplyr)
library(spData)
library(spDataLarge)
library(rgdal)
library(rgeos)

library(tmap)

historic_map <- raster("~/historic.tif")

Does anyone has a solution to propose?

7
  • "File does not exist" makes me think the file does not exist. What does file.exists("~/historic.tif") say?
    – Spacedman
    Sep 25, 2019 at 11:52
  • Hi @Spacedman it says [1] TRUE Sep 25, 2019 at 11:54
  • Can you load any TIF files? Is this one corrupt? What does file.info("~/historic.tif") say?
    – Spacedman
    Sep 25, 2019 at 12:02
  • Yes I can load other raster tif files. Sep 25, 2019 at 12:25
  • Can you share this tif file somewhere? Otherwise I think we're stuck...
    – Spacedman
    Sep 25, 2019 at 12:47

1 Answer 1

0

There seems to be nothing wrong with the file and I can't reproduce the error except by trying to access a file that doesn't exist. I have the file in the same folder that I start R from (ie ./) and get this:

> file.size("./liberia_concessions.tif")
[1] 7747782

> lc = raster::raster("./liberia_concessions.tif")
> lc
class      : RasterLayer 
band       : 1  (of  4  bands)
dimensions : 1117, 1732, 1934644  (nrow, ncol, ncell)
resolution : 0.00450692, 0.00450692  (x, y)
extent     : -13.00627, -5.200282, 3.311637, 8.345867  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : /nobackup/rowlings/Downloads/liberia_concessions.tif 
names      : liberia_concessions 
values     : 0, 255  (min, max)

and then:

> library(raster)
Loading required package: sp
> lcs = stack("./liberia_concessions.tif")
> plotRGB(lcs)
Warning message:
In couldBeLonLat(x) : CRS is NA. Assuming it is longitude/latitude

enter image description here

6
  • do you know the equivalent of plotRGB(lcs) with tm_shape so I keep the original colors of the map. When I run map= tm_shape(lcs) + tm_raster() I get the map with colors from light orange to red however I would like to keep the original colors. I guess it has something to with the bands. Sep 26, 2019 at 10:06
  • So is it not giving "File not found" errors now? What did you change? We need to answer the question of why you were getting "File not found" even though R said the file exists.
    – Spacedman
    Sep 26, 2019 at 11:49
  • Asking how to show a three-band raster in tmap is a new question (and you can answer this yourself by reading the help for tm_raster).
    – Spacedman
    Sep 26, 2019 at 11:50
  • So what works best is to import the raster file as lcs = brick("~path/liberia_concessions.tif"). Then change the crs crs(lcs) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" Sep 27, 2019 at 12:32
  • Finally to plot the map respecting the original colors I did map = tm_shape(lcs) + tm_rgb() . It is as simple as that. Sep 27, 2019 at 12:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.