Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEMHandler] apply file lock to VRTs #319

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading