【问题标题】:Shapefile created with GDAL doesn't hold projection使用 GDAL 创建的 Shapefile 不保存投影
【发布时间】:2016-07-11 08:53:26
【问题描述】:

我正在使用 Python 中的 GDAL 和 shapely 库生成 shapefile。投影有问题,当我在 ArcMap 中导入生成的 shapefile 时,没有与该文件关联的投影。我该如何纠正,或者如何投影生成的图层?

这是我的代码:

output_shp = "circle.shp"
data_projection = 26919

[...]

srs = osr.SpatialReference()
srs.ImportFromEPSG(data_projection)

# output of srs: <osgeo.osr.SpatialReference; proxy of <Swig Object of type 'OSRSpatialReferenceShadow *' at 0x00000000099FF5A0> >

driver = ogr.GetDriverByName('Esri Shapefile')
ds = driver.CreateDataSource(output_shp)
layer = ds.CreateLayer(layer_name, srs, ogr.wkbPolygon)
layer.CreateField(ogr.FieldDefn('id',ogr.OFTInteger))
defn = layer.GetLayerDefn()  

for x in range(0,length):
    feat = ogr.Feature(defn)
    feat.SetField('id', x+1)

    geome = ogr.CreateGeometryFromWkt(geom_out[x].wkt)
    feat.SetGeometry(geome)   
    layer.CreateFeature(feat)

【问题讨论】:

    标签: python projection gdal shapefile ogr


    【解决方案1】:

    好的。我找到了解决方案,thanks to MikeT answer in this post.

    问题是我的GDAL_DATA 环境变量设置不正确。因此,我添加到图层的 SRS 是空的,因为它在目录中找不到它。

    检查导入是否失败(感谢 MikeT):

    from osgeo import osr
    testSR = osr.SpatialReference()
    res = testSR.ImportFromEPSG(4326)
    if res != 0:
        raise RuntimeError(repr(res) + ': could not import from EPSG')
    print testSR.ExportToPrettyWkt()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 2017-02-20
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 2016-01-15
      相关资源
      最近更新 更多