Skip to content

Instantly share code, notes, and snippets.

@schwehr
Created February 22, 2013 19:57
Show Gist options
  • Save schwehr/5016127 to your computer and use it in GitHub Desktop.
Save schwehr/5016127 to your computer and use it in GitHub Desktop.
Using gdal to read/write the metadata fields for a geotiff and for specific bands within the geotiff.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "geotiff_metadata"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"from osgeo import gdal, gdalconst"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"geotiff_drv = gdal.GetDriverByName('GTiff')\n",
"w = h = 10\n",
"dst = geotiff_drv.Create('test.tif', w, h, 1, gdalconst.GDT_Byte)\n",
"\n",
"dst.SetDescription('Override the description')\n",
"\n",
"# All values must be strings\n",
"dst.SetMetadata( {'key1': '1', 'key2': 'yada'} )\n",
"\n",
"band = dst.GetRasterBand(1)\n",
"band.SetMetadata( {'bandkey1': 'foo', 'bandkey2': 'bar', 'bk3': '3.1415'} )\n",
"\n",
"band.WriteArray(np.random.random_integers(0,255, (10,10)))\n",
"dst.GetMetadata()\n",
"del band\n",
"del dst"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"!gdalinfo -mm test.tif"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Driver: GTiff/GeoTIFF\r\n",
"Files: test.tif\r\n",
"Size is 10, 10\r\n",
"Coordinate System is `'\r\n",
"Metadata:\r\n",
" key1=1\r\n",
" key2=yada\r\n",
"Image Structure Metadata:\r\n",
" INTERLEAVE=BAND\r\n",
"Corner Coordinates:\r\n",
"Upper Left ( 0.0, 0.0)\r\n",
"Lower Left ( 0.0, 10.0)\r\n",
"Upper Right ( 10.0, 0.0)\r\n",
"Lower Right ( 10.0, 10.0)\r\n",
"Center ( 5.0, 5.0)\r\n",
"Band 1 Block=10x10 Type=Byte, ColorInterp=Gray\r\n",
" Computed Min/Max=2.000,251.000\r\n",
" Metadata:\r\n",
" bandkey1=foo\r\n",
" bandkey2=bar\r\n",
" bk3=3.1415\r\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"src = gdal.Open('test.tif', gdalconst.GA_ReadOnly)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Description lost\n",
"src.GetDescription()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 5,
"text": [
"'test.tif'"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"src.GetMetadata()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 6,
"text": [
"{'key1': '1', 'key2': 'yada'}"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"band = src.GetRasterBand(1)\n",
"band.GetMetadata()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 7,
"text": [
"{'bandkey1': 'foo', 'bandkey2': 'bar', 'bk3': '3.1415'}"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment