Reads a spatial file (.zip containing a single shapefile, .shp, .gpkg,
or .geojson), drops Z/M dimensions, replaces non-ASCII characters in every
attribute column, reprojects the geometry to a target CRS and writes the
result to a user-provided output path. The output format is determined by
the extension of output and may differ from the input format (for example,
a .shp can be cleaned and written as .gpkg).
clean_geo(path, output, crs = 4326, encoding = "ISO-8859-1", quiet = FALSE)Path to the input spatial file. Must be .zip (containing
exactly one shapefile), .shp, .gpkg, or .geojson. Alternatively, an
in-memory sf object (for example read and filtered with read_geo())
may be supplied directly; it is cleaned and written to output.
Path to the output file. Required. The extension determines
the output format and must also be one of .zip, .shp, .gpkg, or
.geojson. Existing files at output are overwritten.
Target coordinate reference system passed to
sf::st_transform(). Defaults to EPSG:4326 (WGS84).
Encoding string used when writing shapefile attribute
tables (passed as layer_options = "ENCODING=<encoding>"). Applies only
to .shp and .zip outputs. Defaults to "ISO-8859-1".
Logical. If TRUE, suppress progress messages.
Invisibly returns the normalized output path (character).
The input may also be an in-memory sf object, so a layer read
and pre-filtered with read_geo() can be written out directly without
round-tripping through a file.
This function replaces a standalone batch script that cleaned shapefiles
from a client geospatial portal. The non-ASCII replacement step relies on
textclean::replace_non_ascii(); textclean lives in Suggests:, so the
function stops with an informative error if it is not installed.
Other geo-io:
read_gdb(),
read_geo(),
read_kmz(),
read_sf_zip()
# \donttest{
if (requireNamespace("textclean", quietly = TRUE)) {
z <- system.file("extdata", "misc_example.zip", package = "misc")
if (nzchar(z) && file.exists(z)) {
out <- tempfile(fileext = ".zip")
clean_geo(z, out)
out_gpkg <- tempfile(fileext = ".gpkg")
clean_geo(z, out_gpkg)
}
# An in-memory sf object can be passed directly as `path`.
s <- sf::st_sf(
id = 1L, name = "caf\u00e9",
geometry = sf::st_sfc(sf::st_point(c(1, 2)), crs = 4326)
)
out_sf <- tempfile(fileext = ".gpkg")
clean_geo(s, out_sf)
}
#> • Cleaning in-memory sf object
#> • Replacing non-ASCII characters and reprojecting
#> • Writing /tmp/RtmpRkUiJd/file1fc168a7cdf2.gpkg
#> ✔ /tmp/RtmpRkUiJd/file1fc168a7cdf2.gpkg written!
# }