detangles: Revert anonymised point patterns or raster objects

View source: R/detangles.R

detanglesR Documentation

Revert anonymised point patterns or raster objects

Description

Reverses the spatial anonymisation applied by the tangles function, restoring original XY coordinates or raster data. Requires a matching detangler object containing the transformation sequence and hash key. The restoration process preserves spatial relationships and supports both point and raster inputs.

Usage

detangles(
  data = NULL,
  tanglerInfo = NULL,
  raster_object = FALSE,
  stub = NULL,
  hash_key = NULL,
  saveTangles = FALSE,
  exportShapefile = FALSE,
  path = NULL
)

Arguments

data

A 2-column matrix or data.frame of spatial coordinates, an sf POINT object, or a terra::SpatRaster object.

tanglerInfo

A detangler object returned by tangles(), containing the anonymisation steps and associated hash key.

raster_object

Logical; set to TRUE if the input is a raster object. This enables value restoration for all raster cells.

stub

Optional character string used in output file naming. Helps distinguish saved files.

hash_key

Character string representing the hash key to verify identity of the detangler. Must match tanglerInfo\$hash.

saveTangles

Logical; if TRUE, saves untangled data as .rds (and optionally raster .tif) files to path.

exportShapefile

Logical; if TRUE and input is point-based, a shapefile of the untangled coordinates is written (with undefined CRS).

path

Directory to save output files if saveTangles or exportShapefile is enabled. Defaults tempdir().

Value

A restored data.frame of spatial XY coordinates or a terra::SpatRaster object, depending on input type.

If saveTangles = TRUE, corresponding files are saved to path, including:

  • detangledXY_<stub>_<hash>.rds

  • detangledXY_raster_<name>.tif (for raster input)

  • detangledXY_<stub>_<hash>.shp (for point shapefile export, if exportShapefile = TRUE)

Note

If the hash_key provided does not match the one in the detangler object, the function stops to prevent incorrect spatial restoration.

When writing shapefiles, no CRS is assigned. This ensures that untangled data does not imply real-world georeferencing but retains relative spatial relationships.

Outputs can be safely saved and shared using the embedded hash key and transformation record.

Author(s)

Brendan Malone

References

  • CM O’Keefe, S Otorepec, M Elliot, E Mackey, and K O’Hara (2017) The De-Identification Decision Making Framework. CSIRO Reports EP173122 and EP175702. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.4225/08/59c169433efd4")}

Examples


## EXAMPLE 1: Untangle point data.frame and export shapefile
library(sf)
set.seed(1)

# Simulate XY data
pts <- data.frame(X = runif(100), Y = runif(100))

# Anonymise
tangled <- tangles(data = pts, depth = 4, saveTangles = FALSE)

# Restore points
restored_pts <- detangles(
  data = tangled[[1]],
  tanglerInfo = tangled[[2]],
  raster_object = FALSE,
  stub = "points",
  hash_key = tangled[[2]]$hash,
  exportShapefile = TRUE
)

## EXAMPLE 2: Untangle from sf POINT input
sf_pts <- st_as_sf(pts, coords = c("X", "Y"))

# Anonymise sf object
tangled_sf <- tangles(data = sf_pts, depth = 3)

# Restore using sf input
restored_from_sf <- detangles(
  data = tangled_sf[[1]],
  tanglerInfo = tangled_sf[[2]],
  stub = "sf_restore",
  hash_key = tangled_sf[[2]]$hash,
  exportShapefile = TRUE
)

## EXAMPLE 3: Untangle raster data (terra)
library(terra)
ext_path <- system.file("extdata", package = "tangles")
rast.files <- list.files(path = ext_path, full.names = TRUE)
rasters <- terra::rast(rast.files)

# Anonymise raster
tangled_rast <- tangles(data = rasters, depth = 3, rasterdata = TRUE, raster_object = TRUE)

# Restore raster
restored_rast <- detangles(
  data = tangled_rast[[1]],
  tanglerInfo = tangled_rast[[2]],
  raster_object = TRUE,
  stub = "raster_demo",
  hash_key = tangled_rast[[2]]$hash,
  saveTangles = TRUE
)

tangles documentation built on June 8, 2025, 11:38 a.m.