Questions tagged [terra]

This tag refers to the R Terra package. For questions about NASA's Terra satellite, use the tags of its censors like Aster, Ceres, or Modis.

Filter by
Sorted by
Tagged with
1 vote
0 answers
5 views

Applying aggregate function in terra to multiple raster files in a folder

I have 330 raster files in a folder showing landcover type. They are of a 10 m resolution and each file is approximately 3 MB in size. I want to change the resolution of all of the files to 100 m and ...
  • 29
1 vote
0 answers
14 views

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 ...
-1 votes
2 answers
21 views

Why doesn't terra::spin() work in R?

I am using a map that is rotated sideways. Terra has a function to address this problem, called spin library(terra) maize_requirement_2016 <- rast("./data/raw/BW_irrig_2016.nc") |> ...
0 votes
1 answer
26 views

Is there a limit to how many SpatRasters can be handled by terra::compareGeom() in R?

I want to ensure that my 8 SpatRasters have the same extent so that I can combine them as layers in the same SpatRaster. When I use compareGeom() with more than 3 SpatRasters, I get this error: Error: ...
  • 505
0 votes
1 answer
80 views

Extracting values from one raster based on condition in another raster and distinguished by polygons in a vector file

I have one raster file on land cover (30x30m pixels), one raster file on soil organic carbon (SOC) (250x250m pixels), and one vector file with contiguous polygons. I use ArcGIS 10.5.1. The two raster ...
  • 125
0 votes
0 answers
16 views

Use 'project' function in {terra} raster package, treat NA as 0s

I'm trying to resample/project a high res raster, using a low res raster as a template library("terra") high_res <- rast(ncol = 2, nrow = 2, vals = c(1, NA, NA, NA)) low_res <- rast(...
1 vote
1 answer
33 views

Select cells from different raster layers based on an index raster

I have a raster that contains correction indexes and another raster with each layer representing the index and the layer value, the correction factor for a region. So wherever the index value is 1, I ...
  • 2,373
0 votes
1 answer
27 views

R: Wrong transformation from SpatRast (terra) to stars object

When I transform my SpatRast object to a stars object, suddenly the the location of my origin changes. I need the location of the origin to be an exact point (376491.4,5875118.5, EPSG:25833), as I am ...
  • 1
0 votes
1 answer
36 views

terra analogous of randomPoints?

I'm currently using the dismo function randomPoints to sample background points (bgp) over a raster. This function has an argument called p, which is used to avoid sampling bgp over cells that already ...
0 votes
3 answers
95 views

How to extract the frequency of raster values from polygons in terra?

I have raster layer of days in which the temperature exceeds the threshold of 0 degrees, and a shapefile of the states of United States. I would like to extract the frequency of the pixel values per ...
  • 806
1 vote
1 answer
31 views

Use *jpg to create *jgw file using R

