数据集

创建点状数据集

  1. from osgeo import ogr
  2. import os,math
  3. driver = ogr.GetDriverByName("ESRI Shapefile")
  4. extfile = 'point_demo.shp'
  5. point_coors = [[300,450], [750, 700], [1200, 450], [750, 200], [750, 450]]
  6. print point_coors
  7. driver = ogr.GetDriverByName("ESRI Shapefile")
  8. if os.access( extfile, os.F_OK ):
  9. driver.DeleteDataSource( extfile )
  10. newds = driver.CreateDataSource(extfile)
  11. layernew = newds.CreateLayer('point',None,ogr.wkbPoint)
  12. for aa in point_coors:
  13. wkt = 'POINT (' + str(aa[0]) + ' ' + str(aa[1]) + ')'
  14. geom = ogr.CreateGeometryFromWkt(wkt)
  15. feat = ogr.Feature(layernew.GetLayerDefn())
  16. feat.SetGeometry(geom)
  17. layernew.CreateFeature(feat)
  18. newds.Destroy()

创建线状数据集

  1. from osgeo import ogr
  2. import os,math
  3. driver = ogr.GetDriverByName("ESRI Shapefile")
  4. extfile = 'line_demo.shp'
  5. point_coors = [300,450, 750, 700, 1200, 450, 750, 200]
  6. print point_coors
  7. driver = ogr.GetDriverByName("ESRI Shapefile")
  8. if os.access( extfile, os.F_OK ):
  9. driver.DeleteDataSource( extfile )
  10. newds = driver.CreateDataSource(extfile)
  11. layernew = newds.CreateLayer('point',None,ogr.wkbLineString)
  12. wkt = 'LINESTRING (%f %f,%f %f,%f %f,%f %f,%f %f)' % (point_coors[0],point_coors[1],
  13. point_coors[2],point_coors[3], point_coors[4],point_coors[5],
  14. point_coors[6],point_coors[7], point_coors[0],point_coors[1])
  15. print(wkt)
  16. geom = ogr.CreateGeometryFromWkt(wkt)
  17. feat = ogr.Feature(layernew.GetLayerDefn())
  18. feat.SetGeometry(geom)
  19. layernew.CreateFeature(feat)
  20. newds.Destroy()

创建多边形数据集

  1. from osgeo import ogr
  2. import os,math
  3. driver = ogr.GetDriverByName("ESRI Shapefile")
  4. extfile = 'rect_demo.shp'
  5. if os.access( extfile, os.F_OK ):
  6. driver.DeleteDataSource( extfile )
  7. extent = [400, 1100, 300, 600]
  8. newds = driver.CreateDataSource(extfile)
  9. layernew = newds.CreateLayer('rect',None,ogr.wkbPolygon)
  10. width = math.fabs(extent[1]-extent[0])
  11. height = math.fabs(extent[3]-extent[2])
  12. tw = width/2
  13. th = width/2
  14. extnew = extent[0]+tw
  15. wkt = 'POLYGON ((%f %f,%f %f,%f %f,%f %f,%f %f))' % (extent[0],extent[3],
  16. extent[1],extent[3], extent[1],extent[2],
  17. extent[0],extent[2], extent[0],extent[3])
  18. geom = ogr.CreateGeometryFromWkt(wkt)
  19. feat = ogr.Feature(layernew.GetLayerDefn())
  20. feat.SetGeometry(geom)
  21. layernew.CreateFeature(feat)
  22. newds.Destroy()

属性

创建属性

  1. from osgeo import ogr
  2. import os,math
  3. driver = ogr.GetDriverByName("ESRI Shapefile") #shp驱动器
  4. extfile = 'rect_field_demo.shp' #输出的shp路径
  5. if os.access( extfile, os.F_OK ): #文件是否已存在
  6. driver.DeleteDataSource( extfile )
  7. extent = [400, 1100, 300, 600] #范围
  8. newds = driver.CreateDataSource(extfile) #按照驱动器创建数据源
  9. layernew = newds.CreateLayer('rect',None,ogr.wkbPolygon) #创建图层
  10. # 创建字段
  11. fieldcnstr = ogr.FieldDefn("fd",ogr.OFTString) #创建字段(字段名,类型)
  12. fieldcnstr.SetWidth(32) #设置宽度
  13. layernew.CreateField(fieldcnstr) #将字段设置到layer
  14. fieldf = ogr.FieldDefn("f",ogr.OFTReal)
  15. layernew.CreateField(fieldf)
  16. # wkt格式的polygon
  17. wkt = 'POLYGON ((%f %f,%f %f,%f %f,%f %f,%f %f))' % (extent[0],extent[3],
  18. extent[1],extent[3], extent[1],extent[2],
  19. extent[0],extent[2], extent[0],extent[3])
  20. geom = ogr.CreateGeometryFromWkt(wkt) #根据wkt创建geometry
  21. feat = ogr.Feature( layernew.GetLayerDefn() ) #从layer中得到feature的要求,创建一个Feature对象
  22. feat.SetField('fd',"这里是字段的值") #设置字段值
  23. feat.SetGeometry(geom) #设置要素的geometry属性
  24. layernew.CreateFeature(feat) #在图层中创建该feature
  25. newds.Destroy() #删除(包括保存)

Geometry

创建点Geometry

  1. from osgeo import ogr
  2. point = ogr.Geometry(ogr.wkbPoint)
  3. point.AddPoint(10,20) #向Point添加多个点,不会报错,但最终只会用最后添加的点

创建线Geometry

  1. from osgeo import ogr
  2. line = ogr.Geometry(ogr.wkbLineString) # 创建
  3. line.AddPoint(10, 20) # 添加点
  4. line.SetPoint(1, 50, 50) # 修改点坐标
  5. line.GetPointCount() # 得到所有点数目
  6. line.GetX(0); line.GetY(0) # 读取0号点的坐标

创建多边形Geometry

  1. from osgeo ipmort ogr
  2. # 创建环
  3. ring = ogr.Geometry(ogr.wkbLinearRing) #创建
  4. ring.AddPoint(10,10) #添加点
  5. ring.CloseRings() #闭合 或 添加的最后一个点和第一个相同
  6. # 将环添加到多边形中
  7. polygon = ogr.Geometry(ogr.wkbPolygon)
  8. polygon.AddGeometry(outring)
  9. polygon.AddGeometry(inring)
  10. ring = polygon.GetGeometryRef(0) #得到多边形内的几何对象