Reading and writing spatial data

Introduction

Reading and writing spatial data is complicated by the fact that there are many different file formats. However, there are a few formats that are most common that we discuss here.

Vector files

The shapefile is the most commonly used file format for vector data. It is trivial to read and write such files. Here we use a shapefile that comes with the terra package.

Reading

We use the system.file function to get the full path name of the file’s location. We need to do this as the location of this file depends on where the raster package is installed. You should not use the system.file function for your own files. It only serves for creating examples with data that ships with R.

library(terra)
filename <- system.file("exdata/lux.shp", package="terra")
filename
## [1] "C:/soft/R/R-3.6.3/library/terra/exdata/lux.shp"

Now we have the filename we need we use the vect function.

s <- vect(filename)
s
## class       : SpatVector
## geometry    : polygons
## elements    : 12
## extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +no_defs
## names       : ID_1, NAME_1, ID_2, NAME_2, AREA

The vect function returns SpatVector objects. It is important to recognise the difference between this type of R object (SpatVector), and the file (shapefile) that was used to create it. (Do not say “I have a shapefile in R”, say “I have a SpatVector of polygons in R” (“created from a shapefile”).

Writing

You can also write new shapefiles using the writeVector method. You can add argument overwrite=TRUE if you want to overwrite an existing file.

#outfile <- 'shptest.shp'
#writeVector(s, outfile, overwrite=TRUE)

Raster files

The terra package can read and write several raster file formats.

Reading raster data

Again we need to get a filename for an example file.

f <- system.file("exdata/logo.tif", package="terra")
f
## [1] "C:/soft/R/R-3.6.3/library/terra/exdata/logo.tif"

Now we can do

r <- rast(f)
r
## class       : SpatRaster
## dimensions  : 77, 101, 3  (nrow, ncol, nlyr)
## resolution  : 1, 1  (x, y)
## extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
## data source : C:/soft/R/R-3.6.3/library/terra/exdata/logo.tif
## names       : red, green, blue
## min values  :   0,     0,    0
## max values  : 255,   255,  255

Note that x is a SpatRasterayer of three layers (“bands”). We can subset it to get a single layer.

r2 <- r[[2]]
r2
## class       : SpatRaster
## dimensions  : 77, 101, 1  (nrow, ncol, nlyr)
## resolution  : 1, 1  (x, y)
## extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
## data source : C:/soft/R/R-3.6.3/library/terra/exdata/logo.tif
## names       : green
## min values  :     0
## max values  :   255

The same approach holds for other raster file formats, including GeoTiff, NetCDF, Imagine, and ESRI Grid formats.

Writing raster data

Use writeRaster to write raster data. You must provide a SpatRaster object and a filename. The file format will be guessed from the filename extension (if that does not work you can provide an argument like format=GTIFF). Note the argument overwrite=TRUE and see ?writeRaster for more arguments, such as datatype= to set the datatype (e.g., integer, float).

x <- writeRaster(r, "output.tif", overwrite=TRUE)
x
## class       : SpatRaster
## dimensions  : 77, 101, 3  (nrow, ncol, nlyr)
## resolution  : 1, 1  (x, y)
## extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
## data source : output.tif
## names       : red, green, blue
## min values  :   0,     0,    0
## max values  : 255,   255,  255