将进度条添加到gdal.Warp()

时间:2017-11-08 15:45:32

标签: python-2.7 gdal tqdm

我试图找出一种方法来在gdal.Warp()中使用进度条来显示已完成的工作量。对于进度条,我使用Tqdm和gdal.Warp()用于从远程URL裁剪图像

def getSubArea(url):
  vsicurl_url = '/vsicurl/' + url
  output_file = 'someID_subarea.tif'
  gdal.SetConfigOption('GDAL_HTTP_UNSAFESSL', 'YES')
  gdal.Warp(output_file, vsicurl_url, dstSRS='EPSG:4326', cutlineDSName='area.geojson', cropToCutline=True)

我知道有callback参数报告从0到1的进度,但只有在gdal.warp完成下载裁剪图像后才会调用它。

1 个答案:

答案 0 :(得分:1)

您可以通过'gdal.Warp'(文档:https://gdal.org/python/)中的'kwargs'参数为进度添加回调函数。

代码:

def getSubArea(url):
  vsicurl_url = '/vsicurl/' + url
  output_file = 'someID_subarea.tif'
  # Data you want to pass to your callback (goes in to unknown parameter)
  es_obj = { ... }
  kwargs = {
    'dstSRS': 'EPSG:4326',
    'cutlineDSName': 'area.geojson',
    'cropToCutline': True,
    'callback': progress_callback,
    'callback_data': es_obj
  }
  gdal.SetConfigOption('GDAL_HTTP_UNSAFESSL', 'YES')
  gdal.Warp(output_file, vsicurl_url, **kwargs)

def progress_callback(self, complete, message, unknown):
  # Calculate percent by integer values (1, 2, ..., 100)
  percent = floor(complete * 100)
  # Code for saving or using percent value
  ...

关于进度回调:https://gdal.org/api/cpl.html#_CPPv416GDALProgressFunc