feat: 输出渠道点图层并匹配渠道点落入的 AOI

This commit is contained in:
2026-11-13 16:56:16 +08:00
parent 180ff5030d
commit ab39627b63
4 changed files with 35 additions and 4 deletions
+32 -1
View File
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""AOI 与渠道 2 公里范围的交叠匹配。"""
"""AOI 与渠道 2 公里范围交叠,并匹配渠道点落入的 AOI。"""
import json
import os
@@ -303,6 +303,33 @@ def overlap_tables(aoi, channels):
return table[columns], channel_aoi[channel_columns], stored
def point_aoi_table(aoi, channels):
columns = [*CHANNEL_FIELDS, "AOI唯一标签", "AOI名称"]
if aoi.empty or channels.empty:
return pd.DataFrame(columns=columns)
tree = STRtree(aoi.geometry.to_numpy())
pairs = tree.query(channels.geometry.to_numpy(), predicate="intersects")
if pairs.shape[1] == 0:
return pd.DataFrame(columns=columns)
channel_index = pairs[0]
aoi_index = pairs[1]
table = pd.DataFrame({
"渠道编码": channels["渠道编码"].to_numpy()[channel_index],
"全网渠道编码": channels["全网渠道编码"].to_numpy()[channel_index],
"渠道名称": channels["渠道名称"].to_numpy()[channel_index],
"所属渠道管理员工姓名": channels["所属渠道管理员工姓名"].to_numpy()[channel_index],
"地理经度": channels["地理经度"].to_numpy()[channel_index],
"地理纬度": channels["地理纬度"].to_numpy()[channel_index],
"AOI唯一标签": aoi["AOI唯一标签"].to_numpy()[aoi_index],
"AOI名称": aoi["AOI名称"].to_numpy()[aoi_index],
})
return table.sort_values(["渠道编码", "AOI唯一标签"], ascending=[True, True], kind="mergesort")
def channel_points(channels):
return gpd.GeoDataFrame(channels[CHANNEL_LAYER_FIELDS], geometry=channels.geometry, crs=channels.crs)
def write_gpkg(layer, path, name):
layer.to_file(path, layer=name, driver="GPKG", spatial_index=True, encoding="UTF-8")
with sqlite3.connect(path) as connection:
@@ -394,19 +421,23 @@ def main():
aoi, aoi_blank, aoi_removed, aoi_invalid = read_aoi(aoi_path)
channels, channel_blank, channel_removed, channel_invalid = read_channels(channel_path)
aoi_channel, channel_aoi, circles = overlap_tables(aoi, channels)
contained = point_aoi_table(aoi, channels)
staging = temporary / "out"
staging.mkdir()
write_gpkg(aoi, staging / "AOI.gpkg", "AOI")
write_gpkg(circles, staging / "Channel.gpkg", "Channel")
write_gpkg(channel_points(channels), staging / "ChannelPoint.gpkg", "ChannelPoint")
with pd.ExcelWriter(staging / "result.xlsx", engine="openpyxl") as writer:
aoi_channel.to_excel(writer, sheet_name="AOI-渠道", index=False)
channel_aoi.to_excel(writer, sheet_name="渠道-AOI", index=False)
contained.to_excel(writer, sheet_name="渠道-单点-AOI", index=False)
output = ROOT / "Output" / TOOL / stamp
output.parent.mkdir(parents=True, exist_ok=True)
shutil.move(staging, output)
print(f"AOI {len(aoi)},丢弃空几何 {aoi_blank},去掉重复 {aoi_removed},无效几何 {aoi_invalid}")
print(f"渠道 {len(channels)},丢弃空坐标 {channel_blank},去掉重复 {channel_removed},无效坐标 {channel_invalid}")
print(f"交叠 {len(aoi_channel)}")
print(f"渠道点落入 {len(contained)}")
print(f"输出 {relative(output)}")
finally:
cleanup(temporary)