ateucher / rmapshaper

An R wrapper for the mapshaper javascript library
http://andyteucher.ca/rmapshaper/
Other
200 stars 13 forks source link

output format option #128

Open Shaunson26 opened 1 year ago

Shaunson26 commented 1 year ago

Is it possible to simply use rmapshaper to convert between json types e.g.

mapshaper -i input.geojson -o output.topojson format=topojson

I see in utils.R and sys_mapshaper(), that the cmd_args object created would not currently allow this option

cmd_args <- c(sys_mem, shQuote(in_data_file), command, "-o", shQuote(out_data_file))

would it be simple enough to allow a format parameter somewhere?

# some earlier format parameter wrangling if argument supplied .. resulting in something like this
cmd_args <- c(sys_mem, shQuote(in_data_file), command, "-o", shQuote(out_data_file), 'format=topojson')

if I run system2(ms_path, cmd_args), with command="", then i get a file type convert occurring .. so it seems to work

Shaunson26 commented 1 year ago

I had a play with this .. required a new ms_change_format() function and some minor changes to sys_mapshaper()..

Assuming you only allow file changes (only use of sys_mapshaper()) ..

Allow sys_mapshaper() to have format and outfile parameters, where if missing, format='' and outfile will be the current default temp_geojson(), and then introduce format to cmd_args

sys_mapshaper <- function(data, data2 = NULL, command, sys_mem = 8, format, outfile) {

  ...

  if (!missing(format)){
    format = paste0('format=', format)
  } else {
    format = ''
  }

  if(!missing(outfile)){
    out_data_file <- outfile
  } else {
    out_data_file <- temp_geojson()
  }

  if (!is.null(data2)) {
    cmd_args <- c(sys_mem, shQuote(in_data_file), command, shQuote(in_data_file2), "-o", shQuote(out_data_file), format)
  } else {
    cmd_args <- c(sys_mem, shQuote(in_data_file), command, "-o", shQuote(out_data_file), format)
  }

  system2(ms_path, cmd_args)

 ...
}

A new function

ms_change_format <- function(input, output, format, sys_mem = 8) {
  # check format, or file extention if format missing: shapefile|geojson|topojson|json|dbf|csv|tsv|svg
  sys_mapshaper(data = input, command = '', sys_mem = sys_mem, outfile = output, format = format)
}

Note that mapshaper say this about thr output parameter -o

format=shapefile|geojson|topojson|json|dbf|csv|tsv|svg Specify output format. If the format= option is missing, Mapshaper tries to infer the format from the output filename. If no filename is given, Mapshaper uses the input format. The json format is an array of objects containing data properties for each feature.

res <- 
  ms_change_format(input = geo_temp,
                   output =  topo_temp,
                   format = 'topojson')

Allocating 8 GB of heap memory
[o] Wrote C:\Users\Shaunus\AppData\Local\Temp\RtmpekjpbW\file6ac57152d61.topojson
``
ateucher commented 1 year ago

Hey @Shaunson26 sorry for the long delay in replying here. I'll have to think about this a bit more. I can understand the desire to convert geojson to topojson but that's not really the intent of this package. I feel like it belongs more in the domain of geojsonio as it's converting between formats rather than spatial operations. Did the work over there satisfy your need?

Shaunson26 commented 1 year ago

We solved the issue over there.

I do still think allowing a fuller suite of the mapshaper options in rmapshaper would be good - viewing it as an R port of mapshaper - but I'll leave that in your hands 👍