gdal python 进行shp文件投影转换
时间: 2023-08-02 16:09:58 浏览: 187
你可以使用GDAL库中的Python绑定来进行shp文件的投影转换。首先,确保你已经安装了GDAL库。然后,按照以下步骤进行操作:
1. 导入必要的库:
```python
from osgeo import ogr, osr
```
2. 打开原始的shp文件:
```python
source = ogr.Open('path/to/source.shp')
layer = source.GetLayer()
```
3. 创建一个新的shp文件作为输出:
```python
driver = ogr.GetDriverByName('ESRI Shapefile')
output = driver.CreateDataSource('path/to/output.shp')
outLayer = output.CreateLayer('output', geom_type=ogr.wkbPolygon)
```
4. 定义原始坐标系和目标坐标系:
```python
sourceSR = layer.GetSpatialRef()
targetSR = osr.SpatialReference()
targetSR.ImportFromEPSG(targetEPSG) # 替换targetEPSG为你想要的目标EPSG代码
```
5. 创建一个坐标转换器:
```python
transform = osr.CoordinateTransformation(sourceSR, targetSR)
```
6. 遍历原始图层中的要素,并进行投影转换:
```python
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
```
这样,你就可以将原始shp文件中的几何体投影到目标坐标系,并保存为新的shp文件。
注意:在代码中,将`path/to/source.shp`和`path/to/output.shp`替换为你实际的文件路径,将`targetEPSG`替换为你想要的目标EPSG代码。
阅读全文
相关推荐


















