[Python] 纯文本查看 复制代码
import pandas as pd
import numpy as np
from geopy.distance import geodesic
from tqdm import tqdm # Import tqdm
import swifter # Import swifter
def calculate_azimuth(lat1, lon1, lat2, lon2):
"""计算从点1到点2的方位角"""
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
x = np.sin(dlon) * np.cos(lat2)
y = np.cos(lat1) * np.sin(lat2) - np.sin(lat1) * np.cos(lat2) * np.cos(dlon)
initial_bearing = np.arctan2(x, y)
initial_bearing = np.degrees(initial_bearing)
compass_bearing = (initial_bearing + 360) % 360
return compass_bearing
def calculate_distance(row1, row2):
"""计算两个点之间的地理位置距离(千米)"""
return geodesic((row1['Latitude'], row1['Longitude']), (row2['Latitude'], row2['Longitude'])).kilometers
def is_neighbor(row1, row2, distance, azi1, azi2, alpha_s, alpha_n):
"""根据距离和夹角判断是否为邻区"""
if distance <= 2:
return True
elif 2 < distance <= 3.5 and alpha_s <= 120 and alpha_n <= 120:
return True
elif 3.5 < distance <= 4.5 and alpha_s <= 90 and alpha_n <= 90:
return True
return False
def calculate_interference(planned_df, full_df):
"""计算每个小区到其他小区的邻区关系"""
results = []
for _, row1 in tqdm(planned_df.iterrows(), total=len(planned_df)):
lat1, lon1, azimuth1 = row1['Latitude'], row1['Longitude'], row1['Azimuth']
for _, row2 in full_df.iterrows():
if row1['CellName'] != row2['CellName']:
lat2, lon2 = row2['Latitude'], row2['Longitude']
azi1 = calculate_azimuth(lat1, lon1, lat2, lon2)
azi2 = calculate_azimuth(lat2, lon2, lat1, lon1)
alpha_s = min(abs(azi1 - azimuth1), 360 - abs(azi1 - azimuth1))
alpha_n = min(abs(azi2 - row2['Azimuth']), 360 - abs(azi2 - row2['Azimuth']))
distance = calculate_distance(row1, row2)
if is_neighbor(row1, row2, distance, azi1, azi2, alpha_s, alpha_n):
results.append({
'需规划小区名': row1['CellName'],
'需规划 eNodeB-ID': row1['eNodeB-ID'],
'需规划 小区本地ID': row1['本地ID'],
'全量小区名': row2['CellName'],
'全量 eNodeB-ID': row2['eNodeB-ID'],
'全量 CellID': row2['CellID'],
'全量 PCI': row2['PCI'],
'全量 频点号': row2['Earfcn'],
'全量 TAC': row2['TAC'],
'距离': round(distance, 2),
'方位角1': round(azi1, 2),
'方位角2': round(azi2, 2),
'夹角1': round(alpha_s, 2),
'夹角2': round(alpha_n, 2)
})
return pd.DataFrame(results)
# 读取Excel数据
planned_path = 'C:/Users/Desktop/邻区规划/需规划小区工参.xlsx'
full_path = 'C:/Users/Desktop/邻区规划/全量工参.xlsx'
planned_df = pd.read_excel(planned_path)
full_df = pd.read_excel(full_path)
# 计算干扰
interference_results = calculate_interference(planned_df, full_df)
# print(interference_results)
# 保存结果到新的Excel文件
interference_results.to_excel('C:/Users/Desktop/邻区规划/邻区规划结果.xlsx', index=False)