feat: 补充渠道图层字段并按交叠面积排序
This commit is contained in:
+20
-8
@@ -16,7 +16,12 @@ import numpy as np
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
TOOL = "AoiChannel"
|
||||
AOI_FIELDS = ["AOI唯一标签", "AOI名称", "空间信息(几何)"]
|
||||
CHANNEL_LAYER_FIELDS = [
|
||||
"渠道编码", "渠道类型", "全网渠道编码", "渠道名称", "星级",
|
||||
"地理经度", "地理纬度", "连锁", "头腰尾标识",
|
||||
]
|
||||
CHANNEL_FIELDS = ["渠道编码", "全网渠道编码", "渠道名称", "所属渠道管理员工姓名", "地理经度", "地理纬度"]
|
||||
CHANNEL_READ_FIELDS = list(dict.fromkeys([*CHANNEL_LAYER_FIELDS, *CHANNEL_FIELDS]))
|
||||
RADIUS_M = 2000
|
||||
AREA_FIELD = "交叠面积(平方米)"
|
||||
PAIR_CHUNK = 100_000
|
||||
@@ -198,7 +203,7 @@ def channel_sheet(path):
|
||||
book = pd.ExcelFile(path)
|
||||
for sheet in book.sheet_names:
|
||||
columns = pd.read_excel(book, sheet_name=sheet, nrows=0).columns.tolist()
|
||||
if all(name in columns for name in CHANNEL_FIELDS):
|
||||
if all(name in columns for name in CHANNEL_READ_FIELDS):
|
||||
return sheet
|
||||
raise SystemExit("渠道表中没有所需字段")
|
||||
|
||||
@@ -206,11 +211,11 @@ def channel_sheet(path):
|
||||
def read_channels(path):
|
||||
sheet = channel_sheet(path)
|
||||
frame = pd.read_excel(path, sheet_name=sheet, dtype=str)
|
||||
missing = [name for name in CHANNEL_FIELDS if name not in frame.columns]
|
||||
missing = [name for name in CHANNEL_READ_FIELDS if name not in frame.columns]
|
||||
if missing:
|
||||
raise SystemExit("渠道表缺少字段: " + "、".join(missing))
|
||||
frame = frame[CHANNEL_FIELDS].copy()
|
||||
for name in CHANNEL_FIELDS:
|
||||
frame = frame[CHANNEL_READ_FIELDS].copy()
|
||||
for name in CHANNEL_READ_FIELDS:
|
||||
frame[name] = text_column(frame[name])
|
||||
blank = frame["地理经度"].eq("") | frame["地理纬度"].eq("")
|
||||
blank_count = int(blank.sum())
|
||||
@@ -221,7 +226,10 @@ def read_channels(path):
|
||||
invalid = int((~valid).sum())
|
||||
frame = frame.loc[valid].copy()
|
||||
before = len(frame)
|
||||
frame = frame.drop_duplicates(CHANNEL_FIELDS).reset_index(drop=True)
|
||||
frame = frame.drop_duplicates(CHANNEL_READ_FIELDS).reset_index(drop=True)
|
||||
if frame.empty:
|
||||
points = gpd.GeoDataFrame(columns=CHANNEL_READ_FIELDS, geometry=[], crs="EPSG:4326")
|
||||
return points, blank_count, before - len(frame), invalid
|
||||
points = gpd.GeoDataFrame(
|
||||
frame,
|
||||
geometry=shapely.points(pd.to_numeric(frame["地理经度"]), pd.to_numeric(frame["地理纬度"])),
|
||||
@@ -241,7 +249,7 @@ def intersection_area(left, right):
|
||||
|
||||
def overlap_tables(aoi, channels):
|
||||
columns = ["AOI唯一标签", "AOI名称", *CHANNEL_FIELDS, AREA_FIELD]
|
||||
channel_columns = [*CHANNEL_FIELDS, "AOI唯一标签", "AOI名称"]
|
||||
channel_columns = [*CHANNEL_FIELDS, "AOI唯一标签", "AOI名称", AREA_FIELD]
|
||||
empty_aoi = pd.DataFrame(columns=columns)
|
||||
empty_channel = pd.DataFrame(columns=channel_columns)
|
||||
if aoi.empty or channels.empty:
|
||||
@@ -254,7 +262,7 @@ def overlap_tables(aoi, channels):
|
||||
channel_m = channels.to_crs(projected)
|
||||
channel_m = channel_m.set_geometry(shapely.buffer(channel_m.geometry.to_numpy(), RADIUS_M, quad_segs=16))
|
||||
stored = gpd.GeoDataFrame(
|
||||
channels.drop(columns="geometry"),
|
||||
channels[CHANNEL_LAYER_FIELDS],
|
||||
geometry=channel_m.to_crs("EPSG:4326").geometry,
|
||||
crs="EPSG:4326",
|
||||
)
|
||||
@@ -286,7 +294,11 @@ def overlap_tables(aoi, channels):
|
||||
ascending=[False, True, False, True],
|
||||
kind="mergesort",
|
||||
)
|
||||
channel_aoi = table.sort_values(["渠道编码", "AOI唯一标签"], kind="mergesort")
|
||||
channel_aoi = table.sort_values(
|
||||
["渠道编码", AREA_FIELD, "AOI唯一标签"],
|
||||
ascending=[True, False, True],
|
||||
kind="mergesort",
|
||||
)
|
||||
return table[columns], channel_aoi[channel_columns], stored
|
||||
|
||||
|
||||
|
||||
@@ -13,5 +13,5 @@
|
||||
- 存放路径:`Source/实体渠道匹配表/【26年11月11日】实体渠道匹配表v2s_渠道信息.xlsx`
|
||||
- 命名格式:`实体渠道匹配表*.xlsx`
|
||||
- 数据格式:xlsx。使用含下列字段的工作表,当前是 Sheet1。
|
||||
- 字段:渠道编码、全网渠道编码、渠道名称、所属渠道管理员工姓名、地理经度、地理纬度
|
||||
- 说明:经纬度是十进制度。经度或纬度为空的记录先丢弃。其余记录提取这些字段后去重。每个点生成半径 2 公里的圆。交叠面积单位是平方米。
|
||||
- 字段:渠道编码、渠道类型、全网渠道编码、渠道名称、星级、所属渠道管理员工姓名、地理经度、地理纬度、连锁、头腰尾标识
|
||||
- 说明:经纬度是十进制度。经度或纬度为空的记录先丢弃。其余记录提取这些字段后去重。每个点生成半径 2 公里的圆。渠道图层保留渠道编码、渠道类型、全网渠道编码、渠道名称、星级、地理经度、地理纬度、连锁、头腰尾标识。交叠面积单位是平方米。
|
||||
|
||||
Reference in New Issue
Block a user