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

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

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

krikr42
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
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to read all metadata from GeoTIFF file?

Travis Kirstine
Kristofer,

Could you use the gdal createcopy method first to create the 'second' image before opening it with pyvips


fileformat = "GTiff"
driver = gdal.GetDriverByName(fileformat)
src_ds = gdal.Open(src_filename)
dst_ds = driver.CreateCopy(dst_filename, src_ds, strict=0)
# Once we're done, close properly the dataset
dst_ds = None
src_ds = None

Regards


On Wed, 12 Sep 2018 at 04:48, krikr42 <[hidden email]> wrote:
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
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to read all metadata from GeoTIFF file?

Jon Morris
In reply to this post by krikr42
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 <[hidden email]> On Behalf Of krikr42
Sent: 12 September 2018 09:49
To: [hidden email]
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
[hidden email]
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>
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to read all metadata from GeoTIFF file?

David Strip-2
If you're willing to use command line tools, there is a pair of tools that ship with libgeotiff for extracting metadata from a geotiff and importing into a tiff to make it a geotiff.

Given a GeoTIFF file named original.tif, and a modified file (modified.tif) without the GeoTIFF tags, but still the same size and region:

  listgeo -no_norm original.tif > original.geo
  geotifcp -g original.geo modified.tif modified_geotiff.tif


_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to read all metadata from GeoTIFF file?

Even Rouault-2
In reply to this post by krikr42
On mercredi 12 septembre 2018 01:48:34 CEST krikr42 wrote:

> 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#a8ce3bf5795bbebfe9bc643e2152
> bb360> 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#a61ab7226d95b20e3e1f42461a1f
> 62906> (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.

You need to use SetGeoTransform() / GetGeoTransform() and SetProjection() /
GetProjectionRef() to transfer georeferencing information.
Overall presentation at https://gdal.org/gdal_datamodel.html


--
Spatialys - Geospatial professional services
http://www.spatialys.com
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to read all metadata from GeoTIFF file?

krikr42
Hi everybody,

Thanks for your replies! The method that worked in the end was to use an external tool called geotifcp (as David Strip-2 wrote). For some reason, all methods I tried that involved GDAL only copied parts of the metadata.
Travis Kirstine wrote
Could you use the gdal createcopy method first to create the 'second' image before opening it with pyvips
Why should I copy the dataset that way when I can just copy the file directly on the file system? The problem is that pyvips doesn't recognize the GeoTIFF-specific metadata, so the metadata is lost anyway when I open it in pyvips to edit it.
Jon Morris wrote
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.
Yes – the domain with an empty name is the default domain, which I also include in the domains I looped over to fetch the metadata from. It did contain some of the metadata I wanted to copy, but not any of the metadata related to the coordinate system and location of the image within that coordinate system.
Jon Morris wrote
bnd_tags = src.GetRasterBand(band).GetMetadata(BASE_METADATA_DOMAIN)
I didn't know that raster bands could also contain metadata! Anyway, the raster bands in my GeoTIFF image don't contain any metadata, that is, they don't have any entries in any of the existing metadata domains, at least not for the non-XML domains.

Speaking of which, I'm still wondering how to list available domains, including XML domains, because there may be some XML domains in the GeoTIFF file that I'm not aware know of, and the metadata I need to copy could exist in those. How do you do that?
David Strip-2 wrote
If you're willing to use command line tools, there is a pair of tools that ship with libgeotiff for extracting metadata from a geotiff and importing into a tiff to make it a geotiff. Given a GeoTIFF file named original.tif, and a modified file (modified.tif) without the GeoTIFF tags, but still the same size and region:
    listgeo -no_norm original.tif > original.geo
    geotifcp -g original.geo modified.tif modified_geotiff.tif
That's exactly what I found was working too!
Even Rouault-2 wrote
You need to use SetGeoTransform() / GetGeoTransform() and SetProjection() / GetProjectionRef() to transfer georeferencing information. Overall presentation at https://gdal.org/gdal_datamodel.html
I did that, and that did transfer some of the metadata related to the coordinate system and location of the image within that coordinate system, but not all of it. Together with GetMetadata() and SetMetadata(), it seems to have copied the same metadata as when I used the command line tools gdalinfo and gdal_translate.

Regards

Kristofer

Sent from the GDAL - Dev mailing list archive at Nabble.com.

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev