0

here you have my data https://www.eea.europa.eu/data-and-maps/data/corine-land-cover-accounting-layers. I downloaded "CLC2000 Accounting Layer " file and "CLC2018 Accounting Layer" file. I import the raster file from 2000:

clc00 <- raster("CLC2000ACC_V2018_20.tif")
clc00
class      : RasterLayer 
dimensions : 46000, 65000, 2.99e+09  (nrow, ncol, ncell)
resolution : 100, 100  (x, y)
extent     : 9e+05, 7400000, 9e+05, 5500000  (xmin, xmax, ymin, ymax)
crs        : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs 
source     : CLC2000ACC_V2018_20.tif 
names      : CLC2000ACC_V2018_20 
values     : 111, 999  (min, max)

then I do the same with the file from 2018. But when I want to list a file here comes the problem. I rename the two .tif files I want to make a list with ("abcCLC2000ACC_V2018_20.tif" and "abcCLC2018ACC_V2018_20.tif")

rlist <- list.files(pattern="abc") 
rlist
 [1] "abcCLC2000ACC_V2018_20.tif" "abcCLC2018ACC_V2018_20.tif"
import <- lapply(rlist,raster)
import
[[1]]
class      : RasterLayer 
dimensions : 46000, 65000, 2.99e+09  (nrow, ncol, ncell)
resolution : 100, 100  (x, y)
extent     : 9e+05, 7400000, 9e+05, 5500000  (xmin, xmax, ymin, ymax)
crs        : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs 
source     : abcCLC2000ACC_V2018_20.tif 
names      : abcCLC2000ACC_V2018_20 
values     : -32768, 32767  (min, max)


[[2]]
class      : RasterLayer 
dimensions : 46000, 65000, 2.99e+09  (nrow, ncol, ncell)
resolution : 100, 100  (x, y)
extent     : 9e+05, 7400000, 9e+05, 5500000  (xmin, xmax, ymin, ymax)
crs        : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs 
source     : abcCLC2018ACC_V2018_20.tif 
names      : abcCLC2018ACC_V2018_20 
values     : -32768, 32767  (min, max)

And I get two files with different values ranges! I don't know why! Any guess?

1
  • when I import the two files separatly with raster() I get a range of values between 111 and 999, while when I list them I get a range like -32768, 32767. I can't figure out why Commented Jan 5, 2022 at 16:03

1 Answer 1

1

The GDAL library uses the XML "sidecar" files (specifically, I think, the .aux.xml files) for meta data. If they don't have the same name as the TIFF, or if they aren't in the same folder, then they won't get found. Then GDAL won't understand that the TIFF is categorical.

More info here:

https://gdal.org/drivers/raster/gtiff.html#raster-gtiff

Not sure why this info can't appear in TIFF blocks within the .tif file, but hey ho, so many file standards...

So if you want to read in a number of TIFF files in a loop, make sure you only loop over the .tif files, and not any of the sidecar files, but make sure the .tif.aux.xml files are there in the same place with the same name-roots as the .tif files:

> rlist <- list.files(pattern=".tif$")
> rlist
[1] "CLC2000ACC_V2018_20.tif" "CLC2018ACC_V2018_20.tif"
> import <- lapply(rlist,raster)
> import  
[[1]]
class       : RasterLayer 
dimensions  : 46000, 65000, 2.99e+09  (nrow, ncol, ncell)
resolution  : 100, 100  (x, y)
extent      : 9e+05, 7400000, 9e+05, 5500000  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
data source : /mmfs1/storage/users/rowlings/LC/CLC2000ACC_V2018_20.tif 
names       : CLC2000ACC_V2018_20 
values      : 111, 999  (min, max)
attributes  :
        ID  COUNT CLC_CODE              LABEL1       LABEL2
 from: 111 777883      111 Artificial surfaces Urban fabric
 to  : 999  40471      999        999 (Nodata) 999 (Nodata)
                  LABEL3   R   G   B      RED GREEN     BLUE
 Continuous urban fabric 230   0  77 0.901961     0 0.301961
            999 (Nodata) 255 255 255 1.000000     1 1.000000


[[2]]
class       : RasterLayer 
dimensions  : 46000, 65000, 2.99e+09  (nrow, ncol, ncell)
resolution  : 100, 100  (x, y)
extent      : 9e+05, 7400000, 9e+05, 5500000  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
data source : /mmfs1/storage/users/rowlings/LC/CLC2018ACC_V2018_20.tif 
names       : CLC2018ACC_V2018_20 
values      : 111, 999  (min, max)
attributes  :
        ID  COUNT CLC_CODE              LABEL1       LABEL2
 from: 111 791482      111 Artificial surfaces Urban fabric
 to  : 999  40471      999        999 (Nodata) 999 (Nodata)
                  LABEL3   R   G   B      RED GREEN     BLUE
 Continuous urban fabric 230   0  77 0.901961     0 0.301961
            999 (Nodata) 255 255 255 1.000000     1 1.000000

2
  • So if there is no way to apply the raster function to a list, how can I import both .tif file and relative sidecar file? Commented Jan 5, 2022 at 17:04
  • You don't "import" both. When you do raster("foo.tif") the underlying code will read the data from the foo.tif file and the metadata from foo.tif.aux.xml if it finds it to create an R object with all the right info in it. If you want to apply it to a list, only put the .tif files in the list and make sure the .aux.xml files exist, are there, and have the same name-root.
    – Spacedman
    Commented Jan 5, 2022 at 17:09

Your Answer

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

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