Python3实现批量坐标转换(GPS转百度坐标)

Python3调用百度地图开发者平台坐标转换API服务,实现不同平台获取的坐标之间的转换,如GPS转百度,谷歌地图坐标转百度地图坐标等等。

百度地图开发者平台坐标转换API服务官网:http://lbsyun.baidu.com/index.php?title=webapi/guide/changeposition

以下是代码:

# -*- coding: utf-8 -*-
"""
Created on 2019.02.26
@author: zjp
Python3.6.6
"""

import csv
import json
import time
import requests
from bs4 import BeautifulSoup

filePath1 = 'F://data_temp/test20190226_02.csv'
filePath2 = 'F://data_temp/result20190226_02.txt'

urlChangeCode = r'http://api.map.baidu.com/geoconv/v1/?'
aks = ['euCQFAFCeGPfUwSwcBbRFTredMQtuc90']
n = 0
akNbr = 0
columnNames1 = 'index|count|originCode|toCode'
with open(filePath2, 'a', encoding='utf-8') as f:
    f.write('{0}\n'.format(columnNames1))
    f.close()
fileData1 = csv.reader(open(filePath1, 'r', encoding='utf-8'))
while True:
    try:
        for i in fileData1:
            if akNbr < len(aks):
                fromType1 = 1  # http://lbsyun.baidu.com/index.php?title=webapi/guide/changeposition
                toType1 = 5
                tar_url1 = '{0}coords={1},{2}&from={3}&to={4}&output=json&ak={5}'\
                    .format(urlChangeCode, i[2], i[3], fromType1, toType1, aks[akNbr])
                print(tar_url1)
                response1 = requests.get(url=tar_url1)
                soup1 = BeautifulSoup(response1.content, 'html.parser')
                response1.close()
                dictInfo1 = json.loads(str(soup1))  # json转dict
                status1 = dictInfo1['status']
                print(status1)
                if status1 == 0:
                    toCoordX = dictInfo1['result'][0]['x']
                    toCoordY = dictInfo1['result'][0]['y']
                    with open(filePath2, 'a', encoding='utf-8') as f:
                        f.write('{0}|{1}|{2},{3}|{4},{5}\n'.format(i[0], i[1], i[2], i[3], toCoordX, toCoordY))
                        f.close()
                elif status1 == 302 or status1 == 210:
                    with open(filePath2, 'a', encoding='utf-8') as f:
                        f.write('{0}|{1}|{2},{3}|getMoreAk\n'.format(i[0], i[1], i[2], i[3]))
                        f.close()
                    print('please get more ak !')
                    break
                else:
                    with open(filePath2, 'a', encoding='utf-8') as f:
                        f.write('{0}|{1}|{2},{3}|wrong\n'.format(i[0], i[1], i[2], i[3]))
                        f.close()
                    print('wrong')
    except:
        print('unknow wrong')
        time.sleep(5)
        with open(filePath2, 'a', encoding='utf-8') as f:
            f.write('unknow wrong\n')
            f.close()
        continue
    break
print('program finnish.')

转换后的部分示例

index|count|originCode|toCode
30305|30309|104.09256,30.60888|104.10169745629116,30.61216518682409
20444|20447|104.04816,30.69431|104.0570366931986,30.69813681300454
20303|20304|104.03393,30.63906|104.04278767910742,30.642881057560345
40269|40269|104.05763,30.57868|104.06655853369774,30.582366736053185
20005|10107|104.09503,30.67435|104.10415652653431,30.67765006626657
31092|40482|103.98207,30.64407|103.99092102950257,30.647130769257917
30601|30598|104.009414,30.674921|104.01824074549938,30.6784018859763
41235|41234|103.9584,30.63815|103.96713528824876,30.641286392043014
30341|30022|104.070318,30.622481|104.07936715257307,30.626013486191514
20139|31292|104.111955,30.65223|104.12107622206119,30.655605943102618
20570|20049|104.07085,30.69484|104.07987008937036,30.69838582416529
31159|30014|104.101501,30.63545|104.1106558239689,30.638746579814796
30561|30495|103.9954,30.65238|104.00425923754186,30.655599008437942
30541|30497|103.99825,30.65325|104.00710739575065,30.656517002448368
20462|30096|104.088053,30.696286|104.09715887725255,30.699626616989356
40642|40748|103.96589,30.7396|103.97467511624272,30.742711152584544
10231|10051|104.06505,30.65987|104.07405214576144,30.663493036706896
30391|31001|104.05126,30.61555|104.06018139443778,30.619328129116898
50680|50607|103.92281,30.74601|103.93133630414401,30.7496016791913
30841|40094|104.055858,30.731486|104.06480885071763,30.735268031871396
30319|30374|104.07294,30.61115|104.08199687963727,30.61463700134192
31679|30909|104.00213,30.62709|104.01099270063196,30.63041902375282
20577|30711|104.02629,30.687161|104.03510226333222,30.6909247932303
50767|50841|103.839496,30.692156|103.8481785150337,30.695289410504344
30746|20501|104.032191,30.687723|104.04101022501767,30.69154387168493

 

 

 

 

 

 

 

 

 

 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值