def RasterToPoly(raster_file_path, out_vec_path, DelBG=False): inraster = gdal.Open(raster_file_path) # 读取路径中的栅格数据 inband = inraster.GetRasterBand(1) # 这个波段就是最后想要转为矢量的波段,如果是单波段数据的话那就都是1 prj = osr.SpatialReference() prj.ImportFromWkt(inraster.GetProjection()) # 读取栅格数据的投影信息,用来为后面生成的矢量做准备 raster_file = os.path.basename(raster_file_path) raster_name, _ = os.path.splitext(raster_file) shp_file_path = os.path.join(out_vec_path, raster_name + '.shp') drv = ogr.GetDriverByName("ESRI Shapefile") if os.path.exists(shp_file_path): drv.DeleteDataSource(shp_file_path) Polygon = drv.CreateDataSource(shp_file_path) # 创建一个目标文件 Poly_layer = Polygon.CreateLayer( shp_file_path[:-4], srs=prj, geom_type=ogr.wkbMultiPolygon) # 对shp文件创建一个图层,定义为多个面类 newField = ogr.FieldDefn( 'Value', ogr.OFTReal) # 给目标shp文件添加一个字段,用来存储原始栅格的pixel value Poly_layer.CreateField(newField) gdal.FPolygonize(inband, None, Poly_layer, 0, ['8CONNECTED=8']) # 核心函数,执行的就是栅格转矢量操作 Polygon.SyncToDisk() Polygon = None if DelBG: deleteBackground(shp_file_path, 0) # 删除背景
def polygonize_1(is_int_polygonize=True): src_ds = gdal.Open('data/polygonize_in.grd') src_band = src_ds.GetRasterBand(1) # Create a memory OGR datasource to put results in. mem_drv = ogr.GetDriverByName('Memory') mem_ds = mem_drv.CreateDataSource('out') mem_layer = mem_ds.CreateLayer('poly', None, ogr.wkbPolygon) fd = ogr.FieldDefn('DN', ogr.OFTInteger) mem_layer.CreateField(fd) # run the algorithm. if is_int_polygonize: result = gdal.Polygonize(src_band, src_band.GetMaskBand(), mem_layer, 0) else: result = gdal.FPolygonize(src_band, src_band.GetMaskBand(), mem_layer, 0) if result != 0: gdaltest.post_reason('Polygonize failed') return 'fail' # Confirm we get the set of expected features in the output layer. expected_feature_number = 13 if mem_layer.GetFeatureCount() != expected_feature_number: gdaltest.post_reason( 'GetFeatureCount() returned %d instead of %d' % (mem_layer.GetFeatureCount(), expected_feature_number)) return 'fail' expect = [107, 123, 115, 115, 140, 148, 123, 140, 156, 100, 101, 102, 103] tr = ogrtest.check_features_against_list(mem_layer, 'DN', expect) # check at least one geometry. if tr: mem_layer.SetAttributeFilter('dn = 156') feat_read = mem_layer.GetNextFeature() if ogrtest.check_feature_geometry( feat_read, 'POLYGON ((440720 3751200,440720 3751020,440900 3751020,440900 3751200,440720 3751200),(440780 3751140,440840 3751140,440840 3751080,440780 3751080,440780 3751140))' ) != 0: tr = 0 feat_read.Destroy() if tr: return 'success' else: return 'fail'
dst_layer.CreateField(fd) dst_field = 0 else: if dst_fieldname is not None: dst_field = dst_layer.GetLayerDefn().GetFieldIndex(dst_fieldname) if dst_field < 0: print("Warning: cannot find field '%s' in layer '%s'" % (dst_fieldname, dst_layername)) # ============================================================================= # Invoke algorithm. # ============================================================================= if quiet_flag: prog_func = None else: prog_func = gdal.TermProgress_nocb # result = None if int_flag: result = gdal.Polygonize(srcband, maskband, dst_layer, dst_field, options, callback=prog_func) else: result = gdal.FPolygonize(srcband, maskband, dst_layer, dst_field, options, callback=prog_func) srcband = None src_ds = None dst_ds = None mask_ds = None