根據epsg代號進行座標的批量投影轉換

接口來源:http://epsg.io/transform#s_srs=4490&t_srs=4326

轉換參數使用的是默認值而不是精確的自定義數值,有更高精度要求就別用了,或者拿去改改也行。

將要轉換的座標文本放在txt文件夾裏,然後直接運行convert.py腳本。
格式參考示例數據demoData.txt,其中s_srs爲待轉換座標的epsg代號,t_srs爲輸出的座標的epsg代號

# -*- encoding: utf-8 -*-
import urllib,urllib2
import requests
import json
import os
def convert(x,y,s_srs,t_srs):
    url='http://epsg.io/trans'
    textmod ={'x':x,'y':y,'s_srs':s_srs,'t_srs':t_srs}
    textmod = urllib.urlencode(textmod)
    print(textmod)
    #輸出內容:password=admin&user=admin
    req = urllib2.Request(url = '%s%s%s' % (url,'?',textmod))
    print(req.get_full_url())
    res = urllib2.urlopen(req)
    res = res.read()
    print(res)
def request(data,s_srs,t_srs):
    url='http://epsg.io/trans'
    textmod ={'data':data,'s_srs':s_srs,'t_srs':t_srs}
    response = requests.get(url, params=textmod)
    jsonData = response.json()
    coor=json.dumps(jsonData)
    return coor
    # print(json)
# 遍歷指定目錄,顯示目錄下的所有文件名
def eachFile(filepath):
    pathDir =  os.listdir(filepath)
    for allDir in pathDir:
        child = os.path.join('%s%s' % (filepath, allDir))
        childPath=child.decode('gbk') # .decode('gbk')是解決中文顯示亂碼問題
        with open(childPath, 'r') as file:
            json_data = file.read()
            json_data = json.loads(json_data)
            s_srs=json_data['s_srs']
            t_srs=json_data['t_srs']
            cdata=json_data['data']
            coor=request(cdata,s_srs,t_srs)
            outPutPath=os.path.splitext(childPath)[0]+'Convert.txt'
            with open(outPutPath, 'w') as wfile:
                geojson=json.dumps(coor)
                geojson.replace('u\'','\'')
                geojson=geojson.decode("unicode-escape") 
                wfile.write(geojson)
                print('success')
def readFile():
    path=os.path.dirname(os.path.realpath(__file__))#獲取py文件所在文件夾
    txtDirPath=path+'\\txt\\'
    eachFile(txtDirPath)
def reptile():
    url='http://epsg.io/trans?y=2544675&x=36518161&t_srs=4326&s_srs=2360'
    page = urllib.urlopen(url)
    return page.read()
if __name__=='__main__':
    # request(36518161,2544675,2360,4326)
    readFile()

完整文件:https://download.csdn.net/download/jin80506/10784573

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章