tangles: Anonymise spatial point patterns and raster objects

View source: R/tangles.R

tanglesR Documentation

Anonymise spatial point patterns and raster objects

Description

Performs spatial anonymisation ("tangling") of coordinates through randomized transformation sequences, preserving relative spatial relationships while obscuring true locations. Three transformation types are used: X shift, Y shift, and rotation around a random origin. The sequence and parameters used for anonymisation are recorded and returned for later disentanglement.

Usage

tangles(data = NULL, depth = 3, rasterdata = FALSE, raster_object = FALSE, 
saveTangles = FALSE, exportShapefile = FALSE, path = NULL)

Arguments

data

Either a two-column matrix or data.frame of coordinates, an sf POINT object, or a terra::SpatRaster object.

depth

Integer. Number of transformation steps to apply (default is 3).

rasterdata

Logical. If TRUE, rotations are limited to 90, 180, or 270 degrees to preserve axis alignment in raster data.

raster_object

Logical. Set TRUE if input is a terra::SpatRaster object. All raster values will be mapped to their transformed coordinates.

saveTangles

Logical. If TRUE, writes both the transformed data and detangler metadata to the specified path.

exportShapefile

Logical. If TRUE and input is a point-based dataset (not a raster), the anonymised coordinates are written to a shapefile. No CRS is assigned.

path

Character. Path to directory where outputs will be saved. Defaults tempdir().

Value

A list with two elements:

  • The transformed coordinates (if point input) or a terra::SpatRaster (if raster input)

  • A detangler list containing the transformation log (unpicker) and a unique hash

If saveTangles = TRUE, the following files are written:

  • tangledXY_<hash>.rds or tangledXY_raster_<hash>.rds — the transformed data

  • detangler_<hash>.rds — the metadata required for reverse transformation

  • Optional: shapefile output if exportShapefile = TRUE

Note

For raster input, both rasterdata = TRUE and raster_object = TRUE are usually recommended. This ensures rotation steps align with raster grid expectations.

The detangler object is the critical output. It allows the same transformation sequence to be reversed or applied to related datasets.

Coordinate reference systems are intentionally ignored in this function. Anonymised outputs do not have spatial meaning, but retain topological properties of the input.

If writing shapefiles, no CRS is assigned and no .prj file is written.

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: Using point data.frame
library(digest)
set.seed(1)
pts <- data.frame(X = runif(100), Y = runif(100))
res <- tangles(data = pts, depth = 4)
str(res)

## Example 2: Using sf object
library(sf)
sf_pts <- st_as_sf(pts, coords = c("X", "Y"))
res_sf <- tangles(data = sf_pts, depth = 3, exportShapefile = TRUE)

## Example 3: Using terra raster
library(terra)
ext_path <- system.file("extdata", package = "tangles")
rast.files <- list.files(path = ext_path, full.names = TRUE)
rasters <- terra::rast(rast.files)
res_r <- tangles(data = rasters, depth = 3, rasterdata = TRUE, raster_object = TRUE)
str(res_r)

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