JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
601 stars 81 forks source link

Support for sf objects? #439

Open Robinlovelace opened 2 years ago

Robinlovelace commented 2 years ago

Having taken a look at https://echarts.apache.org/examples/en/editor.html?c=lines-bmap-effect I am v. impressed and would like to try creating nice route network maps, e.g. to create improved versions of the Propensity to Cycle Tool: https://www.pct.bike/

However, this would mean translating sf objects into raw linestring datasets. This may be a bit of a big ask and I may be better off using mapdeck and rdeck but thought it worth asking: is this within scope? Many thanks!

Robinlovelace commented 2 years ago

See working prototype with sp objects here: https://github.com/helgasoft/echarty/issues/10

JohnCoene commented 2 years ago

Hi Robin.

We can make it work, I'm not too familiar with sf objects, but it should just be a matter of converting the data structure to the JSON expected by echarts.js

I may find some time to look into it but cannot promise anything right now as I'm really busy with work :(

I'll take any pointer you may have

Robinlovelace commented 2 years ago

Pointers:

Not sure it would work, but could try something like:

e_lines = function(
  e,
  source_lon,
  source_lat,
  target_lon,
  target_lat,
  source_name,
  target_name,
  value,
  coord_system = "geo",
  name = NULL,
  rm_x = TRUE,
  rm_y = TRUE,
  sf_object = NULL, # new argument?
  ...
) {
# ...
if(!is.null(sf_object)) {
  sf_raw = sfheaders::sf_to_df(sf_object)
  sf_echarts = sfdf_to_echarts(sf_raw)
  source_lon = sf_echarts$fx,
  source_lat = sf_echarts$fy,
  target_lon = sf_echarts$tx,
  target_lat = sf_echarts$ty,
# ...
}

Reasonable/plausible?

Happy to put in a basic PR exploring an 'sfdf_to_echarts()' function and have a play if you'd be happy to review/direct such a mission.