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
3 votes
2 answers
40 views

Plotting a categorical terra raster with a colour map

I'm trying to plot a categorical terra raster object with an explicit colour for each level, like you'd do with a land cover raster, but running into problems when the raster doesn't have every level ...
user avatar
  • 52.1k
2 votes
2 answers
63 views

What's the most efficient way to calculate line length per raster cell in R?

Problem I'm looking for an efficient, memory friendly to calculate total line (vector data) length per raster cell. The question of line length per cell has already been posed here in 2014 and a few ...
user avatar
  • 164
0 votes
1 answer
47 views

r Terra Mosaic Raster uses huge amounts of memory and takes too long - any alternatives?

I built a process using google earth engine code editor to output a raster of urban land cover in Wisconsin. Google exports these as 210 shard which are geotiffs ranging from 2-100mb in size. Most are ...
user avatar
0 votes
0 answers
16 views

Terra: Setting the right resolution when transforming data frame to SpatRaster

I have a data frame which cell sizes aren't regular due to some statistic analysis and filtering, but for presentation purposes I need to transform the data frame back to a SparRaster. I don't want to ...
user avatar
  • 199
0 votes
1 answer
25 views

terra R package functions error that file exists and needs overwrite=T

For the past 2 days I am trying to use the terra R package functions crop(), mask() and project() for simple spatial analyses. However, after 2-3 min of running each of the functions gives the same ...
user avatar
  • 780
0 votes
1 answer
24 views

Permission denied (GDAL error 4) when attempting to open folder using list.files

I'm new to R and using terra and cannot figure out this permission error. This exact code works on another researcher's computer, so I'm not sure why it's not working on my Rstudios. The folder and ...
user avatar
  • 1
0 votes
1 answer
50 views

How to export arguments for terra's app function when using multiple cores?

Suppose the following example: library(terra) r <- rast() r1 <- setValues(r, rnorm(ncell(r))) r2 <- setValues(r, rnorm(ncell(r))) r3 <- setValues(r, rnorm(ncell(r))) rs <- c(r1,r2,r3) ...
user avatar
  • 12.1k
0 votes
1 answer
10 views

Plotting a buffer polygon over wrld_simpl

I want to create a buffer around a set of spatial points and plot it over wrld_simpl, without using ggplot2. Yet, there seem to be some issues with the projection I do not quite understand. here is my ...
user avatar
0 votes
1 answer
41 views

Rasterise a point pattern in R

I have a set of georeferenced points within an area for which I want to know the spatial distribution (sparse/clustered) according to a regular grid. I thought to describe the pattern using the Clark-...
user avatar
  • 143
2 votes
2 answers
87 views

Calculate area weighted mean values of polygons for raster cells using R

Problem I want to rasterize polygon values and assign their weighted mean to raster cells. Weighting should be done based on the polygons' cell coverage. Problem visualized Calculation for grid cell ...
user avatar
  • 164
3 votes
1 answer
170 views

Parallelize raster area calculation per class in R

I am trying to get crop area for admin units of whole African continent using the data available here at 30-m spatial resolution globally (raster value 1 = crops). I've tried both terra and raster ...
user avatar
1 vote
1 answer
33 views

R Terra: Write multiple time series variables to NetCDF

