diff --git a/dags/poi/dag_check_gov_township_diff.py b/dags/poi/dag_check_gov_township_diff.py index 21752cb..0a1d5fb 100644 --- a/dags/poi/dag_check_gov_township_diff.py +++ b/dags/poi/dag_check_gov_township_diff.py @@ -61,6 +61,60 @@ def get_amap_township(lon, lat, key=AMAP_KEY, max_retries=5): return "" +def get_amap_district_geocode(province, city, district, key=AMAP_KEY, max_retries=5): + """ + 调用高德地理编码接口获取区域经纬度 + https://restapi.amap.com/v3/geocode/geo?address=...&key=... + 地址规则:省份+城市+区域 拼接(若城市与区域相同或城市为空/包含区域,避免重复拼接) + 要求返回的 geocodes 中 level == '区县' + 返回: "lon,lat" 或 "" + """ + url = "https://restapi.amap.com/v3/geocode/geo" + + prov_str = str(province or "").strip() + city_str = str(city or "").strip() + dist_str = str(district or "").strip() + + # 拼接地址:注意城市和区域有可能相同,避免重复拼接(如 海南省-文昌市-文昌市 -> 海南省文昌市) + addr_parts = [prov_str] + if city_str and city_str != prov_str: + addr_parts.append(city_str) + if dist_str and dist_str != city_str: + addr_parts.append(dist_str) + address = "".join(addr_parts) + + params = { + "key": key, + "address": address + } + + for attempt in range(max_retries): + try: + resp = requests.get(url, params=params, timeout=5) + if resp.status_code == 200: + data = resp.json() + if data.get("status") == "1": + geocodes = data.get("geocodes", []) + for item in geocodes: + if item.get("level") == "区县": + location = item.get("location", "") + if location and "," in location: + return location.strip() + # 如果没有精确匹配到 level=='区县' + return "" + else: + info = str(data.get("info", "")) + infocode = str(data.get("infocode", "")) + print(f"高德地理编码返回异常: {info}, code: {infocode}") + if infocode == "10021" or "CUQPS_HAS_EXCEEDED_THE_LIMIT" in info: + time.sleep(0.2) + continue + except Exception as e: + print(f"请求高德地理编码失败 (第{attempt+1}次): {e}") + time.sleep(0.5) + return "" + + def check_gov_township_diff(**context): """ 县政府所在乡镇街道比对主任务,支持从 dag_run.conf 或 params 中传入自定义 amap_key @@ -122,7 +176,8 @@ def check_gov_township_diff(**context): lat = location.get("lat") if isinstance(location, dict) else None if not location or lon is None or lat is None or not str(lon).strip() or not str(lat).strip(): - print(f"区域 {province}-{city}-{district}({mzb_district_code}) 缺少有效经纬度,记录并跳过") + print(f"区域 {province}-{city}-{district}({mzb_district_code}) 缺少有效经纬度,调用高德地理编码获取...") + geo_location = get_amap_district_geocode(province, city, district, key=amap_key) missing_loc_list.append({ "ES文档ID": doc_id, "省份": province, @@ -130,6 +185,8 @@ def check_gov_township_diff(**context): "区域": district, "民政部区划代码": mzb_district_code, "统计局区划代码": tjj_district_code, + "经纬,维度": geo_location if geo_location else "", + "人工审核": "未审核", "原因": "缺少有效经纬度(location)", "登记时间": datetime.now().strftime("%Y-%m-%d %H:%M:%S") })