Skip to content

Commit

Permalink
Merge pull request #319 from johntruckenbrodt/feature/dem_autoload_vr…
Browse files Browse the repository at this point in the history
…t_lock

[DEMHandler] apply file lock to VRTs
  • Loading branch information
johntruckenbrodt authored Sep 17, 2024
2 parents db1eae8 + 4a99280 commit 7a9bec6
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions pyroSAR/auxdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def __find_first(self, dem_type, product):
@staticmethod
def __buildvrt(tiles, vrtfile, pattern, vsi, extent, src_nodata=None,
dst_nodata=None, hide_nodata=False, resolution=None,
tap=True, dst_datatype=None):
tap=True, dst_datatype=None, lock_timeout=600):
"""
Build a VRT mosaic from DEM tiles. The VRT is cropped to the specified `extent` but the pixel grid
of the source files is preserved and no resampling/shifting is applied.
Expand Down Expand Up @@ -510,6 +510,8 @@ def __buildvrt(tiles, vrtfile, pattern, vsi, extent, src_nodata=None,
dst_datatype: int or str or None
the VRT data type as supported by :class:`spatialist.raster.Dtype`.
Default None: use the same data type as the source files.
lock_timeout: int
how long to wait to acquire a lock on `vrtfile`?
Returns
-------
Expand All @@ -534,14 +536,21 @@ def __buildvrt(tiles, vrtfile, pattern, vsi, extent, src_nodata=None,
opts['VRTNodata'] = dst_nodata
opts['outputBounds'] = (extent['xmin'], extent['ymin'],
extent['xmax'], extent['ymax'])
gdalbuildvrt(src=locals, dst=vrtfile, **opts)
if dst_datatype is not None:
datatype = Dtype(dst_datatype).gdalstr
tree = etree.parse(source=vrtfile)
band = tree.find(path='VRTRasterBand')
band.attrib['dataType'] = datatype
tree.write(file=vrtfile, pretty_print=True,
xml_declaration=False, encoding='utf-8')
lock = None
if os.access(vrtfile, os.W_OK):
# lock only if writable, not e.g. vsimem
lock = Lock(vrtfile, timeout=lock_timeout)
if not os.path.isfile(vrtfile):
gdalbuildvrt(src=locals, dst=vrtfile, **opts)
if dst_datatype is not None:
datatype = Dtype(dst_datatype).gdalstr
tree = etree.parse(source=vrtfile)
band = tree.find(path='VRTRasterBand')
band.attrib['dataType'] = datatype
tree.write(file=vrtfile, pretty_print=True,
xml_declaration=False, encoding='utf-8')
if lock is not None:
lock.remove()

def __commonextent(self, buffer=None):
"""
Expand Down Expand Up @@ -1057,7 +1066,7 @@ def load(self, dem_type, vrt=None, buffer=None, username=None,
bounding box of the geometries is expanded so that the coordinates are
multiples of the tile size of the respective DEM option.
lock_timeout: int
how long to wait to acquire a lock on downloaded files?
how long to wait to acquire a lock on the downloaded files and `vrt`?
Returns
-------
Expand Down Expand Up @@ -1153,7 +1162,8 @@ def load(self, dem_type, vrt=None, buffer=None, username=None,
src_nodata=src_nodata, dst_nodata=dst_nodata,
hide_nodata=True,
resolution=resolution,
tap=tap, dst_datatype=datatype)
tap=tap, dst_datatype=datatype,
lock_timeout=lock_timeout)
else:
return locals

Expand Down

0 comments on commit 7a9bec6

Please sign in to comment.