我在Ubuntu 17.10上运行Python 2.7,并通过apt安装了osgeo v2.2.1。
我的代码加载osgeo和1)尝试创建一个包含1层和1个字段的shapefile,2)创建一个多边形(4点几何),然后3)将多边形添加到shapefile的图层。 当我使用layer.CreateFeature(feature)时,一切都运行没有问题,直到第三个函数:
driver = ogr.GetDriverByName('ESRI Shapefile')
ds = driver.Open(shapefile_name,-1)
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
layer = ds.GetLayer()
print "layer",layer,", number of features :",layer.GetFeatureCount()
打印:
layer <osgeo.ogr.Layer; proxy of <Swig Object of type 'OGRLayerShadow *' at 0x7f79aa499c90> > , number of features : 0
然后我从已经创建的多边形创建一个几何体并预先传递给该函数:
defn = layer.GetLayerDefn()
geom = ogr.CreateGeometryFromWkb(poly)
print "geom",geom
打印:
geom POLYGON ((-106 24 0,-100 26 0,-103 20 0,-106 20 0))
然后代码继续创建功能:
feat = ogr.Feature(defn)
feat.SetField('polygon_id', polygon_name)
feat.SetGeometry(geom)
print feat
print "Created feature",feat.GetField('polygon_id')
此功能已创建:
<osgeo.ogr.Feature; proxy of <Swig Object of type 'OGRFeatureShadow *' at 0x7f35cd988d50> >
Created feature polygon_1
但是当我将它添加到图层时,没有任何反应:
layer.CreateFeature(feat)
print "number of features : ",layer.GetFeatureCount()
打印:
number of features : 0
我错过了什么?