Skip to content
Snippets Groups Projects
Commit 0e65b721 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Bugfix

geo.raster.conversion:
- raster2polygon: bugfix for not properly handling bad exit status of gdal.Polygonize()

processing.progress_mon:
- Timer:
    - implemented keyword use_as_callback (now properly raises KeyBoardInterrupt in case of TimeOut
    - improved formatting of elapsed time
- ProgressBar:
    - implemented keyword use_as_callback

- updated __version__
parent 428cf5c2
Branches
Tags v20170123_01
No related merge requests found
......@@ -15,7 +15,7 @@ __all__=[#'compatibility',
'similarity',
'GeoArray']
__version__ = '20170120_02'
__version__ = '20170123_01'
__author__='Daniel Scheffler'
# Validate GDAL version
......
......@@ -54,19 +54,19 @@ def raster2polygon(array_or_GeoArray, gt=None, prj=None, DN2extract=1, exact=Tru
mem_layer.CreateField(fd)
# set callback
callback = ProgressBar(prefix='Polygonize progress ', suffix='Complete', barLength=50, timeout=timeout) \
if progress and not q else Timer(timeout) if timeout else None
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
result = gdal.Polygonize(src_band, src_band.GetMaskBand(), mem_layer, 0, ["8CONNECTED=8"] if exact else [],
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!') if PY3 else TimeoutError_comp('raster2polygon timed out!')
if result is None:
raise Exception(errMsg)
# extract polygon
......
......@@ -3,18 +3,26 @@ __author__ = "Daniel Scheffler"
import sys
from time import time
from datetime import timedelta
class Timer(object):
def __init__(self, timeout=None):
def __init__(self, timeout=None, use_as_callback=False):
self.starttime = time()
self.endtime = self.starttime+timeout if timeout else None
self.use_as_cb = use_as_callback
@property
def timed_out(self):
if self.endtime:
if time() > self.endtime:
if self.use_as_cb:
raise KeyboardInterrupt
else:
return True
else:
if self.use_as_cb:
pass
else:
return False
else:
......@@ -22,7 +30,8 @@ class Timer(object):
@property
def elapsed(self):
return '%.2f sek' %(time()-self.starttime)
return str(timedelta(seconds=time()-self.starttime)).split('.')[0]
#return '%.2f sek' %(time()-self.starttime)
def __call__(self, percent01, message, user_data):
......@@ -40,7 +49,7 @@ class Timer(object):
class ProgressBar(object):
def __init__(self, prefix = '', suffix = 'Complete', decimals = 1, barLength = 50, show_elapsed=True,
timeout=None):
timeout=None, use_as_callback=False):
"""Call an instance of this class in a loop to create terminal progress bar. This class can also be used as
callback function, e.g. for GDAL. Just pass an instance of ProgressBar to the respective callback keyword.
......@@ -59,6 +68,7 @@ class ProgressBar(object):
self.barLength = barLength
self.show_elapsed = show_elapsed
self.Timer = Timer(timeout=timeout)
self.use_as_cb = use_as_callback
def print_progress(self, percent):
"""Based on http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console
......@@ -68,6 +78,7 @@ class ProgressBar(object):
"""
if self.Timer.timed_out:
sys.stdout.flush()
if self.use_as_cb:
raise KeyboardInterrupt
formatStr = "{0:." + str(self.decimals) + "f}"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment