Skip to content

align_move() fails for projected data with identical track length #141

@robe2037

Description

@robe2037

When input data are projected, align_move() can fail if the number of interpolated points along each track in the same.

Reprex:

library(moveVis)
#> Find out about new features added to moveVis at
#> https://movevis.org/news/index.html

data("move_data")

# Create sample dataset with identical track lengths:
d <- move_data |> 
  dplyr::group_split(track) |> 
  purrr::map(~ .x[1:10, ]) 

ts <- seq.POSIXt(
  as.POSIXct("2018-05-15 08:00:00", tz = "UTC"), 
  as.POSIXct("2018-05-24 08:00:00", tz = "UTC"), 
  length.out = 10
)

d[[1]]$timestamp <- ts
d[[2]]$timestamp <- ts
d[[3]]$timestamp <- ts

d <- move2::mt_as_move2(
  purrr::list_rbind(d), 
  time_column = "timestamp", 
  track_id_column = "track"
)

# WGS84 succeeds
x <- align_move(d)
#> Temporal resolution of 1 [d] is used to align trajectories.

# Projected CRS fails because CRS is dropped
x <- align_move(sf::st_transform(d, "epsg:32637"))
#> Temporal resolution of 1 [d] is used to align trajectories.
#> Error: arguments have different crs

Created on 2025-10-15 with reprex v2.1.1

If the projected data don't end up with the same number of interpolated points, align_move() works as expected, so this is an edge-case issue. Still, it seems like it would be easy to resolve in these lines:

moveVis/R/align_move.R

Lines 147 to 151 in cf88cdc

if(all(st_is_longlat(m), sf_use_s2())){
m_aligned <- mapply(.m = m_sf_lines, .nd = nd, function(.m, .nd) st_as_sf(s2_interpolate_normalized(st_geometry(.m), .nd)), SIMPLIFY = T)
} else{
m_aligned <- mapply(.m = m_sf_lines, .nd = nd, function(.m, .nd) st_line_interpolate(st_geometry(.m), .nd), SIMPLIFY = T)
}

For projected CRS, st_line_interpolate() returns objects of "sfc_POINT". When each of the tracks has the same number of these points, mapply() is willing to coerce them to a rectangular matrix given that SIMPLIFY = T, but this drops CRS information. For geographic coordinates, st_as_sf() coerces the output elements to sf objects, which mapply() doesn't simplify to a matrix.

Forcing the interpolation code to return an sf object for projected coordinates would likely fix the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions