Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Progress Bar to gdal.Warp()

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.

like image 883
LithStud Avatar asked Nov 07 '22 14:11

LithStud


2 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

like image 101
Shimon Cohen Avatar answered Dec 02 '22 05:12

Shimon Cohen


See answer here around native gdal callback function.

If you wanted to report the download progress of the raster, you'd likely need to split that out as a separate step using something like requests and wrapping that with a progress bar like tqdm or progressbar2.

like image 33
JF_NZ Avatar answered Dec 02 '22 05:12

JF_NZ