Commit 230affdc authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Fixed timeout issue for now.


Signed-off-by: Daniel Scheffler's avatarDaniel Scheffler <danschef@gfz-potsdam.de>
parent a691dc4c
Pipeline #22785 passed with stage
in 1 minute and 53 seconds
......@@ -74,34 +74,33 @@ def raster2polygon(array, gt, prj, DN2extract=1, exact=True, maxfeatCount=None,
fd = ogr.FieldDefn('DN', ogr.OFTInteger)
mem_layer.CreateField(fd)
try:
timeout = 0.01
# set callback
callback = \
ProgressBar(prefix='Polygonize progress ',
suffix='Complete',
barLength=50,
timeout=timeout,
use_as_callback=True) \
if progress and not q else Timer(timeout, use_as_callback=True) if timeout else None
# run the algorithm
status = gdal.Polygonize(src_band,
src_band.GetMaskBand(),
mem_layer,
0,
["8CONNECTED=8"] if exact else [],
callback=callback)
# handle exit status other than 0 (fail)
if status != 0:
errMsg = gdal.GetLastErrorMsg()
if errMsg == 'User terminated':
raise TimeoutError('raster2polygon timed out!')
raise Exception(errMsg)
except KeyboardInterrupt:
raise TimeoutError('raster2polygon timed out!')
# set callback
callback = \
ProgressBar(prefix='Polygonize progress ',
suffix='Complete',
barLength=50,
timeout=timeout,
use_as_callback=True) \
if progress and not q else Timer(timeout, use_as_callback=True) if timeout else None
# run the algorithm
status = gdal.Polygonize(src_band,
src_band.GetMaskBand(),
mem_layer,
0,
["8CONNECTED=8"] if exact else [],
callback=callback)
# handle exit status other than 0 (fail)
if status != 0:
errMsg = gdal.GetLastErrorMsg()
# Catch the KeyboardInterrupt raised in case of a timeout within the callback. It seems like there is no other
# way to catch exceptions within callbacks.
if errMsg == 'User terminated':
raise TimeoutError('raster2polygon timed out!')
raise Exception(errMsg)
# extract polygon
mem_layer.SetAttributeFilter('DN = %s' % DN2extract)
......
......@@ -32,6 +32,7 @@ class Timer(object):
def __init__(self, timeout=None, use_as_callback=False):
self.starttime = time()
self.endtime = self.starttime + timeout if timeout else None
self.timeout = timeout
self.use_as_cb = use_as_callback
@property
......@@ -39,7 +40,9 @@ class Timer(object):
if self.endtime:
if time() > self.endtime:
if self.use_as_cb:
raise KeyboardInterrupt
# raise a KeyBoardInterrupt instead of a TimeOutError
# as this is catchable by gdal.GetLastException()
raise KeyboardInterrupt()
else:
return True
else:
......@@ -88,6 +91,7 @@ class ProgressBar(object):
self.decimals = decimals
self.barLength = barLength
self.show_elapsed = show_elapsed
self.timeout = timeout
self.Timer = Timer(timeout=timeout)
self.use_as_cb = use_as_callback
self.out = out
......@@ -103,7 +107,9 @@ class ProgressBar(object):
if self.Timer.timed_out:
self.out.flush()
if self.use_as_cb:
raise KeyboardInterrupt
# raise a KeyBoardInterrupt instead of a TimeOutError
# as this is catchable by gdal.GetLastException()
raise KeyboardInterrupt()
formatStr = "{0:." + str(self.decimals) + "f}"
percents = formatStr.format(percent)
......@@ -124,7 +130,7 @@ class ProgressBar(object):
self.out.flush()
def __call__(self, percent01, message, user_data):
"""Allow ProgressBar instances ato be callable and thus to be used as callback function, e.g., for GDAL.
"""Allow ProgressBar instances to be callable and thus to be used as callback function, e.g., for GDAL.
:param percent01: a float number between 0 and 1
:param message: this is not used but expected when used as GDAL callback
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment