背景
目的一:解决不同网络站点之间的距离计算,比如要计算全网GSM共站址的LTE站点;
目的二:批量计算不同场景周边的站点,比如一个城市各居民区的覆盖站点清单。
工具源代码
代码语言:javascript复制import openpyxl,xlrd
import os,sys
import math,re
import datetime
#-----------------------数据输出到EXCEL文件------------------------
def write_xlsx(dis):
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.title = '最小站间距'
for i in range(0, len(dis)):
for j in range(0,len(dis[i])):
sheet.cell(row=i 1, column=j 1, value=dis[i][j])
workbook.save('最小站间距-' datetime.datetime.now().strftime('%Y%m%d') '.xlsx')
#-----------------------程序入口------------------------
if __name__ == '__main__':
if os.path.isfile('GSM经纬度.xlsx'):
GSM_data=xlrd.open_workbook('GSM经纬度.xlsx')
GSM_table=GSM_data.sheets()[0]
GSM_Btsname=GSM_table.col_values(0,start_rowx=1,end_rowx=None)
GSM_lon=GSM_table.col_values(1,start_rowx=1,end_rowx=None)
GSM_lat=GSM_table.col_values(2,start_rowx=1,end_rowx=None)
if os.path.isfile('LTE经纬度.xlsx'):
LTE_data=xlrd.open_workbook('LTE经纬度.xlsx')
LTE_table=LTE_data.sheets()[0]
LTE_Btsname=LTE_table.col_values(0,start_rowx=1,end_rowx=None)
LTE_lon=LTE_table.col_values(1,start_rowx=1,end_rowx=None)
LTE_lat=LTE_table.col_values(2,start_rowx=1,end_rowx=None)
dis = [['网元名称','最小站间距(m)','目标站点']]
print('最小站间距开始计算,请耐心等待:')
GSM_lg = len(GSM_Btsname)
for i in range(0,GSM_lg):
Bts_dic = 100000000
tem = [GSM_Btsname[i]]
name = ''
LTE_lg = len(LTE_Btsname)
for j in range(0,LTE_lg):
lon1=GSM_lon[i]
lat1=GSM_lat[i]
lon1, lat1, lon2, lat2 = map(math.radians, [float(lon1), float(lat1), float(LTE_lon[j]), float(LTE_lat[j])]) # 经纬度转换成弧度
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat / 2) ** 2 math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
c = 2 * math.asin(math.sqrt(a))
r = 6371.137 # 地球平均半径,单位为公里
dic = (c * r * 1000)
if dic < Bts_dic:
Bts_dic = dic
name = LTE_Btsname[j]
tem.append(str('%.2f'%Bts_dic))
tem.append(name)
dis.append(tem)
print(' ',end='')
sys.stdout.flush()
write_xlsx(dis)