Questions tagged [terra]

Use this tag for questions related to functions from the terra package for spatial data analysis. For Terra (Luna) blockchain protocol, use terra-blockchain tag instead

is an package for spatial data analysis, especially raster data. The package also has general raster-related functions, such as for creating, reading, manipulating, and writing raster data. terra is written in and is intended to replace the raster package in the future because it is simpler and much faster. Tutorials are available at https://rspatial.org/terra.

Scope of questions

This tag should be used for programming-related questions about the R terra package. Including a minimal reproducible example of raster data in your question will increase your chances of getting a timely, useful answer. Questions should be specific to the functions in the terra package. Questions should not use the r-raster tag unless they relate specifically to the raster (i.e., r-raster tag) package and not just raster spatial data analysis.

Repositories

Maintainer

525 questions
2
votes
0 answers

Is it possible to create an irregular raster in R with terra package?

I have a dataset which represents a global grid with latitude and longitude and the length of each pixel along the x and y axis. These are simply lat/long CRS. Like this head(latlong) x y x.leng y.leng value 80.221 50.854 72334…
Herman Toothrot
  • 1,463
  • 3
  • 23
  • 53
2
votes
0 answers

Parallelizing nested lapply

How best can I parallelize this? year_ras is a spatRaster while sh_fields_base is an sf object. yearStats <- lapply(year_list, function(year){ year_ras <- terra::rast(paste0(datadir, "/Mark_", year, "_Slut.tif")) fields_baseStats <-…
jmutua
  • 290
  • 1
  • 12
2
votes
1 answer

fill NA raster cells using focal defined by boundary

I have a raster and a shapefile. The raster contains NA and I am filling the NAs using the focal function library(terra) v <- vect(system.file("ex/lux.shp", package="terra")) r <- rast(system.file("ex/elev.tif", package="terra")) r[45:60, 45:60] <-…
89_Simple
  • 3,393
  • 3
  • 39
  • 94
2
votes
1 answer

scale_fill_manual factor levels, colours, or order ignored

I am having difficulty creating a map with scale_fill_manual to fill regions on the map based on some property in the region. I am using tidyterra with terra and the tidyverse packages. I am unaware of methods for writing self-contained map files in…
SGE
  • 311
  • 3
  • 10
2
votes
1 answer

reading tif file using terra and raster package gives different results

I am reading a raster file using both terra and raster package library(raster) library(terra) fl_terra <- terra::rast('my_raster.tif') fl_raster <- raster::raster('my_raster.tif') fl_terra class : SpatRaster dimensions : 157450, 327979,…
89_Simple
  • 3,393
  • 3
  • 39
  • 94
2
votes
2 answers

using remote (vsicurl) calculations on sentinel-2 data with the {stars} package?

I'm using rstac to access Sentinel-2 data in a desired bounding box and date range and computing NDVI. This is relatively[*] clean and straight forward for me when using the {terra} package, but I'd like to use the {stars} syntax instead (more on…
cboettig
  • 12,377
  • 13
  • 70
  • 113
2
votes
1 answer

Set raster metadata (band names) in R on disk

I have a large raster on file. It can be read like so: my_rast <- rast("my_large_file.tif") names(my_rast) [1] "value_a" Using R or command-line GDAL I would like to change the name of the layer to something different, without totally re-writing…
TheRealJimShady
  • 777
  • 3
  • 9
  • 24
2
votes
1 answer

rasterize and mask in one step?

Toy data library(terra) template_raster <- rast(xmin=0, xmax=10, ymin=0, ymax=10, crs="+proj=longlat +datum=WGS84 +no_defs +type=crs", resolution = 0.1) values(template_raster) <- c(rep(NA,4000),rep(10,2000),rep(NA,4000)) polygon_to_rasterize <-…
TheRealJimShady
  • 777
  • 3
  • 9
  • 24
2
votes
1 answer

Converting SpatVector objects to data frames for use in ggplot2

I would like to convert SpatVector objects to data frames for use in ggplot2. pkgs <- c("geodata", "raster", "ggplot2", "tidy") lapply(pkgs, require, character.only = TRUE) boundary_GB <- geodata::gadm(country = "GB", path = tempdir(), resolution =…
2
votes
2 answers

Rasterize SpatVect (points) with buffer around SpatRaster

I have a SpatVect consisting of points and I want to rasterize them into a SpatRaster with a given resolution. Is there a way of specifying a function taking in the points that are within a buffer of each raster cell? Many thanks Joao -- Update…
2
votes
1 answer

How to avoid connecting polygon vertices in the wrong direction when projecting a polygon?

I have a polygon that defines the boundaries of a study area. The area is quadrilateral. However, when I convert it from Lambert Conformal Conic projection to WGS84, one connection between two vertices is now drawn from west to east instead of from…
canderson156
  • 1,045
  • 10
  • 24
2
votes
1 answer

Stack / concatenate rasters in a list (terra - spatRaster)

I want to stack a few rasters I have in a list using terra. This used to work very easily in raster because it was possible to stack() the list. However, this is not possible anymore in terra. See the example below: r1 <- raster(nrows = 1, ncols =…
Javier Fajardo
  • 737
  • 1
  • 10
  • 22
2
votes
2 answers

How can I change the ordering of levels for a categorical SpatRaster in terra?

I have two rasters and I would like them to have the same levels and in the same order. However, one of the rasters does not have all the levels that the other does but I have managed to add these levels in using levels(rast) <- all_labels. My issue…
André.B
  • 617
  • 8
  • 17
2
votes
2 answers

Discrepancy between writeRaster of raster and terra package

I am trying to write rasters in asc format using raster and terra R package. I am using the following code library(terra) library(raster) f <- system.file("external/test.grd", package="raster") r1 <- raster(f) plot(r1) writeRaster(r1,…
UseR10085
  • 7,120
  • 3
  • 24
  • 54
2
votes
3 answers

point in polygon using terra package in R

I want to do a point in polygon analysis using the terra package. I have set of points and I want to extract which polygons do they fall in. The below sample data shows it for a single polygon library(terra) crdref <- "+proj=longlat +datum=WGS84" …
89_Simple
  • 3,393
  • 3
  • 39
  • 94
1 2
3
34 35