ateucher / rmapshaper

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

apply_mapshapper_commands not working? #121

Closed dblodgett-usgs closed 2 years ago

dblodgett-usgs commented 2 years ago

I could work on a solution given a little head start, but maybe this is an easy fix?

nc <- sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf"))
try(rmapshaper::apply_mapshaper_commands(nc, "-clean"))
#> Registered S3 method overwritten by 'geojsonlint':
#>   method         from 
#>   print.location dplyr
#> Error in context_eval(join(src), private$context, serialize, await) : 
#>   Error in V8 context: TypeError: Cannot use 'in' operator to search for '324' in c(0.114, 0.061, 0.143, 0.07, 0.153, 0.097, 0.062, 0.091, 0.118, 0.124, 0.114, 0.153, 0.143, 0.109, 0.072, 0.19, 0.053, 0.199, 0.081, 0.063, 0.044, 0.064, 0.086, 0.128, 0.108, 0.17, 0.111, 0.18, 0.104, 0.077, 0.142, 0.059, 0.131, 0.122, 0.08, 0.118, 0.219, 0.118, 0.155, 0.069, 0.066, 0.145, 0.134, 0.1, 0.099, 0.116, 0.201, 0.18, 0.094, 0.134, 0.168, 0.106, 0.168, 0.207, 0.144, 0.094, 0.203, 0.141, 0.07, 0.065, 0.146, 0.142, 0.154, 0.118, 0.078, 0.125, 0.181, 0.143, 0.091, 0.13, 0.103, 0.095, 0.078, 
#> 0.104, 0.098, 0.091, 0.06, 0.131, 0.241, 0.082, 0.12, 0.172, 0.121, 0.163, 0.138, 0.098, 0.167, 0.204, 0.121, 0.051, 0.177, 0.08, 0.195, 0.24, 0.125, 0.225, 0.214, 0.24, 0.042, 0.212)
#>     at Object.internal.patchMissingFields (<anonymous>:1:61210)
#>     at Object.internal.fixInconsistentFields (<anonymous>:1:60718)
#>     at Object.internal.importJSONTable (<anonymous>:1:339272)
#>     at Object.internal.importJSON (<anonymous>:1:349410)
#>     at Object.internal.importContent (<anonymous>:1:349956)
#>     at Object.api.importFile (<anonymous>:1:353685)
#>     at Object.api.importFiles (<anonymous>:1:352298)
#>     at Object.api.runCommand (<anonymous>:1:518510)
#>     at nextCommand (<anonymous>:1:527690)
#>     at <anonymous>:1:528416
try(rmapshaper::apply_mapshaper_commands(nc, "-clean", sys = TRUE))
#> Warning in if (read_write) {: the condition has length > 1 and only the first
#> element will be used
#> Error : Expected string vector of length 1

tmp_file <- tempfile(fileext = ".geojson")
sf::write_sf(nc, tmp_file)  

tmp_file2 <- tempfile(fileext = ".geojson")

system2(rmapshaper::check_sys_mapshaper(), 
        args = c(tmp_file, "-clean", "-o", tmp_file2))
#> mapshaper version 0.5.68 is installed and on your PATH

plot(sf::read_sf(tmp_file2)["AREA"])

Created on 2022-03-09 by the reprex package (v2.0.1)

ateucher commented 2 years ago

apply_mapshaper_commands() works only on geojson strings or files, not directly on sf or Spatial* objects. So this works:

nc <- sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf"))
rmapshaper::apply_mapshaper_commands(geojsonsf::sf_geojson(nc), "-clean", force_FC = TRUE)

It would certainly be possible to make apply_mapshaper_commands a S3 generic and write methods for sf etc objects...

dblodgett-usgs commented 2 years ago

Oh interesting. OK. That makes sense. Do you mind if we put that into an example? Not sure if that would cause CRAN check issues or anything.

ateucher commented 2 years ago

Sure, that's a great idea. I am in the process of converting most of the internals to using the geojsonsf package in the geojsonsf branch. I can create an example out of this, but if you want to do a PR for it, please do it against that branch. Thanks!