0

I want to use the tree cover density layer from copernicus (https://land.copernicus.eu/pan-european/high-resolution-layers/forests/tree-cover-density/status-maps/tree-cover-density-2018?tab=download)

I updated the question in order and it's now just reading the same vrt

I downloaded the data for austria and cropped it to vienna. I created a tif and, just for demonstration purposes, created a vrt from that single tif. Both files can be found here: https://drive.google.com/drive/folders/169Gj6X36wIKR2_WJaYAft4XwtV69zmbr?usp=drive_link

Now I am reading that vrt with terra and stars (and by what I know I am not using a star_proxy, so I am getting not only a sample but all the values) and just look at the values. They are very different and probaly the mistake is on my side. I just do not know exactly what I am doing wrong.

library(stars)
library(terra)


# tcd raster vienna ----------------------------------------------------------
path = "~/Desktop/test.vrt"
data_raw_tcd_terra_values = terra::rast(path) %>% terra::values()
summary(data_raw_tcd_terra)

# test        
# Min.   :  0.0    
# 1st Qu.:  0.0    
# Median :  0.0    
# Mean   : 21.5    
# 3rd Qu.: 52.0    
# Max.   :100.0    
# NA's   :2792336 

data_raw_tcd_star_values = read_stars(path) %>% .[[1]] %>% as.numeric() %>% summary()
data_raw_tcd_star_values

# Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#       1       1       1 1018260 3110080 4111302 2792336 
4
  • What are the differences? You've not shown any output. Is it just because the rasters are huge and its only showing samples or values from metadata that's not been updated (typically happens with max/min etc)?
    – Spacedman
    Commented Jun 13, 2023 at 12:27
  • No, its really completely different values. But you are right, I'll update the question!
    – Lenn
    Commented Jun 13, 2023 at 13:40
  • 1
    Something's up with stars for you. I get sensible-looking values with your stars-based code, although its 1 greater than the terra code - terra says min=0, max=100, stars says min=1.0, max=101.0.... Maybe some offset/lookup thing? Upgrade all the packages and check again.
    – Spacedman
    Commented Jun 13, 2023 at 15:32
  • thanks!:) I'll do that and check again
    – Lenn
    Commented Jun 15, 2023 at 7:44

1 Answer 1

1

I get the same values when I do

read_stars("test.vrt")[[1]] |> 
          as.character() |> as.numeric() |> summary()

#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#    0.0     0.0     0.0    21.5    52.0   100.0 2792336 

Note the use of as.character, which is needed to translate number-like factors to numbers.

Compare:

factor(c(1,5,10)) |> as.numeric()
#[1] 1 2 3
factor(c(1,5,10)) |> as.character() |> as.numeric()
#[1]  1  5 10

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.