Adding Progress Bar to gdal.Warp()

Viewed 1007

I am trying to figure out a way to use progress bar in gdal.Warp() to show how much of a job is done. For progress bar, I am using Tqdm and gdal.Warp() is used to crop image from remote 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)

I know there is callback argument that reports progress from 0 to 1, but its only called after gdal.warp has finished downloading cropped image.

1 Answers

You may add a callback function for progress through the 'kwargs' parameter in 'gdal.Warp' (documentation: https://gdal.org/python/).

Code:

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
  ...

About progress callback: https://gdal.org/api/cpl.html#_CPPv416GDALProgressFunc

Related