Skip to content

Commit 1e63372

Browse files
Merge pull request #376 from johntruckenbrodt/bugfix/dem_ocean
DEM handling over ocean
2 parents dd8b191 + 0296ee8 commit 1e63372

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

pyroSAR/auxdata.py

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
###############################################################################
22
# tools for handling auxiliary data in software pyroSAR
33

4-
# Copyright (c) 2019-2024, the pyroSAR Developers.
4+
# Copyright (c) 2019-2025, the pyroSAR Developers.
55

66
# This file is part of the pyroSAR Project. It is subject to the
77
# license terms in the LICENSE.txt file found in the top-level
@@ -552,35 +552,33 @@ def __commonextent(self, buffer=None):
552552
return ext_new
553553

554554
@staticmethod
555-
def __create_dummy_dem():
555+
def __create_dummy_dem(filename, extent):
556556
"""
557-
Create a dummy file which spans the whole globe and is 1x1 pixels large to be as small as possible.
557+
Create a dummy file which spans the given extent and
558+
is 1x1 pixels large to be as small as possible.
558559
This file is used to create dummy DEMs over ocean.
559-
560-
Returns
561-
-------
562-
str
563-
the name of the file in the user's home directory: ~/.pyrosar/auxdata/dummy_dem.tif
564560
"""
565-
path = os.path.join(os.path.expanduser('~'), '.pyrosar', 'auxdata')
566-
os.makedirs(name=path, exist_ok=True)
567-
filename = os.path.join(path, 'dummy_dem.tif')
568-
if not os.path.isfile(filename):
569-
driver = gdal.GetDriverByName('GTiff')
570-
dataset = driver.Create(filename, 1, 1, 1, 1)
571-
geo = [-180, 360, 0, 90, 0, -180]
572-
dataset.SetGeoTransform(geo)
573-
dataset.SetProjection('EPSG:4326')
574-
band = dataset.GetRasterBand(1)
575-
band.SetNoDataValue(255)
576-
mat = numpy.zeros(shape=(1, 1))
577-
band.WriteArray(mat, 0, 0)
578-
band.FlushCache()
579-
del mat
580-
band = None
581-
dataset = None
582-
driver = None
583-
return filename
561+
driver = gdal.GetDriverByName('GTiff')
562+
dataset = driver.Create(filename, 1, 1, 1, 1)
563+
geo = [
564+
extent['xmin'],
565+
extent['xmax'] - extent['xmin'],
566+
0,
567+
extent['ymax'],
568+
0,
569+
extent['ymin'] - extent['ymax'] # negative
570+
]
571+
dataset.SetGeoTransform(geo)
572+
dataset.SetProjection('EPSG:4326')
573+
band = dataset.GetRasterBand(1)
574+
band.SetNoDataValue(255)
575+
mat = numpy.zeros(shape=(1, 1))
576+
band.WriteArray(mat, 0, 0)
577+
band.FlushCache()
578+
del mat
579+
band = None
580+
dataset = None
581+
driver = None
584582

585583
@staticmethod
586584
def intrange(extent, step):
@@ -1104,19 +1102,21 @@ def load(self, dem_type, vrt=None, buffer=None, username=None,
11041102
extent['ymax'] += shift_y
11051103

11061104
# special case where no DEM tiles were found because the AOI is completely over ocean
1107-
if len(locals) == 0:
1108-
if vrt is not None:
1109-
# define a dummy file as source file; this file contains one pixel spanning the whole globe
1110-
# this pixel has value 0, nodata value is 255
1111-
locals = [self.__create_dummy_dem()]
1112-
datatype = self.config[dem_type]['datatype'][product]
1113-
src_nodata = 0 # define the data value as nodata, so it can be overwritten in the VRT
1114-
if product == 'dem':
1115-
dst_nodata = 0
1116-
else:
1117-
dst_nodata = self.config[dem_type]['nodata'][product]
1118-
# determine the target resolution based on minimum latitude
1119-
resolution = self.__get_resolution(dem_type=dem_type, y=extent['ymin'])
1105+
if len(locals) == 0 and vrt is not None:
1106+
# define a dummy file as source file
1107+
# his file contains one pixel with a value of 0
1108+
# nodata value is 255
1109+
tif = vrt.replace('.vrt', '_tmp.tif')
1110+
self.__create_dummy_dem(filename=tif, extent=extent)
1111+
locals = [tif]
1112+
datatype = self.config[dem_type]['datatype'][product]
1113+
src_nodata = 0 # define the data value as nodata, so it can be overwritten in the VRT
1114+
if product == 'dem':
1115+
dst_nodata = 0
1116+
else:
1117+
dst_nodata = self.config[dem_type]['nodata'][product]
1118+
# determine the target resolution based on minimum latitude
1119+
resolution = self.__get_resolution(dem_type=dem_type, y=extent['ymin'])
11201120

11211121
# make sure all GETASSE30 tiles get an ENVI HDR file so that they are GDAL-readable
11221122
if dem_type == 'GETASSE30':
@@ -1513,5 +1513,8 @@ def vrt_check_sources(fname):
15131513
tree = etree.parse(fname)
15141514
sources = [x.text for x in tree.findall('.//SourceFilename')]
15151515
for source in sources:
1516+
if not os.path.isabs(source):
1517+
base_dir = os.path.dirname(fname)
1518+
source = os.path.normpath(os.path.join(base_dir, source))
15161519
if not os.path.isfile(source):
15171520
raise RuntimeError(f'missing VRT source file: {source}')

0 commit comments

Comments
 (0)