Skip to content

Commit

Permalink
refactor: save local standardised file gdalinfo in FileTiff object TD…
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfouquet authored Feb 11, 2024
1 parent 94051b8 commit e69cdc5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
16 changes: 12 additions & 4 deletions scripts/files/file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,27 @@ def set_path_standardised(self, path: str) -> None:
"""
self._path_standardised = path

def get_gdalinfo(self) -> Optional[GdalInfo]:
def get_gdalinfo(self, path: Optional[str] = None) -> Optional[GdalInfo]:
"""Get the `gdalinfo` output for the file.
Run gdalinfo if not already ran.
Run gdalinfo if not already ran or if different path is specified.
`path` is useful to specify a local file to avoid downloading from external source.
Args:
path: path to the file. Force the `gdalinfo` to be executed.
Returns:
the `gdalinfo` output
"""
# FIXME: Should not return None but not try running `gdalinfo` if there has already been an error
if self.is_error_type(FileTiffErrorType.GDAL_INFO):
return None
if not self._gdalinfo:
if path:
file_path = path
else:
file_path = self._path_standardised
if path or not self._gdalinfo:
try:
self._gdalinfo = gdal_info(self._path_standardised)
self._gdalinfo = gdal_info(file_path)
except json.JSONDecodeError as jde:
self.add_error(error_type=FileTiffErrorType.GDAL_INFO, error_message=f"parsing result issue: {str(jde)}")
except GDALExecutionException as gee:
Expand Down
4 changes: 4 additions & 0 deletions scripts/standardising.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ def standardising(
# Need GDAL to write to temporary location so no broken files end up in the done folder.
run_gdal(command, input_file=input_file, output_file=standardized_working_path)

# Update the `FileTiff.gdalinfo` as the system has local access to the TIFF
get_log().debug("Saving gdalinfo from local standardised TIFF in FileTiff object", path=standardized_working_path)
tiff.get_gdalinfo(standardized_working_path)

with TiffFile(standardized_working_path) as file_handle:
if any(tile_byte_count != 0 for tile_byte_count in file_handle.pages.first.tags["TileByteCounts"].value):
write(standardized_file_path, read(standardized_working_path), content_type=ContentType.GEOTIFF.value)
Expand Down

0 comments on commit e69cdc5

Please sign in to comment.