gpt4 book ai didi

python - GDAL Python 创建轮廓线

转载 作者:行者123 更新时间:2023-11-28 16:38:55 40 4
gpt4 key购买 nike

我想在 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() 方法,as described in the GDAL/OGR Python Gotchas .

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

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

ogr_ds = None
del ogr_ds

关于python - GDAL Python 创建轮廓线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22100453/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com