2

I'm using mapdeck::add_path() to show aircraft flight paths. I would like to color the paths by elevation; is this possible? Here is a short example:

library(mapdeck)
library(sf)
# From https://github.com/kent37/mapdeck_play/blob/master/tracks_2019_03_11.gpkg?raw=true
one_day = st_read('tracks_2019_03_11.gpkg')

key = 'your_api_key'
mapdeck(location=c(-71.128184, 42.3769824), zoom=14, key=key) %>% 
  add_path(one_day)

enter image description here

Thanks!

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
Kent Johnson
  • 3,320
  • 1
  • 22
  • 23

1 Answers1

3

It's currently not possible to have a multi-coloured path, but it is on my todo list.

To achieve what you're after you'll have to use a line layer, which takes an 'origin' and 'destination' and draws a straight line (i.e., the constituent parts of a path)

To get the Origin-Destination columns we need to decompose the sf object into a data.frame, add the '_to' columns, then make it an sf object again.

(I also have a todo to allow data.frames to use Z and M, but for now we have to do this final conversion to sf again)

library(data.table)
library(sfheaders)

df <- sfheaders::sf_to_df( one_day, fill = TRUE )

setDT( df )[
  , `:=`(
    x_to = shift(x, type = "lead")
    , y_to = shift(y, type = "lead")
    , z_to = shift(z, type = "lead")
    , m_to = shift(m, type = "lead")
    )
  , by = flight
]

df <- df[ !is.na( x_to ) ]

df$origin <- sfheaders::sfc_point(
  obj = df
  , x = "x"
  , y = "y"
  , z = "z"
  , m = "m"
)

df$destination <- sfheaders::sfc_point(
  obj = df
  , x = "x_to"
  , y = "y_to"
  , z = "z_to"
  , m = "m_to"
)

sf <- sf::st_as_sf( df )

mapdeck(
  style = mapdeck_style("dark")
) %>%
  add_line(
    data = sf
    , origin = "origin"
    , destination = "destination"
    , stroke_colour = "z"
  )

enter image description here

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • What is the expected unit for `z`? My data is in feet and the climb profiles look pretty steep; does mapdeck expect meters? – Kent Johnson Feb 05 '20 at 00:32
  • Yes I believe it is expecting [meters](https://github.com/uber/deck.gl/blob/master/docs/developer-guide/coordinate-systems.md) – SymbolixAU Feb 05 '20 at 04:45
  • Apologies for reviving an old question but is there a way to give the line elevation similar to add_column? – Jhonathan Aug 11 '23 at 05:05
  • @Jhonathan for a path to have elevation, each vertext needs it's own altitude, so I don't see how this would work. If you have a suggestion please add it to [github](https://github.com/SymbolixAU/mapdeck/issues) – SymbolixAU Aug 14 '23 at 03:13