[gdal-dev] How to read all metadata from GeoTIFF file?

Jon Morris Jon.Morris at jbarisk.com
Fri Sep 14 01:43:13 PDT 2018


Hi Kristofer,

We have a function to transfer the tags manually, but most of the information is in the domain without a name - you have to call ds.GetMetadata(''). It goes something like this. For the bands, we're only copying some items, but you could take out the for loop and copy them all.


BASE_METADATA_DOMAIN = ''
IMAGE_METADATA_DOMAIN = 'IMAGE_STRUCTURE'


def set_tags_for_domain(src, dst, domain):
    src_tags = src.GetMetadata(domain)
    dst_tags = dst.GetMetadata(domain)
    dst_tags.update(src_tags)
    dst.SetMetadata(dst_tags, domain)


def transfer_tags(src, dst, band_order=None, dest_band=None):
    """Transfer metadata tags from one dataset to another."""
set_tags_for_domain(src, dst, BASE_METADATA_DOMAIN)
set_tags_for_domain(src, dst, IMAGE_METADATA_DOMAIN)

bands = band_order or range(1, dst.RasterCount + 1)
for i, band in enumerate(bands):
bnd_tags = src.GetRasterBand(band).GetMetadata(BASE_METADATA_DOMAIN)
dst_bnd = dst.GetRasterBand(dest_band or i + 1)

# copy selected items
for mditem in ("RETURN_PERIOD", "UNITS", "SCALE"):
if mditem in bnd_tags:
dst_bnd.SetMetadataItem(mditem, bnd_tags[mditem])


Regards,

Jon


-----Original Message-----
From: gdal-dev <gdal-dev-bounces at lists.osgeo.org> On Behalf Of krikr42
Sent: 12 September 2018 09:49
To: gdal-dev at lists.osgeo.org
Subject: [gdal-dev] How to read all metadata from GeoTIFF file?

Hi,

I have two tiff images, of which the first one is a GeoTIFF image and the second is a tiff image created from the first one by opening it with pyvips in Python, modifying it (blurring it) and saving it as a new file (so I don't know whether the second image counts as a GeoTIFF file). However, the second image lacks a lot of metadata that exists in the first image which I need, such as the coordinate system and corner coordinates.

How can I read this metadata from the first file with GDAL (in Python, although I guess the C++ API is pretty similar)? I have already tried loading the file as a GDAL dataset using  gdal.Open
<https://gdal.org/python/osgeo.gdal-module.html>   and reading the data with
the  GetMetadata
<https://www.gdal.org/classGDALMajorObject.html#a8ce3bf5795bbebfe9bc643e2152bb360>
method, and I have looked in the  domains
<https://www.gdal.org/gdal_datamodel.html>   'SUBDATASETS',
'IMAGE_STRUCTURE', 'RPC', 'IMAGERY' as well as the default domain, but none of those contain the information about the coordinate system or the coordinates. There are supposedly also xml: domains, and I'm wondering whether the data I'm looking for may be under an xml domain, but I don't know what the name of that domain would be, and I can't find any way to list available domains (is there any? Because that would be great).

So, how can I read the metadata specifying the coordinate system and corner coordinates from the first file and write it to the other? If I can read it with GetMetadata, I suppose I will also be able to write it with SetMetadata <https://www.gdal.org/classGDALMajorObject.html#a61ab7226d95b20e3e1f42461a1f62906>
(I was able to do that for the domains listed above).

I have also tried to transfer the information by reading it with the command line tool gdalinfo and writing it with gdal_translate, but that fails as 1) I don't know how to write all the relevant metadata with gdal_translate and
2) the metadata that I try to write isn't successfully written as the coordinate system information – which looks to be JSON-formatted – ends up with a different JSON-tag in the root element and with some elements missing.

Regards

Kristofer



--
Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/gdal-dev
T +44 (0) 1756 799919
www.jbarisk.com<http://www.jbarisk.com>

[Visit our website]<http://www.jbarisk.com> [http://www.jbagroup.co.uk/imgstore/JBA-Email-Sig-Icons-LINKEDIN.png] <https://www.linkedin.com/in/jon-morris-a2897b4/>  [Follow us on Twitter] <https://twitter.com/jbarisk>


More information about the gdal-dev mailing list