使用gdal均匀筛选点矢量
作用:
通过计算各点之间的欧式距离,筛选出符合目标的、均匀发布在空间中的N个数据点。
效果示意图
运行环境
python 3.10
安装:tqdm、numpy和tqdm这三个库
完整代码
import numpy as np
from osgeo import ogr, osr
from tqdm import tqdm
# 代码作用:通过计算各点之间的欧式距离,筛选出符合目标的、均匀发布在空间中的N个数据点。
# 定义需要采样的个数
n_samples = 100
input_path = r"测试数据\村点.shp"
output_path = r"测试数据\samples.shp"
# 1. 读取原始点数据
driver = ogr.GetDriverByName('ESRI Shapefile')
inds = driver.Open(input_path, 0)
layer = inds.GetLayer()
# 2. 提取点坐标和属性
coords = []
attrs = []
for feature in layer:
geom = feature.GetGeometryRef()
coords.append((geom.GetX(), geom.GetY()))
attrs.append([feature.GetField(i) for i in range(feature.GetFieldCount())])
coords = np.array(coords)
attrs = np.array(attrs)
# 3. 定义距离函数
def distance(p1, p2):
return np.sqrt(np.sum((p1 - p2)**2))
# 4. 随机选择第一个点
idx = np.random.choice(coords.shape[0], 1)
samples = coords[idx]
sample_attrs = attrs[idx]
# 5. 选择空间均衡的采样点
for _ in tqdm(range(n_samples - 1)):
dists = np.array([np.min(np.array([distance(p, s) for s in samples])) for p in coords])
idx = np.argmax(dists)
samples = np.append(samples, [coords[idx]], axis=0)
sample_attrs = np.append(sample_attrs, [attrs[idx]], axis=0)
coords = np.delete(coords, idx, axis=0)
attrs = np.delete(attrs, idx, axis=0)
# 6. 将采样点转为gdal几何对象
out_samples = []
for sample in samples:
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(sample[0], sample[1])
out_samples.append(point)
# 7. 创建新的矢量层并写入采样点
out_driver = ogr.GetDriverByName('ESRI Shapefile')
out_ds = out_driver.CreateDataSource(output_path)
out_layer = out_ds.CreateLayer('samples', layer.GetSpatialRef(), ogr.wkbPoint)
# 添加属性字段
for i in range(len(layer.schema)):
field_defn = layer.schema[i]
out_layer.CreateField(field_defn)
# 写入采样点要素
for i, sample in enumerate(out_samples):
feature = ogr.Feature(out_layer.GetLayerDefn())
feature.SetGeometry(sample)
for j, attr in enumerate(sample_attrs[i]):
feature.SetField(j, attr)
out_layer.CreateFeature(feature)
out_layer = None
out_ds = None