|
| 1 | +import pandas as pd |
| 2 | +import requests |
| 3 | +import time |
| 4 | +import configparser |
| 5 | + |
| 6 | +# 读取配置文件 |
| 7 | +config = configparser.ConfigParser() |
| 8 | +# config.read('config.ini') |
| 9 | +config.read('config.ini', encoding='utf-8-sig') |
| 10 | + |
| 11 | +api_key = config['settings']['api_key'] |
| 12 | +address_prefix = config['settings']['address_prefix'] |
| 13 | +input_file = config['settings']['input_file'] |
| 14 | +output_file = config['settings']['output_file'] |
| 15 | + |
| 16 | + |
| 17 | +def get_location_and_address(address): |
| 18 | + if address_prefix in address: |
| 19 | + full_address = address |
| 20 | + else: |
| 21 | + full_address = f"{address_prefix}{address}" |
| 22 | + url = f"https://restapi.amap.com/v3/geocode/geo?address={full_address}&output=json&key={api_key}" |
| 23 | + response = requests.get(url) |
| 24 | + data = response.json() |
| 25 | + location, detailed_address = None, None |
| 26 | + if data['status'] == '1' and data['geocodes']: |
| 27 | + if len(data['geocodes']) > 1: |
| 28 | + print(f"地址: {full_address}\n找到多个匹配地址:") |
| 29 | + for idx, geocode in enumerate(data['geocodes']): |
| 30 | + print(f"{idx}. {geocode['formatted_address']}") |
| 31 | + choice = int(input("请输入所需地址的索引: ")) |
| 32 | + geocode = data['geocodes'][choice] |
| 33 | + else: |
| 34 | + geocode = data['geocodes'][0] |
| 35 | + location = geocode['location'] |
| 36 | + detailed_address = geocode['formatted_address'] |
| 37 | + return location, detailed_address |
| 38 | + |
| 39 | + |
| 40 | +def get_distance_duration(origin, destination): |
| 41 | + url = f"https://restapi.amap.com/v3/distance?origins={origin}&destination={destination}&output=json&key={api_key}" |
| 42 | + response = requests.get(url) |
| 43 | + data = response.json() |
| 44 | + distance, duration = None, None |
| 45 | + if data['status'] == '1' and data.get('results'): |
| 46 | + distance = float(data['results'][0]['distance']) / 1000 # 转换为公里 |
| 47 | + duration = float(data['results'][0]['duration']) / 3600 # 转换为小时 |
| 48 | + return distance, duration |
| 49 | + |
| 50 | + |
| 51 | +def process_excel(input_file, output_file): |
| 52 | + df = pd.read_excel(input_file) |
| 53 | + |
| 54 | + # 处理起点 |
| 55 | + df[['起点经纬度', '起点详细地址']] = df['始发地'].apply(lambda x: pd.Series(get_location_and_address(x))) |
| 56 | + |
| 57 | + # 处理目的地 |
| 58 | + df[['目的地经纬度', '目的地详细地址']] = df['目的地'].apply(lambda x: pd.Series(get_location_and_address(x))) |
| 59 | + |
| 60 | + # 为了避免高德API的流量限制,添加延时 |
| 61 | + time.sleep(1) |
| 62 | + |
| 63 | + # 获取距离和时间 |
| 64 | + result = df.apply(lambda row: get_distance_duration(row['起点经纬度'], row['目的地经纬度']), axis=1) |
| 65 | + df['距离(公里)'] = result.apply(lambda x: x[0] if x else None) |
| 66 | + df['行驶时长(小时)'] = result.apply(lambda x: x[1] if x else None) |
| 67 | + |
| 68 | + df.to_excel(output_file, index=False, engine='openpyxl') |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + process_excel(input_file, output_file) |
0 commit comments