feat(poi): 将缺少有效经纬度的区域单独导出到Excel的'缺少经纬度区域'Sheet

This commit is contained in:
王文艳
2026-09-16 10:15:17 +08:00
parent 3e927f2c6d
commit 8f502b42b7
+30 -15
View File
@@ -93,6 +93,7 @@ def check_gov_township_diff(**context):
district_docs = es_client.scan(index=INDEX_NAME, query=query_district) district_docs = es_client.scan(index=INDEX_NAME, query=query_district)
diff_list = [] diff_list = []
missing_loc_list = []
total_districts = 0 total_districts = 0
processed_count = 0 processed_count = 0
@@ -117,18 +118,26 @@ def check_gov_township_diff(**context):
tjj_district_code = source.get("tjj_district_code", "") tjj_district_code = source.get("tjj_district_code", "")
location = source.get("location", {}) location = source.get("location", {})
if not location or "lon" not in location or "lat" not in location: lon = location.get("lon") if isinstance(location, dict) else None
print(f"区域 {province}-{city}-{district}({mzb_district_code}) 缺少有效经纬度,跳过") lat = location.get("lat") if isinstance(location, dict) else None
continue
lon = location.get("lon") if not location or lon is None or lat is None or not str(lon).strip() or not str(lat).strip():
lat = location.get("lat") print(f"区域 {province}-{city}-{district}({mzb_district_code}) 缺少有效经纬度,记录并跳过")
if not lon or not lat: missing_loc_list.append({
"ES文档ID": doc_id,
"省份": province,
"城市": city,
"区域": district,
"民政部区划代码": mzb_district_code,
"统计局区划代码": tjj_district_code,
"原因": "缺少有效经纬度(location)",
"登记时间": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
})
continue continue
processed_count += 1 processed_count += 1
if processed_count % 50 == 0: if processed_count % 50 == 0:
print(f"已处理 {processed_count} 个县/市/旗, 当前发现差异 {len(diff_list)} 条...") print(f"已处理 {processed_count} 个县/市/旗, 当前发现差异 {len(diff_list)} 条, 缺少经纬度 {len(missing_loc_list)} 条...")
# 2. 调用高德 API 获取 township(传入可配置的 amap_key) # 2. 调用高德 API 获取 township(传入可配置的 amap_key)
amap_township = get_amap_township(lon, lat, key=amap_key) amap_township = get_amap_township(lon, lat, key=amap_key)
@@ -198,17 +207,22 @@ def check_gov_township_diff(**context):
"排查时间": datetime.now().strftime("%Y-%m-%d %H:%M:%S") "排查时间": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}) })
print(f"[{datetime.now()}] 扫描完成!共遍历区域: {processed_count} 个,发现不一致/缺失记录: {len(diff_list)} 条") print(f"[{datetime.now()}] 扫描完成!共遍历有效区域: {processed_count} 个,缺少经纬度区域: {len(missing_loc_list)} 个,发现不一致/缺失街道: {len(diff_list)} 条")
# 5. 生成 Excel 并邮件发送 # 5. 生成 Excel 并邮件发送(包含两个 Sheet)
tmp_dir = os.path.join(os.path.expanduser('~'), 'tmp', 'gov_diff') tmp_dir = os.path.join(os.path.expanduser('~'), 'tmp', 'gov_diff')
os.makedirs(tmp_dir, exist_ok=True) os.makedirs(tmp_dir, exist_ok=True)
today_str = datetime.now().strftime('%Y%m%d_%H%M%S') today_str = datetime.now().strftime('%Y%m%d_%H%M%S')
excel_path = os.path.join(tmp_dir, f"县政府所在乡镇街道差异排查_{today_str}.xlsx") excel_path = os.path.join(tmp_dir, f"县政府所在乡镇街道差异排查_{today_str}.xlsx")
df = pd.DataFrame(diff_list) with pd.ExcelWriter(excel_path, engine='openpyxl') as writer:
df.to_excel(excel_path, index=False, engine='openpyxl') df_diff = pd.DataFrame(diff_list)
print(f"排查明细已写入 Excel: {excel_path}") df_diff.to_excel(writer, sheet_name='街道差异名单', index=False)
df_missing = pd.DataFrame(missing_loc_list)
df_missing.to_excel(writer, sheet_name='缺少经纬度区域', index=False)
print(f"排查明细已写入 Excel: {excel_path} (含 '街道差异名单' 与 '缺少经纬度区域' 两个 Sheet)")
# 发送邮件 # 发送邮件
receiver = "wangwenyan@fangjia.com" receiver = "wangwenyan@fangjia.com"
@@ -219,10 +233,11 @@ def check_gov_township_diff(**context):
<p>您好!</p> <p>您好!</p>
<p>系统已完成 206 ES <code>{INDEX_NAME}</code> 索引中县政府所在乡镇街道(<code>has_gov='是'</code>)与高德逆地理街道(<code>township</code>)的比对排查,详情如下:</p> <p>系统已完成 206 ES <code>{INDEX_NAME}</code> 索引中县政府所在乡镇街道(<code>has_gov='是'</code>)与高德逆地理街道(<code>township</code>)的比对排查,详情如下:</p>
<ul> <ul>
<li><b>总排查区域数:</b> {processed_count} 个</li> <li><b>比对县/市/旗区域数:</b> {processed_count} 个</li>
<li><b>发现差异/待核对记录数:</b> {len(diff_list)} 条</li> <li><b>发现街道差异/待核对记录数:</b> {len(diff_list)} 条(见 Sheet: 街道差异名单)</li>
<li><b>缺少经纬度待补全区域数:</b> {len(missing_loc_list)} 个(见 Sheet: 缺少经纬度区域)</li>
</ul> </ul>
<p>详细差异名单已附在附件 Excel 中,请人工审核确认。</p> <p>详细名单已整理在附件 Excel 中,请人工审核确认。</p>
""" """
print(f"正在发送审核邮件至: {receiver}, 抄送: {cc_receiver} ...") print(f"正在发送审核邮件至: {receiver}, 抄送: {cc_receiver} ...")