Extracting metadata from raster (tif) file using python in Knime

Hi,

I would like to extract all metadata from a raster tif file. I know this can be done with a python script in knime. I am using the custom conda environment with GDAL, GeoPandas and pySAL packages installed. I am unsure how to adapt the below script so that I can extract the data in knime and get on with it to get round the fact that raster files are currently not supported in Knime. I have used this script below but could extract the data. Can anyone help with this? Thanks

import knime.scripting.io as knio

from osgeo import gdal

import pandas as pd

Open the raster file

raster = gdal.Open(r"C:\Users\XXXXXXXXXXX\Downloads\Panagos (2022) Soil erosion 2050\2050\RCP85.tif")

Check type of the variable ‘raster’

type(raster)

Projection

raster.GetProjection()

Dimensions

raster.RasterXSize

raster.RasterYSize

Number of bands

raster.RasterCount

Get the metadata

Get the metadata

raster.GetMetadata()

metadata = raster.GetMetadata()

Output the metadata as a pandas DataFrame

raster_df = pd.DataFrame(list(metadata.items()), columns=[‘Key’, ‘Value’])

Output the DataFrame to the output table

knio.output_tables[0] = knio.Table.from_pandas(raster_df)

Hi @Leedsbash7,

I have recently tried finding a way to extract the XMP Meta data from images too and, whilst not Phyton, found a way to extract the information too using a simple RegEx

(?<XMP Meta Data><x:xmpmeta((?!</x:xmpmeta>).)+</x:xmpmeta>)

You basically:

  1. Create a list of Paths to each image
  2. Convert it to binary and remove all line breaks
  3. Extract XMP and convert to XML

PS: I created a sample workflow for you- Whilst not using a tif, this still might be applicable.

Please note that the Python-Approach is still WIP because of:

Best
Mike

Hi Mike,

Thanks a lot for this. I will give this a go sometimes this week and get back to you. I am currently warming up to python and python in knime. So it would be great if you you could fine the python solution too whenever possible.

I am also not used to XML so will try and figure it out.

Most importantly, I am very pleased you looked into this. Thanks again.