#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pandas as pd
import json
import time
import os
from urllib.request import urlopen, quote

# 添加skill scripts到路径
skill_dir = r'C:\Users\xiaopeng\.claude\skills\geocoding-cn\scripts'
if skill_dir not in os.sys.path:
    os.sys.path.insert(0, skill_dir)

from coordinate_transform import CoordinateTransformer

# 读取数据
input_file = r'E:\Project\2026_KG_ICH\data\黑龙江国家级和省级非遗名单-地理编码-最终.xlsx'
df = pd.read_excel(input_file)

# 百度地图API配置
AK = "L5SlQ1Kwmg6zaESvmc6RKG37yK2va7Ry"
BASE_URL = 'https://api.map.baidu.com/geocoding/v3/'

def geocode_with_retry(address):
    """尝试多次地理编码，使用不同的地址格式"""
    attempts = [
        address,  # 原始地址
        f"黑龙江省{address}",  # 添加省名
        f"{address}黑龙江",  # 省名在后
        f"中国黑龙江省{address}",  # 添加国家
    ]

    for attempt in attempts:
        try:
            encoded_address = quote(attempt)
            url = f'{BASE_URL}?address={encoded_address}&output=json&ak={AK}'
            req = urlopen(url, timeout=10)
            response = req.read().decode()
            result = json.loads(response)

            if result['status'] == 0:
                location = result['result']['location']
                return location['lat'], location['lng'], attempt
        except Exception as e:
            continue

        time.sleep(0.2)

    return None, None, None

# 问题记录索引（0-based）
problematic_indices = [20, 127, 148, 161, 185, 199, 214, 58, 223]

print("=== 重新地理编码问题记录 ===\n")

success_count = 0
for idx in problematic_indices:
    if idx >= len(df):
        continue

    row = df.iloc[idx]
    original_address = str(row.iloc[1])  # 项目保护单位列

    print(f"Row {idx+1}: {original_address}")
    print(f"   Old coords: {row['wgs84_lat']:.4f}N, {row['wgs84_lon']:.4f}E")

    # 尝试重新地理编码
    lat, lon, used_address = geocode_with_retry(original_address)

    if lat and lon:
        # 转换为WGS84
        wgs_lon, wgs_lat = CoordinateTransformer.bd09_to_wgs84(lon, lat)

        print(f"   New BD09: {lat:.4f}N, {lon:.4f}E")
        print(f"   New WGS84: {wgs_lat:.4f}N, {wgs_lon:.4f}E")
        print(f"   Used address: {used_address}")

        # 检查是否在合理范围内
        if 43 <= wgs_lat <= 53 and 121 <= wgs_lon <= 135:
            print(f"   OK: Within Heilongjiang range")
            # 更新数据
            df.at[idx, 'bd09_lat'] = lat
            df.at[idx, 'bd09_lon'] = lon
            df.at[idx, 'wgs84_lat'] = wgs_lat
            df.at[idx, 'wgs84_lon'] = wgs_lon
            df.at[idx, 'geocode_status'] = 'success'
            success_count += 1
        else:
            print(f"   WARNING: Still outside range (43-53N, 121-135E)")
    else:
        print(f"   FAILED: Geocoding failed")

    print()
    time.sleep(0.3)

# 保存更新后的文件
output_file = r'E:\Project\2026_KG_ICH\data\黑龙江国家级和省级非遗名单-地理编码-最终-修正.xlsx'
df.to_excel(output_file, index=False)
print(f"\n{'='*60}")
print(f"Correction complete!")
print(f"Successfully corrected: {success_count}/{len(problematic_indices)} records")
print(f"Saved to: {output_file}")
