Lines

ImportantSummary

This tutorial explore how to handle spatial lines in R with terra package:

  • read a spatial object with (terra::vect())
  • calculate length of lines with (terra::perim())
  • calculate distances between points and lines with (terra::distance())
TipThe ecologist mind

Otter lives around rivers, but how far from the rivers were they observed? To answer this question, we will need to discover another type of vector: lines.

Setup

Follow the setup instructions if you haven’t followed the tutorial on points

If haven’t done it already, please follow the setup instructions. Let’s start with loading the required packages.

suppressPackageStartupMessages({
  library(mapview)
  library(here)
  library(sf)
  library(terra)
})
pt_otter <- vect(here("data", "gbif_otter_2021_mpl50km.gpkg"))
pt_otter <- vect(
  "https://github.com/FRBCesab/spatial-r/raw/main/data/gbif_otter_2021_mpl50km.gpkg"
)

Load lines from a geopackage file

Lines are made of multiple points. It is possible to create lines directly from coordinates but, in practice, it often comes from an existing spatial dataset. For our example, we will load rivers for the area of interest from IGN data BD CARTO.

Note that dataset has rough resolution (BD TOPO would be more suited for real analysis), but it’s perfect for our illustration and learning purpose.

You can load all vector dataset in R with the function sf::st_read(). It can read many different formats. Another function sf::read_sf() is also available and return a similar spatial object but with a tibble instead of a data.frame as attribute table.

river <- vect(here("data", "BDCARTO-River_mpl50km.gpkg"))

In sf, there are two functions to read all types of vector data sf::st_read() and sf::read_sf(). The main difference lies in the format of the attribute table: data.frame with st_read, and a tibble with read_sf().

river_sf <- st_read(here("data", "BDCARTO-River_mpl50km.gpkg"))
Reading layer `BDCARTO-River_mpl50km' from data source 
  `/home/romain/GitHub/spatial-r/data/BDCARTO-River_mpl50km.gpkg' 
  using driver `GPKG'
Simple feature collection with 2110 features and 8 fields
Geometry type: MULTILINESTRING
Dimension:     XY
Bounding box:  xmin: 3.18983 ymin: 43.21278 xmax: 4.56447 ymax: 44.16754
Geodetic CRS:  WGS 84
river <- vect(
  "https://github.com/FRBCesab/spatial-r/raw/refs/heads/main/data/BDCARTO-River_mpl50km.gpkg"
)
NoteYour turn
  1. How many different lines of river was loaded?
  2. What is the coordinate reference system (CRS) of the loaded river data?
  3. How can you get the name of the all the river stretches?
Click to see the answer
  1. There are 2110 lines in the dataset. You can access it with dim(river), nrow(river) or just by typing river in the console.
  2. The coordinates are in WGS 84 (EPSG 4326). You can access this information with crs(river, describe = TRUE) (or in sf with st_crs(river_sf)).
  3. The column that stored the name of river stretches is toponyme. You could identify it with head(river) or names(river). There are 151 river stretches without names (table(is.na(river$toponyme))).

Calculate length of lines

When calculating distances and length, be careful with projection systems. Some are not suited to calculate distance. Prefer equidistance projections or use local projection system (less distorsion if your study area is small).

The function terra::perim() The package terra recommends the calculation of distances in lat/long to get more accurate results (considering the geodesic distance, so accounting for Earth’s curvature).

# calculate the length of rivers (in km)
river$length_km <- perim(river) / 1000

The function sf::st_length() calculate the length of lines. While calculating distance and length, be careful with projection systems. Some are not suited to calculate distance.It is recommended to use equidistant projections or use local projection system (if your study area is small).

# calculate the length of rivers (in km)
river_sf$length_km <- st_length(river_sf) / 1000
# see the distribution of river length
boxplot(river$length_km, ylab = "length (km)")

NoteYour turn
  • Which is the longest river in our dataset?
Click to see the answer
# get the name of the longest river
river$toponyme[which.max(river$length_km)]
[1] "l'Hérault"

Map multiple layers

Let’s map the river and their length, as well as position of the otter observations.

You can combine multiple layers in mapview with a +.

mapview(river, zcol = "length_km") +
  mapview(pt_otter, col.regions = "red", color = NA)
plot(river, y = "length_km")
plot(pt_otter, add = TRUE)

plot(river_sf["length_km"], reset = FALSE, axes = TRUE)
plot(pt_otter, add = TRUE)

Warning

If you want to overlay multiple spatial object with base plot from sf, don’t forget to use the argument reset=FALSE.

Calculate the distance to the closest line

TipThe ecologist mind

At which distance from a river were the otter observed?

We will need to calculate distance among objects with the function terra::distance().

Before comparing two spatial objects, it is recommended to plot the data (if not too big) and make sure the projection systems are the same and the extents match. Do not use the package mapview because it will automatically project your data.

# make sure the spatial objects have the same projection
crs(river) == crs(pt_otter)
[1] TRUE

Find the closest river

distance_matrix <- distance(pt_otter, river)
# get the nearest distance
nearest_distance <- apply(distance_matrix, 1, min)
# get which river was the closest
nearest_river <- apply(distance_matrix, 1, which.min)
Warning

Be aware that the newly added function terra::nearest() is not as precise as the approach shown above (as in terra 1.8-70, 03/11/2025).

In sf, the same procedure as in terra could be followed with sf::st_distance(). However it is much faster to use a two-step process: (1) find the closest river sf::st_nearest_feature() and (2) calculate the distance between points and their closest river sf::st_distance().

# Step 1: Find index of nearest line for each point
nearest_river_sf <- st_nearest_feature(st_as_sf(pt_otter), river_sf)

The object nearest_river_sf is a vector containing the index of the closest river for each otter observation.

# Step 2: Calculate distance only to the nearest line
nearest_distance_sf <- st_distance(
  st_as_sf(pt_otter),
  river_sf[nearest_river_sf, ],
  by_element = TRUE
)
boxplot(nearest_distance, ylab = "distance to river (m)")

NoteYour turn
  1. Which are the rivers with most sights of otter? answer doesn’t work anymore
  2. Make a map with the distance to river and visually check the coherence of the calculation.
  3. (Reflexion) What can be the factors explaining the few outliers with large distances (>1km)?
Click to see the answer 1
table(river$toponyme[nearest_river]) |>
  sort(decreasing = TRUE) |>
  head(5)

                                                         l'Hérault 
                                                                 8 
                                                            la Vis 
                                                                 8 
                                                        le Lamalou 
                                                                 4 
Canal d'Irrigation du Bas Rhône-Languedoc ou Canal Philippe Lamour 
                                                                 3 
                                      Canal de Peccais à Sylvéréal 
                                                                 3 
Click to see the answer 2
# add the distance to river to the spatial object
pt_otter$dist_river <- nearest_distance

# make an interactive map
mapview(river) +
  mapview(pt_otter, z = "dist_river")

# or static map
plot(st_geometry(river))
plot(pt_otter[, "dist_river"], add = TRUE, pch = 16)