geomask

Geographic masking of confidential point data (donut, adaptive donut, AAE, location swapping, aggregation with small-cell suppression) with spatial k-anonymity, privacy and utility metrics.

Version 0.2.3 · Python >=3.9 · MIT licence
pip install geomask
PyPISourceIssuesChangelogCite (DOI)

geomask

PyPI version Python versions pipeline status License: MIT DOI Downloads

Geographic masking of confidential point data, with the privacy and utility metrics you need to document the trade-off.

Public-health, social-science and urban researchers often hold point data that identify people: home addresses of patients, locations of incidents, survey respondents' dwellings. Publishing those points, or even a detailed map of them, can let anyone with a street map work out who is who. geomask implements the established geomasking methods from the literature and, just as importantly, the metrics that tell you how much privacy a mask buys and how much analytical value it costs.

Masking is not anonymisation by itself. Read the ethics and limitations section before you share anything.

Installation

pip install geomask            # NumPy only
pip install "geomask[fast]"    # + SciPy KD-trees for large data sets

Python 3.9 or later.

Quick start

All examples use synthetic data generated by geomask.synthetic_city; no real person is represented anywhere in this project.

import geomask as gm

town = gm.synthetic_city(n_population=5000, n_cases=300, seed=1)
cases, homes = town.cases, town.population   # (n, 2) arrays in metres

# Donut mask: every point moves between 50 m and 300 m.
masked = gm.donut_mask(cases, min_radius=50, max_radius=300, seed=1)

# Population-adaptive donut: radii grow in sparse areas and every masked point
# is re-drawn until at least 10 households are closer to it than the true home.
safe = gm.donut_mask(cases, 50, 300, seed=1, population=homes, k=10)

k = gm.spatial_k_anonymity(cases, safe, homes)
print(k.min())                          # >= 10, audited independently

gm.report(gm.evaluate(cases, safe, population=homes, k=10))

Comparing methods

examples/compare_methods.py masks the same synthetic cases with every method and prints the trade-off table:

results = gm.compare(cases, {
    "perturb r=250": gm.random_perturbation(cases, 250, seed=1),
    "gaussian s=100": gm.gaussian_displacement(cases, 100, seed=1),
    "donut 50-300": gm.donut_mask(cases, 50, 300, seed=1),
    "donut k>=10": gm.donut_mask(cases, 50, 300, seed=1, population=homes, k=10),
    "swap r=300": gm.location_swap(cases, homes, 300, seed=1),
    "AAE k=20": gm.adaptive_areal_elimination(cases, homes, k=20, cell_size=100, seed=1),
    "grid 250 k=5": gm.aggregate_to_grid(cases, 250, k=5).masked_points,
    "hex 150 k=5": gm.aggregate_to_grid(cases, 150, k=5, shape="hex").masked_points,
}, population=homes, k=10)
gm.report(results)
method          k min  k med   <k %   risk  NN re-id  disp m  min m  dL max m  KDE r  Moran I  hot J  suppr
--------------  -----  -----  -----  -----  --------  ------  -----  --------  -----  -------  -----  -----
perturb r=250       0     42  20.0%  0.081     19.7%     165     19      26.9  0.999    0.700   0.36      0
gaussian s=100      0     19  35.0%  0.174     34.7%     118      4      23.1  0.998    0.702   0.47      0
donut 50-300        0     62  13.7%  0.049     15.7%     203     55      37.2  0.998    0.738   0.67      0
donut k>=10        10     66   0.0%  0.023      8.7%     224     66      31.9  0.997    0.755   0.62      0
swap r=300          1     88  12.0%  0.035     13.3%     207     26      45.3  0.989    0.663   0.53      0
AAE k=20            0     24  27.0%  0.122     32.3%     238      6      45.2  0.993    0.678   0.55      0
grid 250 k=5        0     18  28.5%  0.106      6.3%      93     14     727.8  0.932    0.507   0.25    170
hex 150 k=5         0     18  30.1%  0.120      6.7%      93     21     668.0  0.945    0.538   0.33    167
n = 300; k threshold = 10; Moran I of original counts = 0.708

How to read it: only the population-adaptive donut guarantees that every point has at least k = 10 households closer to its released location than the true one (k min), while keeping the density surface almost unchanged (KDE r 0.997). Fixed-radius masks leave some rural points weakly protected (<k %). Aggregation with suppression drops more than half the points in this small sample (suppr) and distorts second-order structure (dL max). AAE's guarantee is areal (each release area holds at least k residents), which the point-based k min column does not measure - see below.

Methods

Method Function What it does Privacy Utility Reference
Random perturbation random_perturbation Uniform random point within radius r (uniform over the disk, or uniform distance) Weak: some points barely move Good at scales above r Armstrong et al. 1999
Gaussian displacement gaussian_displacement Adds N(0, sigma^2) to x and y; distance is Rayleigh Weak: unbounded small moves Good, smooth Armstrong et al. 1999; Zandbergen 2014
Donut masking donut_mask Random point in an annulus [r_min, r_max] Guaranteed minimum displacement Bounded error r_max Hampton et al. 2010
Population-adaptive donut donut_mask(population=, k=) Radii from the k-th / k_max-th nearest population unit; candidates re-drawn until spatial k-anonymity >= k Verified k per point Small moves in dense areas, larger in sparse Allshouse et al. 2010; Hampton et al. 2010
Grid / hexagon snapping snap_to_grid, snap_to_hex Moves points to cell centres Points in a cell become identical Resolution = cell size Armstrong et al. 1999
Aggregation + suppression aggregate_to_grid, aggregate_to_voronoi, aggregate_by_label Counts per cell / Voronoi cell / area; counts 0 < c < k suppressed Small-cell rule Loses point detail; suppression loses rural cases Armstrong et al. 1999
Location swapping location_swap Replaces each point with a random real address within r (never itself) Plausible locations; resists "point in a lake" checks Similar to donut Zhang et al. 2017
Adaptive areal elimination adaptive_areal_elimination Merges grid cells until each holds >= k people, then relocates points randomly inside their merged area Areal k-anonymity Adaptive: small areas in dense places Kounadi & Leitner 2016

