使用rmapshaper的ms_simplify生成损坏GeoJSON文件的问题求助
使用rmapshaper的ms_simplify调用系统版时临时文件损坏的问题
问题描述
处理大型shapefile时,调用rmapshaper包的ms_simplify函数并启用系统版mapshaper(sys=T),初期步骤显示正常,但生成的临时GeoJSON文件损坏,无法打开。更换临时目录后问题依旧。
错误日志
simplif_coo2 <- ms_simplify(raw, keep = 0.5, keep_shapes = TRUE, sys=T, sys_mem=6, no_repair=T) Allocating 6 GB of heap memory [i] Snapped 200 points [o] Wrote C:\TEMP\RtmpkFv92L\file25f818895f48.geojson Error: Cannot open "C:\TEMP\RtmpkFv92L\file25f818895f48.geojson"; The source could be corrupt or not supported. See `st_drivers()` for a list of supported formats.
已做排查
- 确认临时目录下的生成文件确实无法打开
- 更换临时存储目录,问题未解决
- 尝试将原shapefile转为GeoJSON后再处理,无改善
环境信息
数据读取方式
raw<-st_read("RedList_Countries_V2022.1.shp")
R会话信息
R version 4.2.2 (2022-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 22000) Matrix products: default locale: [1] LC_COLLATE=French_France.utf8 LC_CTYPE=French_France.utf8 LC_MONETARY=French_France.utf8 LC_NUMERIC=C [5] LC_TIME=French_France.utf8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] dplyr_1.1.0 sf_1.0-9 rmapshaper_0.4.6 loaded via a namespace (and not attached): [1] Rcpp_1.0.10 rstudioapi_0.14 magrittr_2.0.3 units_0.8-1 tidyselect_1.2.0 jsonvalidate_1.3.2 [7] R6_2.5.1 rlang_1.0.6 fansi_1.0.4 tools_4.2.2 grid_4.2.2 utf8_1.2.2 [13] KernSmooth_2.23-20 cli_3.6.0 e1071_1.7-12 DBI_1.1.3 class_7.3-20 tibble_3.1.8 [19] lifecycle_1.0.3 httpcode_0.3.0 vctrs_0.5.2 curl_5.0.0 crul_1.3 glue_1.6.2 [25] V8_4.2.2 proxy_0.4-27 pillar_1.8.1 compiler_4.2.2 generics_0.1.3 classInt_0.4-8 [31] geojsonlint_0.4.0 jsonlite_1.8.4 pkgconfig_2.0.3
系统版mapshaper状态
通过npm安装,check_sys_mapshaper()输出:
mapshaper version 0.6.24 is installed and on your PATH mapshaper-xl "C:\\Users\\Victor\\AppData\\Roaming\\npm\\MAPSHA~3.CMD"
解决方案建议
- 升级mapshaper版本:当前使用的0.6.24版本较旧,执行以下命令升级到最新稳定版:
npm update -g mapshaper - 移除
no_repair=T参数:让mapshaper自动修复几何问题,避免生成损坏的GeoJSON文件。 - 调整内存分配:尝试降低
sys_mem参数值(例如设为4),避免内存分配过大导致文件写入异常。 - 手动调用系统mapshaper:绕过R包直接处理文件,再读回R:
读取处理后的文件:mapshaper-xl RedList_Countries_V2022.1.shp -simplify keep=0.5 keep-shapes -o simplified_output.geojsonsimplif_coo2 <- st_read("simplified_output.geojson") - 调整系统编码:由于系统locale为法语,尝试切换到UTF-8编码:
Sys.setlocale("LC_ALL", "English_United States.utf8")
内容的提问来源于stack exchange,提问作者Victor Cz
相关产品推荐
相关产品推荐

