根据epsg代号进行坐标的批量投影转换

15 篇文章 0 订阅

接口来源: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

若要批量进行shp文件的投影转换,你可以使用Python的os模块来遍历指定目录中的所有shp文件,并对每个文件执行投影转换操作。以下是一个示例代码: ```python import os from osgeo import ogr, osr # 定义原始坐标系和目标坐标系 sourceEPSG = 4326 # 原始坐标系的EPSG代码 targetEPSG = 3857 # 目标坐标系的EPSG代码 # 创建目标坐标系 targetSR = osr.SpatialReference() targetSR.ImportFromEPSG(targetEPSG) # 遍历目录中的所有shp文件 source_dir = 'path/to/source_directory' # 源文件目录 output_dir = 'path/to/output_directory' # 输出文件目录 for filename in os.listdir(source_dir): if filename.endswith('.shp'): # 打开原始shp文件 source_path = os.path.join(source_dir, filename) source = ogr.Open(source_path) layer = source.GetLayer() # 创建输出的shp文件 output_path = os.path.join(output_dir, filename) driver = ogr.GetDriverByName('ESRI Shapefile') output = driver.CreateDataSource(output_path) outLayer = output.CreateLayer('output', geom_type=ogr.wkbPolygon) # 获取原始坐标系 sourceSR = layer.GetSpatialRef() # 创建坐标转换器 transform = osr.CoordinateTransformation(sourceSR, targetSR) # 遍历原始图层中的要素,并进行投影转换 for feature in layer: geom = feature.GetGeometryRef() geom.Transform(transform) # 创建新要素并将转换后的几何体添加到新图层中 newFeature = ogr.Feature(outLayer.GetLayerDefn()) newFeature.SetGeometry(geom) outLayer.CreateFeature(newFeature) newFeature = None # 关闭文件 source = None output = None ``` 请将代码中的`path/to/source_directory`和`path/to/output_directory`替换为你实际的源文件目录和输出文件目录。代码将遍历源文件目录中的所有shp文件,并将转换后的文件保存到输出文件目录中,文件名和文件结构保持一致。同时,你需要修改`sourceEPSG`和`targetEPSG`变量为你实际的原始和目标坐标系的EPSG代码。 这样,你就可以批量进行shp文件的投影转换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值