Commit 30968972 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

added two functions to GeoArray

ptds.io.raster.GeoArray.GeoArray:
- added function calc_mask_nodata()
- added function cache_array_subset()
- added setter to 'mask_nodata' property
parent b4a998ca
......@@ -18,11 +18,11 @@ from rasterio.warp import reproject as rio_reproject
from rasterio.warp import calculate_default_transform as rio_calc_transform
from rasterio.warp import Resampling
from ..projection import WKT2EPSG, isProjectedOrGeographic
from ..coord_trafo import pixelToLatLon
from ...io.raster.gdal import get_GDAL_ds_inmem
from ..projection import WKT2EPSG, isProjectedOrGeographic
from ..coord_trafo import pixelToLatLon
from ...io.raster.gdal import get_GDAL_ds_inmem
from ...processing.progress_mon import printProgress
from ...compatibility.gdal import get_gdal_func
from ...compatibility.gdal import get_gdal_func
# dictionary to translate GDAL data types (strings) in corresponding numpy data types
......@@ -364,8 +364,13 @@ def warp_ndarray(ndarray, in_gt, in_prj, out_prj=None, out_dtype=None, out_gsd=(
if CPUs is None or CPUs>1:
gdal.SetConfigOption('GDAL_NUM_THREADS', str(CPUs if CPUs else multiprocessing.cpu_count()))
#gdal.SetConfigOption('GDAL_CACHEMAX', str(800))
# GDAL Translate if needed
if gcpList:
#if gcpList:
# in_ds.SetGCPs(gcpList, in_ds.GetProjection())
if True:
gdal_Translate = get_gdal_func('Translate')
in_ds = gdal_Translate(
'', in_ds, format='MEM',
......
......@@ -258,14 +258,18 @@ class GeoArray(object):
return self._mask_nodata
else:
if self.nodata is not None:
self._mask_nodata = np.where(self[:]==self.nodata,0,1).astype(np.uint8) if self.ndim==2 else \
np.all(np.where(self[:]==self.nodata,0,1), axis=2).astype(np.uint8)
self.calc_mask_nodata() # sets self._mask_nodata
return self._mask_nodata
else:
warnings.warn('Calculation of nodata mask failed due to missing no data value.')
return None
@mask_nodata.setter
def mask_nodata(self, maskarray):
self._mask_nodata = maskarray.astype(np.uint8)
@property
def footprint_poly(self):
if self._footprint_poly is not None:
......@@ -348,6 +352,12 @@ class GeoArray(object):
self.__dict__ = state
def calc_mask_nodata(self, fromBand=None):
arr = self[:,:,fromBand]
self.mask_nodata = np.where(arr == self.nodata, 0, 1).astype(np.uint8) if self.ndim == 2 else \
np.all(np.where(arr == self.nodata, 0, 1), axis=2).astype(np.uint8)
def set_gdalDataset_meta(self):
assert self.filePath
ds = gdal.Open(self.filePath)
......@@ -750,6 +760,10 @@ class GeoArray(object):
return self
def cache_array_subset(self, subarray):
self._arr_cache = subarray
def flush_cache(self):
self._arr_cache = None
......
......@@ -18,7 +18,7 @@ def printProgress (percent, prefix = '', suffix = '', decimals = 1, barLength =
formatStr = "{0:." + str(decimals) + "f}"
percents = formatStr.format(percent)
filledLength = int(round(barLength * percent/100))
bar = '█' * filledLength + '-' * (barLength - filledLength)
#bar = '█' * filledLength + '-' * (barLength - filledLength) # this is not compatible to shell console
bar = '=' * filledLength + '-' * (barLength - filledLength)
sys.stdout.write('\r') # resets the cursor to the beginning of the line and allows to write over what was previously on the line
sys.stdout.write('%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
......
Supports Markdown
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