2 Working with Data

This section covers changes made to common data manipulation and querying methods.

2.1 Renamed Methods

In general, when method names were changed in terra, they got shorter and more similar to base R. Most capital letters were removed as well.

Old Methods New Method
coordinates crds
getValues values
nlayers nlyr
reclassify, subs, cut classify
stack, addLayer c
unstack as.list
dropLayer subset
rasterTo* as.*
isLonLat is.lonlat
bind rbind

2.2 Combined Methods

Many tasks that previously at separate methods for raster and vector data, due to the classes originating in separate packages, now have shared method names. Functions in green rows have been renamed, while functions in grey rows, maintain their name from raster.

Operation Old Methods New Method
reproject geometry projectRaster, sp::spTransform project
combine cells/polygons aggregate aggregate
split cells/polygons disaggregate disagg
spatial query extract, sp::over extract
spatial sampling sp::spsample, sampleRandom spatSample

2.3 Speed Improvements

Many of the most time-intensive operations got speed boosts from the C++ implementation.

function median (millisec) n_eval
terra::spatSample 1.98 100
raster::sampleRandom 6.28 100
terra::project 34.17 100
raster::projectRaster 57.14 100
terra::resample 1.79 100
raster::resample 51.62 100
terra::rasterize 1.69 100
raster::rasterize 236.27 100
terra::as.polygons 27.35 100
raster::rasterToPolygons 210.74 100

2.4 Spatial Relationships

A key function in any GIS software is the ability to determine the relationship between two spatial objects. Previous packages (raster, rgeos) used used different method names depending on the geometric query in question. Rgeos in particular contained at least twelve functions just for determining spatial relationship. In terra, the number of objects compared (not the relationship type) determines the function used. For one-to-one and one-to-many comparisons use is.related(), for many-to-many comparisons, use relate(). The relation argument specifies relationship type. Options include intersects, touches, crosses, overlaps, within, contains, covers, covered by, disjoint, or DE-9IM strings.

#is our raster big enough to cover all of Luxembourg
is.related(temp, lux, relation='covers')
## [1] TRUE

Method names for the spatial operations below remain unchanged:

  • union()
  • intersect()
  • symdif()
  • buffer()
  • crop()
  • cover()
  • mask()