2014-02-28 42 views
2

Hej人,我想從Python中的SRTM圖像生成等高線。它似乎計算,但如果我想添加我的輪廓線沒有出現,屬性表也是空的。請看看我的代碼:GDAL Python創建等高線

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()

+1

解決了這個問題 - 我忘了跟.Destroy關閉數據源! –

回答

2

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

要保存並關閉數據集,請取消引用該變量,並可選擇刪除它。

我通常使用這種在最後保存/關閉無論是GDAL或OGR數據集:

ogr_ds = None 
del ogr_ds