0

The idea is to convert some raster files (39 fire occurrences) into vector points (.shp), allowing the use of the extract() function in the terra package to relate the occurrence points to another raster file, e.g., land use, temperature, solar radiation. Is there a better way to do this? The only strategy I thought of was to convert it into a point file.

The size of a single raster file is about 85 MB, and after using the as.points() function, the vector file is about 5.8 GB (318 GB for all 39 files). The rbind does't work, memory crash.

Here's what I have so far:

library(terra)

arquivos <- list.files('Matriciais', pattern = '*.tif$', full.names = T)
matriciais <- sapply(arquivos, rast)

dir.create('Vetoriais', showWarnings = F)

for (i in seq_along(matriciais)) {
  pontos <- as.points(matriciais[[i]])
  
  nome <- basename(arquivos[[i]])
  ano <- sub('.*_(\\d{4})\\.tif$', '\\1', nome)
  
  pontos$ID <- seq_len(nrow(pontos))
  
  pontos$Ano <- as.numeric(ano)
  
  pontos$Mes <- values(matriciais[[i]])
  
  writeVector(pontos, file.path('Vetoriais', paste0('Pontos_Focos_', ano, '.shp')), overwrite = T)

  print(paste0('Processo do ano ', ano, ' concluído'))
}

vetores <- do.call(rbind, lapply(list.files('Vetoriais', pattern = '*.shp$', full.names = T), vect))
5
  • Do your raster have NA values where there no fire occurred? If yes, make sure na.rm = TRUE option is set. Do the raster have only two values like yes/no for occurence? If that's the case, filter out only those cells which are relevant to vectorization. Commented Nov 1, 2024 at 17:02
  • I forgot to mention that the raster values range from 0 to 12, with each number corresponding to a month of the year in which fire occurrences were recorded. A value of 0 indicates no occurrences. However, I am using a statistical method known as Survival Analysis, so unfortunately, the non-occurrences need to be taken into account. Commented Nov 1, 2024 at 17:41
  • 1
    Welcome to SO. When asking a question, please include a minimal, self-contained, reproducible example. That is, do not refer to your actual data, but create some example data with code (see the questions on this site and in the R help files). The better way is to avoid creating points. But it is not possible to answer without example data that resemble your actual data. Commented Nov 2, 2024 at 8:57
  • @LTyrone, I'm using "survival" package on RStudios to run the survival analysis. Commented Nov 5, 2024 at 18:18
  • @RobertHijmans, thank you for the tip! My apologies for not including a reproducible example. I will make sure to do that next time. Regarding the question below, I decided to resample the rasters to a 300 x 300 resolution instead of keeping the 30 x 30, then the point files became lighter. Commented Nov 5, 2024 at 18:24

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.