I have multiple variables in a timeseries which I want to save as NetCDF. But when I combine all variables with terra::sds() the time information disappears. I tried the following: library("terra&...
user avatar
  • 199
1 vote
1 answer
39 views

Creating regular sampling grid, with specific distance between points using R

I wish to create a regular sampling grid within m spatial polygon. Similar question is answered as Creating regular soil sampling grids using QGIS but I wish to use R. In terra package, I can create ...
user avatar
  • 3,266
0 votes
2 answers
63 views

How to treat NAs using a custom function in the lapp function of {terra} in R

I am using the {terra} package of R-Stats. I have some quite complicated conditional maths to run on a set of rasters; a different formula depending on the value in the cell. I have tried to make a ...
user avatar
0 votes
1 answer
64 views

Soilgrids points outside of projection domain

I have downloaded a global soilgrid tif from ISRIC (https://files.isric.org/soilgrids/latest/data_aggregated/5000m/ocs/) and when I import this in R with terra::rast(filename) (and similarly with ...
user avatar
0 votes
1 answer
109 views

Extracting mean of multiple SpatRasters using Terra?

I have a list of rasters which I want to combine into a single raster by getting the mean for each cell, ideally in the R terra package. According to this answer, the calc function in the raster ...
user avatar
2 votes
1 answer
49 views

How to reorder categorical raster legend order in R

For example, I have a landcover raster and I want to use terra::plot to make a map. library(terra) test <- rast('test.tif') plot(test) The order now is "3,4,6,8". Now I want to reorder ...
user avatar
0 votes
0 answers
80 views

using as.polygons() with patches() from terra instead of rasterToPolygons() with clump() from raster

I am trying to figure out the equivalent of contour_poly <- rasterToPolygons(clump(my_raster > 1), dissolve = TRUE) from raster to terra. I tried contour_poly <- as.polygons(patches(...
user avatar
0 votes
1 answer
67 views

How to compute partial correlation of pixels in R

For example, I have 20 years'NDVI and temperature and precipitation rasters. At first,I computed the pearson correlation coefficent with those codes as belows. library(terra) library(Hmisc) ndvi <- ...
user avatar
2 votes
2 answers
51 views

exact_extract Coverage area wrong fcoverage fraction

I'm using R to extract the values from a raster into a polygon. It works fine for most of them, but I found this small polygon (which is smaller than the cells) where it is giving me some misleading ...
user avatar
2 votes
1 answer
133 views

Rasterizing and resampling a vector object using stars::st_rasterize() and terra::rast() loses values/data

I am currently struggling with rasterizing and later, resampling a vector object using functions from stars and terra. Let's start with the example dataset here: https://drive.google.com/drive/u/2/...
user avatar
  • 1,292
1 vote
1 answer
41 views

Comparing polygon areas across simple feature collections

I currently have three simple feature objects created from different shapefiles in R using the sf package. These three objects have all been projected to the same coordinate system using st_transform()...
user avatar
  • 1,292
0 votes
2 answers
108 views

sf::st_read() and terra::vect() reading in different extents from the same shapefile

I am currently working with multiple shapefiles in R and working towards rasterizing them using a combination of functions from sf,terra and stars. However, I noticed while reading in a shapefile that ...
user avatar
  • 1,292
1 vote
1 answer
86 views

Rasterization of polygon with area variable

The usual approach for rasterizing polygons/vectors is to take the values of polygons inside a grid cell and combine them somehow. This works well for field attributes that are not proportional to the ...
user avatar
0 votes
1 answer
51 views

Create raster of aspect relative to the nearest road

I know that slopes which are downhill from the nearest road are an important raster layer for my model (example application: cars carry seeds on their wheels. The seeds fall on the road and downhill ...
user avatar
0 votes
2 answers
47 views

rgdal setting unreliable in parallel raster processing - terra produces auxiliary files for wrapped rasters but not for locally defined ones

Background: When writing rasters using formats that do not support geolocation (for example .png), the Terra package saves auxiliary files containing this information. This can be prevented by ...
user avatar
  • 338
4 votes
2 answers
132 views

Preventing Terra from writing auxiliary files when writing to disc

Background: When writing .png rasters to disc using the terra R package, not only the intended raster is written, but also auxiliary files (.png.aux.xml and .png.aux.json files, depending on version ...
user avatar
  • 338
1 vote
1 answer
63 views

Error calculating aspect from DEM in R using raster/terra

I have a DEM file gathered using R. The raster and terra packages both have a function to calculate aspect, called terrain(). When using the terrain() function and the units="degrees" ...
user avatar
3 votes
1 answer
284 views

Calculating proportion of a polygon covered by each raster category using R?

I am using R. I have a polygon v that covers multiple raster cells in raster z. How could I calculate what proportion of the polygon is covered by each category in the raster? I read a solution on ...
user avatar
2 votes
0 answers
119 views

How can I prevent terra layerCor temp files filling all available diskspace?

I'm trying to analyze a subset of the Woldclim bioclimatic data (19 raster layers) that covers the Canadian Arctic. I'd like to see how correlated the layers are. When using the layerCor function from ...
user avatar
2 votes
1 answer
183 views

How can I select which layers I want to keep in a new SpatRaster?

I have a raster with 10 layers. I want to split it up into two separate objects, one containing the first 5 layers, and one containing the second 5 layers. Reading the help file for rast(), it seems ...
user avatar
0 votes
1 answer
26 views

How can I know if I'm using terra or aqua satellite

I'm using the MOD09GA product (mainly using band 6). I want to work only with the data from the Terra satellite; how can I know if the image is Terra or Aqua? I looked in the MODIS Surface Reflectance ...
user avatar
2 votes
1 answer
327 views

How can I both resample and aggregate a raster using Terra?

I essentially have the same issue described here, but this question has not been answered Can I resample a raster in R using "max" as function?. I have a elevation raster with a resolution ...
user avatar
0 votes
1 answer
27 views

My R package doesn't use `raster` or `sp` but these seem to be loaded when I roxygenize it

I have refactored my package to remove any trace of raster to replace it with the newer, faster terra package. Here is the list of functions and packages I now import as listed in my package's ...
user avatar
3 votes
1 answer
109 views

Cells per category in R terra

On a categorical SpatRast object, how do I efficiently return a count of cells per factor level? Equivalent to e.g. GRASS r.stats with flag 'c' summary(x, size = ncell(x)) returns a table object ...
user avatar
  • 3,430
1 vote
2 answers
56 views

Is there an equivalent of "isLonLat" function in terra package in R?

I am refactoring my code which extensively relied on raster package to now use the newer and better terra package. I was making use of the isLonLat() function that returns TRUE if the crs of a raster ...
user avatar
1 vote
1 answer
64 views

Expand stars-raster in R

In the r-package terra there is the function terra::extend. To my understanding it just expands the raster with no data values on each side right? I wanted to do something similar, but using a stars ...
user avatar
  • 707
2 votes
2 answers
380 views

How to subset a SpatRaster by value in R

How do I subset a SpatRaster from the R terra package by value? This is the raster: > rh10 class : SpatRaster dimensions : 2732, 4379, 1 (nrow, ncol, nlyr) resolution : 1000, 1000 (x, y) ...
user avatar
  • 23
0 votes
1 answer
64 views

Why does {terra} r library not have a mean method for the project function?

Why when using the terra::project function, is there no 'mean' or 'max' or 'min' etc method for how one raster is projected to another resolution and alignment? https://rdrr.io/github/rspatial/terra/...
user avatar
0 votes
2 answers
109 views

R: Get majority percentage after aggregation with modal function

I want to aggregate a raster using a majority filter (modal) and in addition to that, I want to have a layer in which every cell has the value of the majority percentage. For example: Pixel 1 has a ...
user avatar
  • 199
0 votes
2 answers
53 views

Plot filtered patch sizes in R terra

I would like to plot patch sizes above a threshold in R terra. f=system.file("ex/elev.tif", package="terra") r=rast(f) x=app(r,function(x) ifelse(x< 400,NA,x)) y=patches(x) z=...
user avatar
2 votes
2 answers
106 views

get patch size in R terra

I would like to get the patch sizes in a R terra object, and then filter patches above a threshold. How do I get patch sizes in terra (and then filter)? The only output I see is patch ID (see plot). f ...
user avatar
0 votes
0 answers
29 views

Outer overlaps in Terra R

I am relatively new to R and GIS in R especially. Just recently started to quit QGIS to use terra in R. I have tried a couple of times to do overlaps (intersect) and also to merge a data frame into a ...
user avatar
1 vote
1 answer
98 views

How to test whether the argument passed to an R function is a raster or a vector

I am writing a function that expects two arguments: X and Y. Argument X should be a raster which can be generated by packages such as raster, terra, or stars. Argument Y should be a polygon from ...
user avatar
3 votes
1 answer
470 views

terra::rast() vs raster::brick() for loading in .nc files

Long time raster user, first time using terra. I have a large .nc raster file (1,168 cells, 10,000 layers, 168MB in size). When using raster::brick() the file is loaded into my R session within a ...
user avatar
  • 259
2 votes
1 answer
379 views

Unable to open gpkg files in R on HPC

I'm using R (v4.0.3) on my university's High Performance Computing cluster, loading gdal (v3.3.1), geos (v3.6.1), sqlite (v3.32.3), udunits (v2.2.26), and proj (v7.1.0) libraries prior to opening R. ...
user avatar
0 votes
0 answers
45 views

Erroneous output with terra focal

I am getting some odd output from terra::focal where erroneous values are being written in the output (see figure). I have a 18.1 GB 30m, 6 band image (38700x30922) that I am applying a focal function ...
user avatar
1 vote
1 answer
357 views

R->rasterVis->levelplot Visualize raster stacks time series with different variables side by side

I am a bit new to R. Problem summary: I have several raster stacks that share the same spatial extent and time stamp/step. Each stack holds a time series for a unique variable. I want to use rasterVis:...
user avatar
1 vote
1 answer
37 views

Points do not exist when rasterising vector information

I am trying to create a raster from point data by rasterising it with a map of the UK given that these points reside within the UK (Given at the image below). The dataset that I have are of weather ...
user avatar
0 votes
1 answer
39 views

Changing projection changes area when calculated in R

I have a raster with pixel values 1- area of interest and NA-not an area of interest and hence excluded completely. This raster has CRS WGS 1984, with no projection assigned to it and has resolution ...
user avatar
  • 780