echarts4r icon indicating copy to clipboard operation
echarts4r copied to clipboard

Support for sf objects?

Open Robinlovelace opened this issue 3 years ago • 3 comments

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 avatar Jun 12 '22 21:06 Robinlovelace

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

Robinlovelace avatar Jun 14 '22 20:06 Robinlovelace

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

JohnCoene avatar Jun 15 '22 09:06 JohnCoene

Pointers:

  • sfheaders package for fast conversion of sf objects into 'raw' list/matrix forms that I think echarts wants: https://geocompr.robinlovelace.net/spatial-class.html#the-sfheaders-package
  • Or world it be most effective just to pass echarts geojson objects? https://echarts4r.john-coene.com/articles/geo.html?q=geojson#geojson

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.

Robinlovelace avatar Jun 15 '22 10:06 Robinlovelace