Skip to content

Commit 29c4927

Browse files
author
vvnocode
committed
提交
1 parent b1347e7 commit 29c4927

File tree

6 files changed

+114
-2
lines changed

6 files changed

+114
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,4 @@ cython_debug/
159159
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160160
# and can be added to the global gitignore or merged into this file. For a more nuclear
161161
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162-
#.idea/
162+
.idea/

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
1-
# GeoExcelProcessor
1+
# GeoExcelProcessor
2+
3+
`GeoExcelProcessor` 是一个通过高德地图API获取地理位置和计算距离,并处理Excel文件的工具。
4+
5+
## 功能
6+
7+
- 通过地址获取经纬度和详细地址。
8+
- 计算两个地点之间的距离和行驶时间。
9+
- 处理Excel文件并将结果存入新的Excel文件。
10+
11+
## 配置文件
12+
13+
`config.ini` 文件中配置以下参数:
14+
15+
```ini
16+
[settings]
17+
api_key = YOUR_AMAP_API_KEY
18+
address_prefix = YOUR_ADDRESS_PREFIX
19+
input_file = path/to/your/input.xlsx
20+
output_file = path/to/your/output.xlsx
21+
```
22+
23+
## 运行方法
24+
1. 安装依赖库:
25+
pip install -r requirements.txt
26+
2. 运行脚本:
27+
python run.py
28+
3. 打包为可执行文件:
29+
pyinstaller --onefile .\run.py

config.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[settings]
2+
# 高德 api key
3+
api_key =
4+
# 统一增加地址前缀以增加识别成功率,如 广州
5+
address_prefix =
6+
# 输入输出excel
7+
input_file = input.xlsx
8+
output_file = output.xlsx

input.xlsx

8.87 KB
Binary file not shown.

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pandas~=2.2.2
2+
requests~=2.30.0
3+
openpyxl~=3.1.5
4+
numpy~=2.1.0

run.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)