Commit 00152cf0 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

added warpMemoryLimit to warp_ndarray

geo.reproject:
- warp_ndarray: added warpMemoryLimit, updated defaults for warpOptions, transformerOptions

io.raster.GeoArray:
- _get_plottable_image(): bugfix for never reprojecting input image to desired projection if image size <1000x1000 px
parent 7066ee0b
......@@ -224,7 +224,7 @@ def warp_ndarray(ndarray, in_gt, in_prj, out_prj=None, out_dtype=None, out_gsd=(
out_bounds=None, out_bounds_prj=None, out_XYdims = (None,None),
rspAlg='near', in_nodata=None, out_nodata=None, in_alpha=False,
out_alpha=False, targetAlignedPixels=False, gcpList=None, polynomialOrder=None, options=None,
transformerOptions=None, warpOptions=None, CPUs=1, progress=True, q=False):
transformerOptions=None, warpOptions=None, CPUs=1, warpMemoryLimit=0, progress=True, q=False):
"""
:param ndarray: the numpy array to be warped
......@@ -252,8 +252,10 @@ def warp_ndarray(ndarray, in_gt, in_prj, out_prj=None, out_dtype=None, out_gsd=(
:param polynomialOrder: <int> order of polynomial GCP interpolation
:param options: <str> additional GDAL options as string, e.g. '-nosrcalpha' or '-order'
:param transformerOptions: <list> list of transformer options, e.g. ['SRC_SRS=invalid']
:param warpOptions: <list> list of warp options, e.g. ['CUTLINE_ALL_TOUCHED=TRUE']
:param warpOptions: <list> list of warp options, e.g. ['CUTLINE_ALL_TOUCHED=TRUE'],
find available options here: http://www.gdal.org/structGDALWarpOptions.html
:param CPUs: <int> number of CPUs to use (default: None, which means 'all CPUs available')
:param warpMemoryLimit: <int> size of working buffer in bytes (default: 0)
:param progress: <bool> show progress bar (default: True)
:param q: <bool> quiet mode (default: False)
:return:
......@@ -268,7 +270,6 @@ def warp_ndarray(ndarray, in_gt, in_prj, out_prj=None, out_dtype=None, out_gsd=(
'EPSG:%s'%prjArg if isinstance(prjArg,int) else \
prjArg
get_GDT = lambda DT: dTypeDic_NumPy2GDAL[str(np.dtype(DT))]
CPUs = CPUs if CPUs else multiprocessing.cpu_count()
progressBarTran = (lambda percent01, message, user_data: printProgress(percent01 * 100,
**{'prefix': 'Translating progress', 'suffix': 'Complete', 'barLength': 50})) if progress and not q else None
progressBarWarp = (lambda percent01, message, user_data: printProgress(percent01 * 100,
......@@ -298,7 +299,6 @@ def warp_ndarray(ndarray, in_gt, in_prj, out_prj=None, out_dtype=None, out_gsd=(
"SAMP_OFF=3300.34602166792",
"SAMP_SCALE=3297.51222987611"
]
WarpMemoryLimit = 0 # not sure if this is the correct keyword??
""" Create a WarpOptions() object that can be passed to gdal.Warp()
Keyword arguments are :
......@@ -352,7 +352,7 @@ def warp_ndarray(ndarray, in_gt, in_prj, out_prj=None, out_dtype=None, out_gsd=(
# transformerOptions = ['RPC_DEM=data/warp_52_dem.tif']
if CPUs is None or CPUs>1:
gdal.SetConfigOption('GDAL_NUM_THREADS', str(CPUs))
gdal.SetConfigOption('GDAL_NUM_THREADS', str(CPUs if CPUs else multiprocessing.cpu_count()))
# GDAL Translate if needed
if gcpList:
......@@ -364,7 +364,6 @@ def warp_ndarray(ndarray, in_gt, in_prj, out_prj=None, out_dtype=None, out_gsd=(
)
# NOTE: options = ['SPARSE_OK=YES'] ## => what is that for?
# GDAL Warp
out_ds = gdal.Warp(
'', in_ds, format='MEM',
......@@ -381,12 +380,13 @@ def warp_ndarray(ndarray, in_gt, in_prj, out_prj=None, out_dtype=None, out_gsd=(
dstNodata = out_nodata,
srcAlpha = in_alpha,
dstAlpha = out_alpha,
options = options if options else [],
warpOptions = warpOptions,
transformerOptions = transformerOptions,
options = options if options else [],
warpOptions = warpOptions if warpOptions else [],
transformerOptions = transformerOptions if transformerOptions else [],
targetAlignedPixels = targetAlignedPixels,
tps = True if gcpList else False,
polynomialOrder = polynomialOrder,
warpMemoryLimit = warpMemoryLimit,
callback = progressBarWarp,
errorThreshold = 0.125, # this is needed to get exactly the same output like the console version of GDAL warp
)
......
......@@ -423,14 +423,16 @@ class GeoArray(object):
image2plot = self[rS:rE, cS:cE, band]
gt, prj = self.geotransform, self.projection
transOpt = ['SRC_METHOD=NO_GEOTRANSFORM'] if tuple(gt) == (0, 1, 0, 0, 0, -1) else None
xdim, ydim = None, None
if res_factor != 1. and image2plot.shape[0] * image2plot.shape[1] > 1e6: # shape > 1000*1000
# sample image down
xdim, ydim = (self.cols * res_factor, self.rows * res_factor) if res_factor else \
tuple((np.array([self.cols, self.rows]) / (np.array([self.cols, self.rows]).max() / 1000))) # normalize
xdim, ydim = int(xdim), int(ydim)
transOpt = ['SRC_METHOD=NO_GEOTRANSFORM'] if tuple(gt)==(0,1,0,0,0,-1) else None
if xdim or ydim or out_prj:
from ...geo.raster.reproject import warp_ndarray
image2plot, gt, prj = warp_ndarray(image2plot, self.geotransform, self.projection,
out_XYdims=(xdim, ydim), in_nodata=nodataVal, out_nodata=nodataVal,
......@@ -440,7 +442,8 @@ class GeoArray(object):
gt=list(gt)
gt[3]=0
print('Note: array has been downsampled to %s x %s for faster visualization.' % (xdim, ydim))
if xdim or ydim:
print('Note: array has been downsampled to %s x %s for faster visualization.' % (xdim, ydim))
return image2plot, gt, prj
......
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