Notes on parameters:

Metrics

Metric Function Meaning
Spatial k-anonymity spatial_k_anonymity(orig, masked, population) Number of population units strictly closer to the masked point than the true location (the true location itself is never counted, so the anonymity set has k + 1 members).
Expected re-identification risk expected_reidentification_risk(k) mean(1 / (k + 1)): probability that an adversary picking uniformly among the candidates guesses right.
Nearest-neighbour linkage rate nearest_neighbour_reidentification_rate Share of masked points whose nearest true location is their own; models an adversary holding the list of true locations.
Displacement statistics displacement_stats Mean, median, SD, min, 5th / 95th percentile and max of distances moved.
Ripley's K / L difference ripley_k, ripley_l, ripley_l_difference L(r) = sqrt(K(r) / pi) for original and masked points over a shared window; max_abs_difference in metres. No edge correction (biases both patterns equally).
Kernel-density correlation kde_correlation, kernel_density Pearson correlation of Gaussian KDE surfaces (Scott bandwidth by default).
Cluster preservation cluster_preservation, morans_i Moran's I (rook contiguity) of gridded counts before and after masking, and Jaccard overlap of hot-spot cells.
Mean displacement mean_displacement Average distance moved, metres.

gm.evaluate() computes all of them; gm.compare() evaluates several masks with identical settings; gm.report() prints the table. Suppressed points (NaN rows) are excluded from the masked pattern and counted in suppr.

Command line

# Donut mask a lon/lat CSV (coordinates are auto-detected and projected to UTM)
geomask donut cases.csv --x lon --y lat --min 50 --max 300 --seed 1 -o masked.csv

# Population-adaptive donut with k >= 10
geomask donut cases.csv --x lon --y lat --min 50 --max 300 --k 10 \
    --population addresses.csv --seed 1 -o masked.csv
# (the k check keeps a safety margin that covers the --decimals rounding, and
#  the k-anonymity of the coordinates actually written is reported on stderr)

# Other methods
geomask perturb  cases.csv --radius 250 -o out.csv
geomask gaussian cases.csv --sigma 100 -o out.csv
geomask grid     cases.csv --cell 250 --k 5 -o out.csv
geomask hex      cases.csv --size 150 --k 5 -o out.csv
geomask voronoi  cases.csv --seeds clinics.csv --k 5 -o out.csv
geomask swap     cases.csv --radius 300 --population addresses.csv -o out.csv
geomask aae      cases.csv --k 20 --cell 100 --population addresses.csv -o out.csv

# Privacy / utility report (rows must be in the same order)
geomask evaluate cases.csv masked.csv --x lon --y lat --population addresses.csv --k 10
geomask evaluate cases.csv masked.csv --json

All other columns are copied unchanged; use --drop name address ... to remove direct identifiers. Suppressed points are written with empty coordinates. --coords lonlat|projected overrides auto-detection and --projection local uses an equirectangular projection instead of UTM. python -m geomask works the same as geomask.

Reproducibility

Every random function accepts seed (an int, None or a numpy.random.Generator). The same seed, input and version give identical output. Do not publish the seed together with the masked data: with the seed and the method parameters, an adversary who knows some true locations can test candidate locations. Store the seed with your confidential project records.

Ethics and limitations

API overview

Masks         random_perturbation, gaussian_displacement, donut_mask (DonutResult),
              location_swap, snap_to_grid, snap_to_hex,
              aggregate_to_grid, aggregate_to_voronoi, aggregate_by_label (AggregationResult),
              adaptive_areal_elimination (AAEResult)
Privacy       spatial_k_anonymity, expected_reidentification_risk,
              nearest_neighbour_reidentification_rate, displacement, displacement_stats
Utility       ripley_k, ripley_l, ripley_l_difference, kernel_density, kde_correlation,
              morans_i, cluster_preservation, mean_displacement
Evaluation    evaluate (EvaluationResult), compare, report
Projection    make_projection, UTM, LocalEquirectangular, utm_zone
Data          synthetic_city (SyntheticCity)
Backend       set_backend("numpy" | "scipy" | "auto"), get_backend

References

Citing

@software{eminoglu_geomask,
  author  = {Eminoğlu, Yusuf},
  title   = {geomask: geographic masking of confidential point data},
  year    = {2026},
  version = {0.2.3},
  doi     = {10.5281/zenodo.24063373},
  url     = {https://doi.org/10.5281/zenodo.24063373}
}

If you use geomask in research, please cite it using the metadata in CITATION.cff and cite the original method papers above.

Related Geophilo SDKs

License

MIT © 2026 Yusuf Eminoğlu. See LICENSE.

Other Geophilo SDKs