代碼分析Python地圖坐標(biāo)轉(zhuǎn)換
更新時間:2018年02月08日 10:24:39 投稿:laozhang
這篇文章主要介紹了Python地圖坐標(biāo)轉(zhuǎn)換的相關(guān)知識點以及分享了相關(guān)的代碼實例,對此有興趣的朋友學(xué)習(xí)下。
最近做項目正好需要坐標(biāo)的轉(zhuǎn)換
- 各地圖API坐標(biāo)系統(tǒng)比較與轉(zhuǎn)換;
- WGS84坐標(biāo)系:即地球坐標(biāo)系,國際上通用的坐標(biāo)系。設(shè)備一般包含GPS芯片或者北斗芯片獲取的經(jīng)緯度為WGS84地理坐標(biāo)系,
- 谷歌地圖采用的是WGS84地理坐標(biāo)系(中國范圍除外);
- GCJ02坐標(biāo)系:即火星坐標(biāo)系,是由中國國家測繪局制訂的地理信息系統(tǒng)的坐標(biāo)系統(tǒng)。由WGS84坐標(biāo)系經(jīng)加密后的坐標(biāo)系。
- 谷歌中國地圖和搜搜中國地圖采用的是GCJ02地理坐標(biāo)系; BD09坐標(biāo)系:即百度坐標(biāo)系,GCJ02坐標(biāo)系經(jīng)加密后的坐標(biāo)系;
- 搜狗坐標(biāo)系、圖吧坐標(biāo)系等,估計也是在GCJ02基礎(chǔ)上加密而成的.
然后在csv中將其轉(zhuǎn)化。
最后再在百度地圖API上進行檢驗成功
#http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=10923
#代碼原地址
import csv
import string
import time
import math
#系數(shù)常量
a = 6378245.0
ee = 0.00669342162296594323
x_pi = 3.14159265358979324 * 3000.0 / 180.0;
#轉(zhuǎn)換經(jīng)度
def transformLat(lat,lon):
ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon +0.2 * math.sqrt(abs(lat))
ret += (20.0 * math.sin(6.0 * lat * math.pi) + 20.0 * math.sin(2.0 * lat * math.pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lon * math.pi) + 40.0 * math.sin(lon / 3.0 * math.pi)) * 2.0 / 3.0
ret += (160.0 * math.sin(lon / 12.0 * math.pi) + 320 * math.sin(lon * math.pi / 30.0)) * 2.0 / 3.0
return ret
#轉(zhuǎn)換緯度
def transformLon(lat,lon):
ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * math.sqrt(abs(lat))
ret += (20.0 * math.sin(6.0 * lat * math.pi) + 20.0 * math.sin(2.0 * lat * math.pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lat * math.pi) + 40.0 * math.sin(lat / 3.0 * math.pi)) * 2.0 / 3.0
ret += (150.0 * math.sin(lat / 12.0 * math.pi) + 300.0 * math.sin(lat / 30.0 * math.pi)) * 2.0 / 3.0
return ret
#Wgs transform to gcj
def wgs2gcj(lat,lon):
dLat = transformLat(lon - 105.0, lat - 35.0)
dLon = transformLon(lon - 105.0, lat - 35.0)
radLat = lat / 180.0 * math.pi
magic = math.sin(radLat)
magic = 1 - ee * magic * magic
sqrtMagic = math.sqrt(magic)
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * math.pi)
dLon = (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * math.pi)
mgLat = lat + dLat
mgLon = lon + dLon
loc=[mgLat,mgLon]
return loc
#gcj transform to bd2
def gcj2bd(lat,lon):
x=lon
y=lat
z = math.sqrt(x * x + y * y) + 0.00002 * math.sin(y * x_pi)
theta = math.atan2(y, x) + 0.000003 * math.cos(x * x_pi)
bd_lon = z * math.cos(theta) + 0.0065
bd_lat = z * math.sin(theta) + 0.006
bdpoint = [bd_lon,bd_lat]
return bdpoint
#wgs transform to bd
def wgs2bd(lat,lon):
wgs_to_gcj = wgs2gcj(lat,lon)
gcj_to_bd = gcj2bd(wgs_to_gcj[0], wgs_to_gcj[1])
return gcj_to_bd;
for i in range (3,4):
n = str('2017.040'+ str(i)+'.csv')
m = str('2017040' + str(i)+'.csv')
csvfile = open(m,'w',encoding='UTF-8',newline='')
nodes = csv.writer(csvfile)
nodes.writerow(['md5','content','phone','conntime','recitime','lng','lat'])
l=[]
with open(n,newline='',encoding='UTF-8') as f:
reader = csv.DictReader(f)
for row in reader:
if row['md5'] == 'md5':
continue
else:
y=float(row['lng'])
x=float(row['lat'])
loc=wgs2bd(x,y)
l.append([row['md5'],row['content'],row['phone'],row['conntime'],row['recitime'],loc[0],loc[1]])
nodes.writerows(l)
csvfile.close()
print("轉(zhuǎn)換成功")
您可能感興趣的文章:
- Python實現(xiàn)WGS84火星百度及web墨卡托四種坐標(biāo)系相互轉(zhuǎn)換
- Python實現(xiàn)常見坐標(biāo)系的相互轉(zhuǎn)換
- Python實現(xiàn)常見的4種坐標(biāo)互相轉(zhuǎn)換
- 使用Python和GDAL給圖片加坐標(biāo)系的實現(xiàn)思路(坐標(biāo)投影轉(zhuǎn)換)
- Python經(jīng)緯度坐標(biāo)轉(zhuǎn)換為距離及角度的實現(xiàn)
- 解決python gdal投影坐標(biāo)系轉(zhuǎn)換的問題
- python實現(xiàn)無人機航拍圖片像素坐標(biāo)轉(zhuǎn)世界坐標(biāo)的示例代碼
相關(guān)文章
如何用Python中Tushare包輕松完成股票篩選(詳細流程操作)
這篇文章主要介紹了如何用Python中Tushare包輕松完成股票篩選(詳細流程操作),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
使用pickle存儲數(shù)據(jù)dump 和 load實例講解
今天小編就為大家分享一篇使用pickle存儲數(shù)據(jù)dump 和 load實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python編程新標(biāo)準(zhǔn)學(xué)會十項好習(xí)慣提升編碼質(zhì)量
這篇文章主要為大家介紹了Python編程新標(biāo)準(zhǔn)學(xué)會十項好習(xí)慣提升編碼質(zhì)量,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01

