Skip to contents

This functions creates a line by connecting start and end points.

Usage

make_line(start_x, start_y, end_x, end_y)

Arguments

start_x

longitudinal sf coordinate object from the desired start point

start_y

latitudinal sf coordinate object from the desired start point

end_x

longitudinal sf coordinate object from the desired end point

end_y

latitudinal sf coordinate object from the desired end point

Value

A simple feature geometry object or simple feature linestring object

Examples

library(sporeg)
library(dplyr)
library(sf)
library(purrr)
#> 
#> Attaching package: ‘purrr’
#> The following object is masked from ‘package:data.table’:
#> 
#>     transpose
library(tidyr)

load(system.file("extdata", "at_dly_locs.Rda", package = "sporeg"))

at_lines <- at_dly_locs %>%
 dplyr::group_by(ID, time) %>%
 sf::st_transform(3857) %>%
 dplyr::summarise(pt = sf::st_combine(geometry)) %>%
 sf::st_centroid() %>%
 dplyr::mutate(lat = sf::st_coordinates(pt)[,2],
        lon = sf::st_coordinates(pt)[,1]) %>%
 dplyr::arrange(ID, time) %>% #Order data for making lines
 dplyr::mutate(start_x = lon, start_y = lat,
 end_x = dplyr::lead(lon), end_y = dplyr::lead(lat)) %>%
 sf::st_as_sf(coords = c("lon", "lat"), crs = 3857) %>%
 dplyr::filter(!is.na(end_y)) %>%
 tidyr::nest() %>%
 dplyr::mutate(
   data = purrr::map(data,
                     ~ dplyr::mutate(.x,
                              x = purrr::pmap(.l = list(start_x, start_y, end_x, end_y),
                                              .f = make_line))))
#> `summarise()` has grouped output by 'ID'. You can override using the `.groups`
#> argument.