In my example, I have a single dji image in *JPG: library(terra) single.image <-rast("https://github.com/Leprechault/trash/raw/main/DJI_0274.JPG") plotRGB(single.image, r = 3, g = 2, b = ...
1 vote
2 answers
145 views

Renaming and exporting a list of rasters using terra

I've a list of hundreds of rasters that I've renamed, but when I export using writeRaster, the filenames have not changed. For example: library(terra) library(tidyverse) # all the of the packages I ...
  • 1,360
1 vote
2 answers
78 views

Change raster cells non-NA values based on the longitude using R terra

I would like to change all the non-NA cell values based on the cells longitude. For instance, any cell that had a non-NA value and a longitude greater than x would have the value replaced with y. I ...
  • 163
1 vote
3 answers
56 views

Intersecting objects and converting it to simple features so I can use 'raster', R

This question is connected to: How to create a regular, square grid, and find centroid by factors using R I have a data frame which have three columns: (1) the "scientificName" column ...
1 vote
1 answer
48 views

How to use terra::predict with mixed model?

I developed a model from a dataset where zmean and zpcum5 are continuous values, and V6 is categorical (evergreen, mixed, deciduous). Lm3c.ranslope <- lmer(plotVol_sqrt ~ zmean + zpcum5 + I(zmean * ...
  • 505
1 vote
1 answer
115 views

How to create a regular, square grid, and find centroid by factors using R

I want to create a regular, square grid of .5x.5 degree around my sampling points and find latlong centroid by factors. I have a data frame which have three columns: (1) the "scientificName" ...
1 vote
1 answer
88 views

How to remove NA value of legend when using R tidyterra?

I downloaded the MCD12Q1 data (land cover and category type data) by MODIStsp package, and I want to plot it using tidyterra package. For example, library(terra) library(tidyterra) library(tidyverse) ...
  • 161
0 votes
1 answer
40 views

R equivalent to the ArcGIS's Shape Metrics Tool

I am trying to calculate the compactness of (vector) polygons in using R. My main reference to do so is this paper. There used to be an ArcGIS toolbox (Shape Metrics) to calculate these metrics. ...
0 votes
1 answer
39 views

Calculate argmax and second argmax of a RasterStack in R

I need to calculate the argmax (index of the maximum) and index of the second maximum of a RasterStack in R. That is, for each pixel of the image, I need to evaluate in which index (layer) is the ...
  • 900
1 vote
1 answer
63 views

How to maintain existing layer names from input rasters when applying `terra::merge()` function?

Layer names are carried over from input rasters when I use terra::mosaic(), but not when I use terra::merge(). terra::merge() currently results in a numbered list prefixed by "lyr". E.g., &...
  • 505
0 votes
0 answers
90 views

Making map with marginal graphics like rasterVis::levelplot(margin=T)

I love rasterVis::levelplot(margin=T) because its marginal graphics show the column (x) and row (y) summaries of the Raster object. I think it is very helpful to visualize the raster. Package ...
  • 161
1 vote
1 answer
39 views

Assigns geographical locations to the pixels in isolate drone image in R

In my example, I have a single dji image in *JPG: library(terra) single.image <-stack("https://github.com/Leprechault/trash/raw/main/DJI_0274.JPG") plotRGB(single.image, r = 3, g = 2, b =...
0 votes
1 answer
125 views

Errors reassigning CRS of SpatRaster using terra::projectRaster and raster::project

I have the following SpatRaster object called pred_th: dimensions : 162, 87, 1 (nrow, ncol, nlyr) resolution : 0.08333333, 0.08333333 (x, y) extent : 43.25, 50.5, -25.58333, -12.08333 (xmin, ...
1 vote
1 answer
80 views

Create several raster layers based on one layer

The terra::app() function accepts one or more layers and a function, and usually (?) returns just a layer with results. However, how to apply a function to one layer that returns more than one layer? ...
  • 932
0 votes
1 answer
47 views

How to import DEM, with R Terra package, from a list containing two elements (list w/ header and matrix)?

Using an example from Geocomputations in R, I'm trying to import a DEM (from RSAGA package) from a 2-element file (header and list of elevations). It works with the Raster package, but I'm having ...
  • 505
1 vote
1 answer
38 views

Converting large number of NetCDFs to single raster using R

There are ~87,500 NetCDF files of a global dataset that must be stacked and the values averaged to create 1 raster. My machine does not like this... I've written nested for loops that, despite ...
1 vote
1 answer
198 views

Aggregate raster in R and get count of contributing pixels

I have a raster with some NULL values. I need to aggregate the raster to a coarser one, and get a count of the pixels that are being aggregated (for every output pixel), excluding the NULLs. I'm ...
  • 31
0 votes
0 answers
236 views

Why is coord_sf needed to plot a geom_raster with ggplot2?

Why is coord_sf() needed to plot a geom_raster in ggplot2 and what exactly does coord_sf() do? According to ggplot2's Map project's documentation: coord_map projects a portion of spherical earth onto ...
  • 750
1 vote
0 answers
38 views

Getting equal number of cells within rasterstack using R

I have a raster_stack with 3 rasters. I made them the same resolution, extent, projection so that I could combine them into the stack. So, I expected each raster to have the same number of pixels. The ...
1 vote
1 answer
191 views

How to efficiently get the intersection between vector and raster in R?

What is an effective way to clip (intersect) a vector layer by raster values? Assume we have the following two layers: vec - a large vector dataset with polygons ras - a raster dataset which works as ...
0 votes
1 answer
124 views

Aggregate raster using gaussian smoothing as a custom function/Units of the sigma

I am trying to aggregate a raster using a custom function and the terra package. For the custom function I am using a Gaussian smoothing. When I try to run the aggregate function I am getting this ...
  • 675
0 votes
1 answer
143 views

Confirm version of gdal that sf is using in R

In the {terra} package I can do this: terra::gdal() To confirm that {terra} has been linked to the correct version of gdal on my operating system. Is there a similar command for {sf} ?
2 votes
3 answers
304 views

Create empty raster with dimensions, pixels size and CRS of another raster in R

I am trying to create an empty raster layer and I want this empty raster to have the same dimensions, pixel size and CRS based on another raster, but no pixel values. I am getting errors when I try to ...
  • 675
1 vote
1 answer
73 views

Modify raster coordinates (wrong projection) using stars or terra

I downloaded a raster image from the following address, which corresponds to rivers and streams in the Barcelona area. https://geoserveis.icgc.cat/icgc_geotecnicbcn25m/wms/service?REQUEST=GetMap&...
  • 137
4 votes
1 answer
199 views

R Focal Function - Terra vs Raster

I'm not sure why the focal function I've been using in Raster does not work with Terra. This simplified example uses a custom function to apply the focal filter only to pixels with a value of 0 and ...
2 votes
0 answers
63 views

How to address NA in pixel regression (local computation) with R terra package

For example, I have five NDVI TIFFs and five precipitation TIFFs. I used the code provided by Robert (https://stackoverflow.com/questions/63831287/extracting-p-values-from-lineair-regression-on-raster-...
  • 161
1 vote
1 answer
120 views

Plotting hillshade with terra

I have a hillshade in the form of a SpatRaster object. When I plot it like this: plot(hs.rast, col = grey(c(0:100)/100), legend = F), the plotting function seems to apply some sort of smoothing to the ...
1 vote
1 answer
70 views

Dimensions and extent changed after rast from 'xyz' data.frame

s #oringinal raster s #check dimensions and extent df <- s %>% as.data.frame(xy=T) #convert to df t <- rast(df,type='xyz')...
  • 161
0 votes
1 answer
112 views

terra R package resample of non-empty raster by 'sum' produces empty raster

I have a raster x which is at a resolution of 100m and pixel value is its area in sqkm is suitable for an action, see below class : SpatRaster dimensions : 769, 716, 1 (nrow, ncol, nlyr) ...
  • 790
0 votes
1 answer
413 views

Creating a catalog from raster files with terra in R

I would like to first explain what I aim to do. I am used to use the R package lidR and I find very useful the comand readLAScatalog, which creates a catalog, "a representation of a collection of ...
0 votes
0 answers
83 views

Apply function at grid cell level across layers of a raster file

I have a netcdf raster object with multiple layers (time: 30 years * 12 month = 360 steps). The raster is constituted of multiple grid-cells (say 1000). I want to linearly detrend each month ...
0 votes
1 answer
122 views

Monte Carlo simulation

I have an equation to calculate z from multiplying several rasters and numbers as seen below: z <- a*b*c*d*e The raster layers and values are as below: library(terra) set.seed(234) r <- rast(...
  • 363
0 votes
1 answer
708 views

R terra::mask report error ‘Error: [mask] extents do not match’

I have a temperature tif, and I want to extract my study area.But it reports that extents do not match. I want to know how to fix it and why the extents do not match? I think to_be_masked raster's ...
  • 161
1 vote
1 answer
250 views

Extracting variables from grid cells using R

I am trying to extract bioclimatic variables from individual grid cells. So i started loading my shapefile: # I used the Admin 0 - Countries (1:10) dataset from Natural Earth shp <- sf::st_read(&...
3 votes
0 answers
166 views

How to force single value terra::rast to plot as continuous

I have a couple of raster in R. Some have a range of values while others have a single value (all 0, for instance). I want to plot all rasters with the same color scale and legend so I can visually ...
  • 2,373
0 votes
1 answer
462 views

Adding an ID column to a SpatVector in R?

I've been rewriting an R script so it uses terra instead of raster and other packages, and I've managed to do it all except for one painfully simple task - adding an ID column to a SpatVector. My code ...
0 votes
1 answer
56 views

How to set number of columns with stack or brick or spatraster in rasterVis::levelplot?

If I have a spatraster with 6 layers, rasterVis::levelplot will give a 2 rows and 3 columns plot. How can I change it to 3 rows and 2columns? library(rasterVis) library(terra) f <- system.file(&...
  • 161
0 votes
1 answer
287 views

How to export(writeRaster) single raster from multi-band stack/brick/spatraster one by one using R?

library(terra) a=rast(nrows=10,ncols=10,vals=1:100) b=rast(nrows=10,ncols=10,vals=1:100) c=rast(nrows=10,ncols=10,vals=1:100) test <- c(a,b,c) for (i in 1:3) { r=test[[i]] writeRaster(r,...
  • 161
0 votes
1 answer
811 views

R terra: [crop] SpatRaster has no values

I am attempting to use terra::crop() to crop 30 meter NLCD raster data to a bounding box. The final error that R returns is: Error: [crop] SpatRaster has no values despite the raster data containing ...
1 vote
0 answers
109 views

Recursively mosaic rasters by directory (year) in R

I have hundreds of rasters spanning 10 years, files are stored by year (e.g., 2020, 2019, 2018, etc.). I would like to mosaic all the raster files within a directory, move to the next directory and ...
  • 1,360