用于空间数据匿名化的地理掩蔽工具
项目描述
MaskMyPy
MaskMyPy是一个(非常 alpha)Python 包,它在GeoPandas geodataframes上执行地理掩码。它提供了两种主要方法:街道掩蔽和甜甜圈掩蔽。
MaskMyPy 还支持使用人口数据的 k-匿名估计和使用地址数据的 k-匿名计算,以及计算敏感点和掩蔽点之间的位移距离。
免责声明:MaskMyPy 按原样提供,不提供任何形式的保证。地理掩蔽是一个难题,需要做出明智的决策和验证。MaskMyPy 为地理掩蔽提供了有用的工具,但不能取代专业知识。
安装
pip install maskmypy
街道掩蔽
街道掩蔽会自动下载 OpenStreetMap 道路网络数据,并使用它在地理上掩蔽您的敏感点。它的工作原理是首先下载道路网络数据,将每个敏感点捕捉到网络上最近的节点(交叉口或死胡同),然后计算该节点与最近的 x 个节点池之间的平均网络距离(例如网络上最近的 20 个节点,称为搜索深度)。这个平均距离就是目标位移距离。最后,它从池中选择一个与起始节点的网络距离最接近目标位移距离的节点。
用法:要对包含搜索深度值为 20 的敏感点的地理数据框进行街道掩码,代码如下:
from maskmypy import Street
streetmask = Street(
sensitive_gdf, # Name of the sensitive geodataframe
depth=20, # The search depth value used to calculate displacement distances.
extent_expansion_distance=2000, # Used to download road network data surrounding the study area. Needs to be sufficiently large to reduce edge effects. Increasing reduces edge effects, but uses more memory.
max_street_length=500) # Optional, but recommended that you read below for full explanation of what this does.
streetmask.execute() # Single threaded by default. Add `parallel=True` as parameter to run on all CPU cores, drastically increasing performance.
masked_gdf = streetmask.masked
关于 max_street_length:当将点捕捉到街道网络时,算法会检查以确保最近的节点实际连接到网络并且其邻居距离不超过 max_street_length(以米为单位)。如果不是,则选择、检查下一个最近的可行节点,依此类推。这可以作为一种健全性检查,以防止过大的掩蔽距离。随意将其更改为您认为合适的任何内容。
甜甜圈掩蔽
用法: 要对包含敏感点的地理数据帧执行基本的甜甜圈地理掩码,最大位移距离为 500 米,最小位移距离为最大距离的 20%(即 100 米),代码如下所示:
from maskmypy import Donut
donutmask = Donut(
sensitive_gdf=sensitive_gdf, # Name of the sensitive geodataframe
max_distance=250, # The maximum possible distance that points are displaced
donut_ratio=0.1, # The ratio used to define the minimum distance points are displaced
distribution='uniform', # The distribution to use when displacing points. Other options include 'gaussian' and 'areal'. 'Areal' distribution means points are more likely to be displaced further within the range.
container_gdf=container_gdf) # Optional, a geodataframe used to ensure that points do not leave a particular area.
donutmask.execute()
masked_gdf = donutmask.masked
要使用最大 k-匿名 1000 和最小 200 以及名为 population_gdf 的人口普查地理数据框执行完整的环形地理掩码(即使用人口普查数据和目标 k-匿名范围而不是距离范围),代码将如下所示:
from maskmypy import Donut_MaxK
donutmask = Donut_MaxK(
sensitive_gdf, # Name of the sensitive geodataframe
population_gdf=population_gdf, # Name of the census geodataframe
population_column='pop', # Name of the column containing the population field
max_k_anonymity=1000, # The maximum possible k-anonymity value
donut_ratio=0.2, # The ratio used to define the minimum possible k-anonymity value.
distribution='uniform', # The distribution to use when displacing points. Other options include 'gaussian' and 'areal'. 'Areal' distribution means points are more likely to be displaced further within the range.
container_gdf=container_gdf) # Optional, a geodataframe used to ensure that points do not leave a particular area.
donutmask.execute()
masked_gdf = donutmask.masked
K-匿名
Maskmypy 能够计算掩蔽后每个点的 k-匿名性。有两种方法可用:估计和精确计算。k-匿名性的估计值是从人口普查数据中推断出来的,并假设每个人口普查多边形内的人口是均匀分布的。基于地址的 k-匿名更准确,并使用实际家庭地址数据来计算 k-匿名。
估计 K-匿名性
用法: 数据被屏蔽后,使用人口普查数据估计 k-anoynmity 将如下所示,并将在屏蔽的地理数据框中添加一列:
mask.k_anonymity_estimate(
population_gdf=population_gdf, # Name of the census geodataframe. Not necessary if you already included this parameter in the original masking steps.
population_column='pop') # Name of the column containing the population field. Not necessary if you already included this parameter in the original masking steps.
计算 K-匿名性
用法: 数据被屏蔽后,计算基于地址的 k-anoynmity 将如下所示,并将在屏蔽的地理数据框中添加一列:
mask.k_anonymity_actual(address_points_gdf='') # Name of the geodataframe including address points.
位移距离
用法: 要向包含实际位移距离(以米为单位)的掩码地理数据框添加一列,只需执行:
mask.displacement_distance()
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。