打开shp

  1. from osgeo import ogr
  2. inshp_path = 'D:\mycode\GISandPython\0data\park_point_shp\xiamen_20181128_park.shp'
  3. driver = ogr.GetDriverByName('ESRI Shapefile') #查找一个特定的驱动程序
  4. datasource = driver.Open(inshp_path, 0) #0只读,1可写
  5. dir( datasource) #使用Python的内省函数dir()查看所有方法

遍历所有属性值

  1. #【遍历所有属性值】
  2. for i in range(feature.GetFieldCount() ):
  3. print( feature.GetField(i) )
  4. ### 【查看表的结构,各个字段的名称等信息】在layer附加信息中看
  5. layerdef = layer.GetLayerDefn()
  6. for i in range(layerdef.GetFieldCount() ):
  7. defn = layerdef.GetFieldDefn(i)
  8. print(defn.GetName(), defn.GetWidth(), defn.GetType(), defn.GetPrecision() )