Converts an sf, exploded_map, or grouped_exploded_map object to
TopoJSON using the external mapshaper command-line tool. The input is
first written as a temporary GeoJSON file, then converted by mapshaper.
Arguments
- x
An
sf,exploded_map, orgrouped_exploded_mapobject. Forexploded_mapobjects, the WGS84 exploded geometry (sf_exp_wgs) is exported. Forgrouped_exploded_mapobjects, the WGS84 grouped geometry (sf_grouped_wgs) is exported.- file
Output file path. Should end in
.topojsonor.json.- simplify
Optional simplification proportion passed to
mapshaper -simplify. Must be a single number between 0 and 1 (exclusive). For example,simplify = 0.5retains 50% of vertices. Default isNULL(no simplification). Note that simplification modifies polygon geometry and therefore breaks the exact geometry preservation guarantee of Proposition 1.- overwrite
Logical; if
TRUE, overwritefileif it already exists. Default isFALSE.
Details
This is intended as a convenience helper for downstream tools such as Power BI, D3.js, and Observable that prefer or require TopoJSON input.
This function requires the external mapshaper command-line tool to be
installed and available on the system path. It can be installed with:
npm install -g mapshaperThe mapshaper tool is not an R package dependency; it is invoked via
system2(). If mapshaper is not found, the function errors with an
informative message.
Examples
if (FALSE) { # \dontrun{
# Two-level export
result <- explode_sf(my_sf, region_col = "region", plot = FALSE)
export_topojson(result, "exploded.topojson")
# Three-level grouped export
grp <- explode_grouped(my_sf, region_col = "region", plot = FALSE)
export_topojson(grp, "grouped.topojson")
# With simplification (breaks Proposition 1 guarantee)
export_topojson(result, "simplified.topojson", simplify = 0.5)
# Raw sf object
export_topojson(my_sf, "raw.topojson", overwrite = TRUE)
} # }
