How to install the R spatial library Terra on a conda enviroment - Tutorial

We have researched advanced tools as programming languages for spatial analysis, so far Python was our de facto choice because of its learning rate and easy to use. However, when working with a high amount of data and performing massive spatial queries some questions arise about the performance of Python and then we look at some other options as R, Julia or Rust. We have made a tutorial for the installation of the advanced spatial package Terra on a R kernel in Jupyter under a Conda environment, the tutorial covers all the installation steps in Windows together with some examples to load and plot vector and raster data.

Tutorial

Code

Commands in Anaconda Prompt:

Create the environment

conda create -n rspatial

Activate your environment

conda activate rspatial

Install the required packages

conda install -c conda-forge r-terra jupyterlab r-irkernel

If you want to deactivate the environment, please type:

deactivate

If you want to remove the environmet just type

conda env remove -n rspatial

This is the example code to load shapefiles and rasters

#Check the version of R to check the compatibily with the terra package
R.version
_                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          4                           
minor          1.3                         
year           2022                        
month          03                          
day            10                          
svn rev        81868                       
language       R                           
version.string R version 4.1.3 (2022-03-10)
nickname       One Push-Up
#install required packages for terra 
#install.packages('codetools')
package 'codetools' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\saulm\AppData\Local\Temp\Rtmpuicsub\downloaded_packages
#call the terra package
library("terra")
terra 1.5.21

Open a vector spatial data

#open the shapefile
vectorData <- vect('../In/Shp/Geology_Aoi_Reclass_10N.shp')

#get shapefile info
vectorData
Warning message:
"PROJ: proj_identify: SQLite error on SELECT key, value FROM metadata WHERE key IN ('DATABASE.LAYOUT.VERSION.MAJOR', 'DATABASE.LAYOUT.VERSION.MINOR'): no such table: metadata (GDAL error 1)"



 class       : SpatVector 
 geometry    : polygons 
 dimensions  : 2, 12  (geometries, attributes)
 extent      : 546956.3, 557456.2, 5294244, 5311978  (xmin, xmax, ymin, ymax)
 source      : Geology_Aoi_Reclass_10N.shp
 coord. ref. : WGS_1984_UTM_Zone_10N 
 names       :  OBJECTID GUNIT_TXT        COMMENTS PUB_SOURCE PUB_NUMBER
 type        :     <num>     <chr>           <chr>      <chr>      <chr>
 values      : 1.108e+04        Qw Whidbey Format~       USGS    MF 1541
               1.107e+04        Qp Peat (Holocene)       USGS    MF 1541
    QUAD_NAME      FEATURE_LI      AGE_LITHOL UNIQUE_ID Shape_Leng Shape_Area
        <chr>           <chr>           <chr>     <int>      <num>      <num>
 Edmonds East USGS | MF 1541~ Pleistocene gl~     11081  3.847e+04  3.086e+06
 Edmonds East USGS | MF 1541~ Quaternary bog~     11071   1.53e+04  7.726e+06
 ModelCode
     <int>
         2
         3
#set size of plot
library(repr)
options(repr.plot.width=12, repr.plot.height=16)

#plot shapefile
plot(vectorData, 'AGE_LITHOL')

Open raster data

#open raster dataset as grd
rasterData <- rast('../In/Rst/dem10m_clip.tif')

#show raster info
rasterData
class       : SpatRaster 
dimensions  : 1773, 1049, 1  (nrow, ncol, nlyr)
resolution  : 10.001, 10.00067  (x, y)
extent      : 546964.7, 557455.7, 5294244, 5311975  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 10N (EPSG:32610) 
source      : dem10m_clip.tif 
name        : dem10m_clip
#replace negative/empty values for NA
rasterData[rasterData<0] <- NA

#plot data
plot(rasterData)

Input data

You can download the input files from this link.

Comment

Saul Montoya

Saul Montoya es Ingeniero Civil graduado de la Pontificia Universidad Católica del Perú en Lima con estudios de postgrado en Manejo e Ingeniería de Recursos Hídricos (Programa WAREM) de la Universidad de Stuttgart con mención en Ingeniería de Aguas Subterráneas y Hidroinformática.

 

Suscribe to our online newsletter

Subscribe for free newsletter, receive news, interesting facts and dates of our courses in water resources.