使用python对shapefile重投影
2015-06-26 11:36
701 查看
脚本如下:
查看原文:http://www.giser.net/?p=1335
#!/usr/bin/env python from osgeo import ogr, osr from osgeo import gdal import os def reproject(inputfile,outputfile,layername): gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","NO") gdal.SetConfigOption("SHAPE_ENCODING","") driver = ogr.GetDriverByName('ESRI Shapefile') # input SpatialReference inSpatialRef = osr.SpatialReference() inSpatialRef.ImportFromEPSG(4326) # output SpatialReference outSpatialRef = osr.SpatialReference() outSpatialRef.ImportFromEPSG(3857) # create the CoordinateTransformation coordTrans = osr.CoordinateTransformation(inSpatialRef, outSpatialRef) # get the input layer inDataSet = driver.Open(inputfile) inLayer = inDataSet.GetLayer() # create the output layer outputShapefile = outputfile outDataSet = driver.CreateDataSource(outputShapefile) print inLayer.GetGeomType() outLayer = outDataSet.CreateLayer(layername,geom_type=inLayer.GetGeomType() ) # add fields inLayerDefn = inLayer.GetLayerDefn() for i in range(0, inLayerDefn.GetFieldCount()): fieldDefn = inLayerDefn.GetFieldDefn(i) outLayer.CreateField(fieldDefn) # get the output layer's feature definition outLayerDefn = outLayer.GetLayerDefn() # loop through the input features inFeature = inLayer.GetNextFeature() while inFeature: # get the input geometry geom = inFeature.GetGeometryRef() # reproject the geometry geom.Transform(coordTrans) # create a new feature outFeature = ogr.Feature(outLayerDefn) # set the geometry and attribute outFeature.SetGeometry(geom) for i in range(0, outLayerDefn.GetFieldCount()): outFeature.SetField(outLayerDefn.GetFieldDefn(i).GetNameRef(), inFeature.GetField(i)) # add the feature to the shapefile outLayer.CreateFeature(outFeature) # destroy the features and get the next input feature outFeature.Destroy() inFeature.Destroy() inFeature = inLayer.GetNextFeature() # close the shapefiles inDataSet.Destroy() outDataSet.Destroy()
查看原文:http://www.giser.net/?p=1335
相关文章推荐
- 【python】 web开发入门
- python 多线程脚本
- python解压压缩包的几种方法
- kmeans python实现
- Python使用struct处理二进制
- 【Python】 最简单的web服务
- python基础知识
- Python多线程4:_thread
- policy机制中经典的python用法【转载】
- Ubuntu下Python建立和连接数据库
- 【python】Python 之 __new__() 方法与实例化
- python对象与属性相关的特殊方法
- python下如何安装python-MySQLdb
- 【Python】Python 新式类介绍
- python3.4学习笔记(七) 学习网站博客推荐
- 使用wxPython建立一个计算文件md5的GUI工具
- python3.4学习笔记(六) 常用快捷键使用技巧,持续更新
- python3.4学习笔记(五) IDLE显示行号问题,插件安装和其他开发工具介绍
- python3.4学习笔记(四) 3.x和2.x的区别,持续更新
- 栅栏加解密python实现(支持密钥加密)