天天看点

根据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