'''
未使用GeoPy庫(kù)殖演,排版混亂請(qǐng)自行縮進(jìn)。
先讀取模版,格式要是.xlsx,文字編碼請(qǐng)先轉(zhuǎn)成Unicode年鸳,模版要一樣
高德個(gè)人用戶KEY一天6000次趴久,設(shè)置了1到2秒隨機(jī)延遲防止被封
如果該點(diǎn)位高德上找不到就默認(rèn)坐標(biāo)為'0.00,0.00'需要手工查找
'''
#! conding = 'UTF-8'
import requests
from xlrd import open_workbook
import re
import math
import time
import random
import xlwings as xw
import tkinter as tk
from tkinter import filedialog,ttk,HORIZONTAL
# 打開(kāi)excel
def read_excel(File_path,sheet_index):
? ? # 打開(kāi)文件
? ? workbook = open_workbook(File_path)
? ? # 獲取所有sheet
? ? sheet2_name = workbook.sheet_names()[sheet_index] # 獲取序列號(hào)相關(guān)的表
? ? # 根據(jù)sheet索引或者名稱獲取sheet內(nèi)容
? ? sheet2 = workbook.sheet_by_name(sheet2_name)
? ? # 獲取整行和整列的值(數(shù)組)
? ? cols = sheet2.col_values(2) # 獲取第三列內(nèi)容
? ? addrs_list=[] # 新建一個(gè)空表格來(lái)存清洗好的地址
? ? for i in cols:
? ? ? ? addr = re.findall(r'.-(.*)-.', i)
? ? ? ? if addr != [] or None:
? ? ? ? ? ? addrs_list.append(addr)
? ? return addrs_list
# 高德正向解析 注意個(gè)人KEY可以解析6000次每天
def PSGIapi(site):
? ? headers = {
? ? ? ? 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
? ? ? ? 'Cookie': 填你自己的Cookie,
? ? ? ? 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
? ? ? ? 'Accept-Encoding': 'gzip, deflate',
? ? ? ? 'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
? ? ? ? 'Cache-Control': 'max-age=0',
? ? ? ? 'Connection': 'keep-alive'
? ? }
? ? parameters = {'address': site, 'city': '你的城市', 'key': '這里填自己申請(qǐng)的key'}
? ? url = 'http://restapi.amap.com/v3/geocode/geo'
? ? response = requests.get(url, parameters,headers=headers)
? ? info_site = response.json()
? ? if info_site['count'] == '1':
? ? ? ? lat_lng = info_site['geocodes'][0]['location']
? ? else:
? ? ? ? # 如果該點(diǎn)位高德上找不到就默認(rèn)坐標(biāo)為0,0需要手工比對(duì)
? ? ? ? lat_lng = [0.00,0.00]
? ? return lat_lng
# 將高德坐標(biāo)(GCJ02)轉(zhuǎn)換為GPS(WGS84)坐標(biāo)
def GCJ2WGS(location,date_lists,site):
? ? # location格式如下:locations[1] = "113.923745,22.530824"
? ? lon = float(location[0:location.find(",")])
? ? lat = float(location[location.find(",") + 1:len(location)])
? ? a = 6378245.0 # 克拉索夫斯基橢球參數(shù)長(zhǎng)半軸a
? ? ee = 0.00669342162296594323 #克拉索夫斯基橢球參數(shù)第一偏心率平方
? ? PI = 3.14159265358979324 # 圓周率
? ? # 以下為轉(zhuǎn)換公式
? ? x = lon - 105.0
? ? y = lat - 35.0
? ? # 經(jīng)度
? ? dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(abs(x))
? ? dLon += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0
? ? dLon += (20.0 * math.sin(x * PI) + 40.0 * math.sin(x / 3.0 * PI)) * 2.0 / 3.0
? ? dLon += (150.0 * math.sin(x / 12.0 * PI) + 300.0 * math.sin(x / 30.0 * PI)) * 2.0 / 3.0
? ? #緯度
? ? dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(abs(x))
? ? dLat += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0
? ? dLat += (20.0 * math.sin(y * PI) + 40.0 * math.sin(y / 3.0 * PI)) * 2.0 / 3.0
? ? dLat += (160.0 * math.sin(y / 12.0 * PI) + 320 * math.sin(y * PI / 30.0)) * 2.0 / 3.0
? ? radLat = lat / 180.0 * PI
? ? magic = math.sin(radLat)
? ? magic = 1 - ee * magic * magic
? ? sqrtMagic = math.sqrt(magic)
? ? dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI)
? ? dLon = (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * PI)
? ? wgsLon = lon - dLon
? ? wgsLat = lat - dLat
? ? date=[wgsLon,wgsLat] # 將名稱坐標(biāo)封裝進(jìn)列表等待后面寫(xiě)入
? ? date_lists.append(date)
? ? return date_lists
# askopenfilename 1次上傳1個(gè)搔确;askopenfilenames1次上傳多個(gè)
def upload_file():
? ? global File_path
? ? File_path = tk.filedialog.askopenfilename()
? ? p1["maximum"] = len_rows(File_path)
? ? main(File_path)
# 獲取行數(shù)
def len_rows(File_path):
? ? app = xw.App(visible=False,add_book=False)
? ? global wb
? ? wb = app.books.open(File_path)
? ? Total_rows=0 # 獲取所有表的總函數(shù)用來(lái)做進(jìn)度表
? ? sht_len=len(wb.sheets)
? ? for sheet_index in range(sht_len):
? ? ? ? sheet=wb.sheets[sheet_index]
? ? ? ? rng=sheet.range('A4').expand('table')
? ? ? ? nrows=rng.rows.count
? ? ? ? # ncols=rng.columns.count #列
? ? ? ? Total_rows += nrows
? ? return Total_rows
def main(File_path):
? ? # 需要上傳的excel文件,需要查詢的sheet表,需要另存為的save_excel名稱
? ? p1["value"] = 0
? ? for sheet_index in range(len(wb.sheets)):
? ? ? ? address=read_excel(File_path,sheet_index)
? ? ? ? date_lists= [] # 最后數(shù)據(jù)表格
? ? ? ? rep_addr = None? # 去重復(fù)地址請(qǐng)求減少服務(wù)器壓力
? ? ? ? for site in address:
? ? ? ? ? ? # 如果地址是一樣的直接復(fù)制列表前面的經(jīng)緯度
? ? ? ? ? ? if site[0] == rep_addr:
? ? ? ? ? ? ? ? old_lat = date_lists[-1]
? ? ? ? ? ? ? ? date_lists.append(old_lat)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? try:
? ? ? ? ? ? ? ? ? ? rep_addr = site[0]
? ? ? ? ? ? ? ? ? ? location = PSGIapi(site[0])
? ? ? ? ? ? ? ? ? ? # 判斷是否是廢點(diǎn)或者是默認(rèn)的溫州市人民政府坐標(biāo)
? ? ? ? ? ? ? ? ? ? if location == [0.00,0.00] or location == '120.699366,27.994267':
? ? ? ? ? ? ? ? ? ? ? ? date_lists.append([0.00,0.00])
? ? ? ? ? ? ? ? ? ? ? ? result=(rep_addr,'點(diǎn)位名稱不正確請(qǐng)手工確認(rèn)')
? ? ? ? ? ? ? ? ? ? ? ? print(result)
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? GCJ2WGS(location,date_lists,site)
? ? ? ? ? ? ? ? ? ? ? ? result=('新發(fā)現(xiàn)地址'+ site[0])
? ? ? ? ? ? ? ? ? ? ? ? print(result)
? ? ? ? ? ? ? ? except Exception as e:
? ? ? ? ? ? ? ? ? ? print('Reason:', e)
? ? ? ? ? ? ? ? ? ? continue
? ? ? ? ? ? ? ? finally:
? ? ? ? ? ? ? ? ? ? time.sleep(random.randint(1,2))
? ? ? ? ? ? # 進(jìn)度條增加1次
? ? ? ? ? ? p1["value"] +=1
? ? ? ? ? ? Windous.update()
? ? ? ? sht = wb.sheets[sheet_index]
? ? ? ? sht.range('D:E').api.Insert()
? ? ? ? sht.range('D3').value = ['經(jīng)度','緯度']
? ? ? ? # 直接寫(xiě)入列表彼棍,注意是按照行寫(xiě)的,列請(qǐng).options(transpose=True)
? ? ? ? sht.range('D4').value = date_lists
? ? ? ? # save_excel(date_lists,excel,sheet_index)
? ? wb.save(File_path)
? ? print('轉(zhuǎn)換完成妥箕,關(guān)閉窗口滥酥。')
? ? Windous.destroy()
if __name__ == '__main__':
? ? Windous = tk.Tk()
? ? Windous.title('點(diǎn)位坐標(biāo)采集(PSGI地圖)')
? ? btn = tk.Button(Windous, text='上傳轉(zhuǎn)換', command=upload_file)
? ? btn.grid(row=1, column=0)
? ? #進(jìn)度條
? ? p1 = ttk.Progressbar(Windous, length=200, mode="determinate", orient=HORIZONTAL)
? ? p1.grid(row=1,column=1)
? ? Windous.mainloop()
其實(shí)如果改成GeoPy庫(kù)應(yīng)該會(huì)更簡(jiǎn)單,大神勿噴畦幢。