Commit c149ede1 authored by mlang's avatar mlang
Browse files

Enable writing metadata in tif file

- when writing file or afterward
- update gitgnore
- fix concatenate_rasters() call in apply_mask method
No related merge requests found
Showing with 89 additions and 2 deletions
+89 -2
...@@ -60,3 +60,9 @@ data/l8_20130707\.tif\.aux\.xml ...@@ -60,3 +60,9 @@ data/l8_20130707\.tif\.aux\.xml
data/l8_20130831\.tif\.aux\.xml data/l8_20130831\.tif\.aux\.xml
qgis-auth\.db qgis-auth\.db
ymraster/\.spyderproject
\.spyderworkspace
\.spyderproject
...@@ -163,6 +163,8 @@ def write_file(out_filename, array=None, overwrite=True, ...@@ -163,6 +163,8 @@ def write_file(out_filename, array=None, overwrite=True,
projection to write in the output file metadata. projection to write in the output file metadata.
transform : 6-tuple of floats transform : 6-tuple of floats
Geo-transformation to use for the output file. Geo-transformation to use for the output file.
band_info : list of string:
Info of each band to write in the output file metadata
""" """
# Size & data type of output image # Size & data type of output image
xsize, ysize = (kw['width'], kw['height']) \ xsize, ysize = (kw['width'], kw['height']) \
...@@ -194,7 +196,8 @@ def write_file(out_filename, array=None, overwrite=True, ...@@ -194,7 +196,8 @@ def write_file(out_filename, array=None, overwrite=True,
# Set metadata # Set metadata
if kw.get('date_time'): if kw.get('date_time'):
out_ds.SetMetadata( out_ds.SetMetadata(
{'TIFFTAG_DATETIME': kw['date_time'].strftime('%Y:%m:%d %H:%M:%S')}) {'TIFFTAG_DATETIME':
kw['date_time'].strftime('%Y:%m:%d %H:%M:%S')})
if kw.get('srs'): if kw.get('srs'):
out_ds.SetProjection(kw['srs'].ExportToWkt()) out_ds.SetProjection(kw['srs'].ExportToWkt())
if kw.get('transform'): if kw.get('transform'):
...@@ -209,6 +212,10 @@ def write_file(out_filename, array=None, overwrite=True, ...@@ -209,6 +212,10 @@ def write_file(out_filename, array=None, overwrite=True,
band.WriteArray(array, xoff=xoffset, yoff=yoffset) band.WriteArray(array, xoff=xoffset, yoff=yoffset)
if kw.get('nodata_value'): if kw.get('nodata_value'):
band.SetNoDataValue(kw.get('nodata_value')) band.SetNoDataValue(kw.get('nodata_value'))
if kw.get('band_info'):
metadata = band.GetMetadata()
metadata['band_info'] = kw.get('band_info')[0]
band.SetMetadata(metadata)
band.FlushCache() band.FlushCache()
else: else:
for i in range(number_bands): for i in range(number_bands):
...@@ -216,6 +223,10 @@ def write_file(out_filename, array=None, overwrite=True, ...@@ -216,6 +223,10 @@ def write_file(out_filename, array=None, overwrite=True,
band.WriteArray(array[:, :, i], xoff=xoffset, yoff=yoffset) band.WriteArray(array[:, :, i], xoff=xoffset, yoff=yoffset)
if kw.get('nodata_value'): if kw.get('nodata_value'):
band.SetNoDataValue(kw.get('nodata_value')) band.SetNoDataValue(kw.get('nodata_value'))
if kw.get('band_info'):
metadata = band.GetMetadata()
metadata['band_info'] = kw.get('band_info')[i]
band.SetMetadata(metadata)
band.FlushCache() band.FlushCache()
band = None band = None
out_ds = None out_ds = None
...@@ -660,6 +671,76 @@ class Raster(Sized): ...@@ -660,6 +671,76 @@ class Raster(Sized):
# Close file # Close file
ds = None ds = None
def write_metadata(self, metadata):
"""
Write metada in the file
Parameters
----------
metadata : dictionnary
Key are Tag of the metadata, values is the corresponding
description
Limitations
-----------
Works for Tif files only.
"""
ds = gdal.Open(self._filename, gdal.GA_Update)
metadata_orign = ds.GetMetadata()
metadata_orign.update(metadata)
ds.SetMetadata(metadata_orign)
ds = None
self.refresh()
def write_band_metadata(self, idx, metadata):
"""
Write band metada in the file
Parameters
----------
idx : int
Band number. Indexation starts at 1.
metadata : dictionnary
Key are Tag of the metadata, values is the corresponding
description
Limitations
-----------
Works for Tif files only.
"""
ds = gdal.Open(self._filename, gdal.GA_Update)
band = ds.GetRasterBand(idx)
metadata_orign = band.GetMetadata()
metadata_orign.update(metadata)
band.SetMetadata(metadata)
ds = None
self.refresh()
def get_band_metadata(self, idx):
"""
Get band metada in the file
Parameters
----------
idx : int
Band number. Indexation starts at 1.
Returns
-------
metadata : dictionnary
Key are Tag of the metadata, values is the corresponding
description
"""
ds = gdal.Open(self._filename, gdal.GA_Update)
band = ds.GetRasterBand(idx)
metadata = band.GetMetadata()
ds = None
return metadata
@otb_function @otb_function
def change_dtype(self, out_dtype, out_filename): def change_dtype(self, out_dtype, out_filename):
""" """
...@@ -1597,7 +1678,7 @@ class Raster(Sized): ...@@ -1597,7 +1678,7 @@ class Raster(Sized):
masked_bands = [Raster(os.path.join(gettempdir(), masked_bands = [Raster(os.path.join(gettempdir(),
'mono_masked_{}.tif'.format(i))) 'mono_masked_{}.tif'.format(i)))
for i in range(self._count)] for i in range(self._count)]
concatenate_rasters(*masked_bands, out_filename=out_filename) concatenate_rasters(masked_bands, out_filename=out_filename)
out_raster = Raster(out_filename) out_raster = Raster(out_filename)
out_raster.nodata_value = set_value out_raster.nodata_value = set_value
print set_value print set_value
......
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