【问题标题】:GDAL Python Creating ContourlinesGDAL Python 创建轮廓线
【发布时间】:2014-02-28 16:23:05
【问题描述】:

我想在 Python 中从 SRTM 图像生成轮廓线。它似乎在计算,但如果我想添加我的轮廓线,什么都不会显示,并且属性表也是空的。请看我的代码:

    from osgeo import gdal, gdal_array
    from osgeo.gdalconst import *
    from numpy import *
    from osgeo import ogr

    #Read in SRTM data
    indataset1 = gdal.Open( src_filename_1, GA_ReadOnly)
    in1 = indataset1.GetRasterBand(1)
    
    #Generate layer to save Contourlines in
    ogr_ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource(dst_filename)
    contour_shp = ogr_ds.CreateLayer('contour')
    
    field_defn = ogr.FieldDefn("ID", ogr.OFTInteger)
    contour_shp.CreateField(field_defn)
    field_defn = ogr.FieldDefn("elev", ogr.OFTReal)
    contour_shp.CreateField(field_defn)
    
    #Generate Contourlines
    gdal.ContourGenerate(in1, 100, 0, [], 0, 0, contour_shp, 0, 1)
    ogr_ds.Destroy()

字段 ID 和字段高度似乎为空,但 contour_shape 文件相当大 ~ 100MB。

知道可能出了什么问题吗?

更新:我明白了!我忘了关闭数据源:ogr_ds.Destroy()

【问题讨论】:

  • 解决了 - 我忘了用 .Destroy 关闭数据源!
  • 你这里用的是什么版本的gdal?

标签: python gis gdal


【解决方案1】:

不要使用Destroy()方法as described in the GDAL/OGR Python Gotchas

要保存和关闭数据集,取消对变量的引用,并可选择删除它。

我通常在最后使用它来保存/关闭 GDAL 或 OGR 数据集:

ogr_ds = None
del ogr_ds

【讨论】:

    猜你喜欢
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多