sf_to_powerbi_topojson.RdConverts an sf spatial dataframe to TopoJSON format optimized for Microsoft Power BI Shape Map visualizations. Automatically handles CRS transformation to WGS84 and validates geometry types.
sf_to_powerbi_topojson(
sf_data,
file = "output_powerbi.json",
object_name = "features",
id_col = NULL,
name_col = NULL,
keep_cols = NULL,
simplify = FALSE,
tolerance = 0.01,
verbose = TRUE
)sf object. Spatial data to convert. Must contain POLYGON or MULTIPOLYGON geometries. Required.
Character string. Output file path. Must end with .json extension (Power BI requirement). Default is "output_powerbi.json".
Character string. Name for the TopoJSON object layer. This name will be used by Power BI to identify the geographic layer. Default is "features".
Character string or NULL. Optional column name to use as the feature ID in the TopoJSON properties. If specified, this field should uniquely identify each feature. Default is NULL (no specific ID field).
Character string or NULL. Optional column name to use as the feature name in the TopoJSON properties. Useful for labeling features in Power BI. Default is NULL (no specific name field).
Character vector or NULL. Column names to keep in the output properties (excluding geometry). If NULL (default), all attribute columns are kept. Geometry column is always retained.
Logical. If TRUE, simplify geometries using rmapshaper to reduce file size while preserving topology. Requires rmapshaper package. Default is FALSE.
Numeric. Simplification tolerance parameter for
rmapshaper::ms_simplify (0-1). Higher values = more simplification.
This is the "keep" parameter: proportion of points to retain. Only used if
simplify = TRUE. Default is 0.01 (keep 1% of points).
Logical. If TRUE (default), print progress messages including CRS transformations, simplification results, and file creation.
Invisibly returns the output file path. The function is called primarily for its side effect of creating the TopoJSON file.
Power BI Requirements: Power BI Shape Map visual has specific requirements for custom map files:
CRS: Must use WGS84 (EPSG:4326) coordinate system
Geometry: Only POLYGON and MULTIPOLYGON supported (no lines/points)
File Extension: Must be .json (not .topojson)
Properties: Need feature properties for data binding
This function automatically handles all these requirements:
Validates that geometries are polygon types
Transforms to WGS84 if needed
Ensures .json file extension
Preserves attribute columns for data binding
Column Filtering:
You can control which attribute columns are included using keep_cols,
id_col, and name_col:
If keep_cols is NULL: all columns are kept
If keep_cols is specified: only those columns (plus geometry)
If id_col or name_col are specified and not in
keep_cols, they are automatically added
Simplification:
Setting simplify = TRUE can dramatically reduce file size for complex
geometries (e.g., detailed municipality boundaries). The tolerance
parameter controls how much simplification occurs:
0.01 = keep 1% of points (aggressive simplification)
0.1 = keep 10% of points (moderate simplification)
0.5 = keep 50% of points (gentle simplification)
Simplification uses topology-preserving algorithms that maintain shared boundaries between adjacent polygons.
Using in Power BI: Once you have created the .json file:
Open Power BI Desktop
Insert > Shape Map visual
Click on the visual
Format visual > Shape > Map settings
Click "+ Add map" and select your .json file
Drag your data fields to the visual:
Location: field matching id_col or other identifier
Color saturation: your metric field
Tooltips: additional fields to display
Export Method:
The function uses geojsonio::topojson_write() with high quantization
(1e5) to create Power BI-compatible TopoJSON files with optimal topology
preservation and precision. Optional geometry simplification is available
via rmapshaper::ms_simplify() when simplify = TRUE.
if (FALSE) { # \dontrun{
# Basic usage with default settings
library(sf)
italy_regions <- st_read("regions.shp")
sf_to_powerbi_topojson(italy_regions, file = "italy_regions.json")
# Specify ID and name columns for Power BI matching
sf_to_powerbi_topojson(
sf_data = italy_regions,
file = "italy_regions.json",
id_col = "COD_REG",
name_col = "DEN_REG",
object_name = "regioni"
)
# Keep only specific columns and simplify geometries
sf_to_powerbi_topojson(
sf_data = italy_municipalities,
file = "italy_comuni_simple.json",
keep_cols = c("PRO_COM", "COMUNE", "COD_REG"),
simplify = TRUE,
tolerance = 0.01 # Keep 1 percent of points
)
# With aggressive simplification for large datasets
sf_to_powerbi_topojson(
sf_data = detailed_boundaries,
file = "boundaries_simplified.json",
simplify = TRUE,
tolerance = 0.005, # Very aggressive
verbose = TRUE # See size reduction
)
# Use with situas package data
library(situas)
files <- prepare_territorial_maps(territorial_level = "province")
province_sf <- readRDS(files$rds)
# Convert to custom Power BI format with specific columns
sf_to_powerbi_topojson(
sf_data = province_sf,
file = "data/province_custom.json",
id_col = "COD_UTS",
name_col = "DEN_UTS",
keep_cols = c("COD_UTS", "DEN_UTS", "COD_REG"),
object_name = "province"
)
} # }