[gdal-dev] gdalinfo: fix metadata output

Christian Authmann authmanc at Mathematik.Uni-Marburg.de
Thu Jun 26 02:35:37 PDT 2014


Hello,

I tried getting a driver to return some useful metadata from the files, 
but gdalinfo never displayed any of it. As it turns out, the culprit was 
gdalinfo. It will never report metadata from the bands, even if 
explicitely requested in the command line.

So I set out to rewrite the metadata handling. It will now use the same 
function to display both dataset metadata and band metadata. While I was 
at it, I fixed a violation of the DRY-principle, introducing a small 
helper function to get a metadata list, loop over it and output everything.


Since there's probably someone out there with a script that parses the 
output of gdalinfo, I was careful not to introduce any functional 
changes in the output. The only visible difference should be that band 
metadata is actually printed.


Patch is attached for review and/or committing.


-- 
Christian Authmann
Philipps-Universität Marburg
Fachbereich Mathematik und Informatik
AG Datenbanksysteme
Hans-Meerwein-Straße
D-35032 Marburg
-------------- next part --------------
--- a/apps/gdalinfo.c	2014-06-26 13:22:16.980934455 +0200
+++ b/apps/gdalinfo.c	2014-06-25 15:38:11.273797119 +0200
@@ -44,6 +44,13 @@
                       const char * corner_name,
                       double x, double y );
 
+static void
+GDALInfoReportMetadata( GDALMajorObjectH hObject,
+                        int bListMDD,
+                        int bShowMetadata,
+                        char **papszExtraMDDomains,
+                        int bIsBand );
+
 /************************************************************************/
 /*                               Usage()                                */
 /************************************************************************/
@@ -78,10 +85,9 @@
     int			i, iBand;
     double		adfGeoTransform[6];
     GDALDriverH		hDriver;
-    char		**papszMetadata;
     int                 bComputeMinMax = FALSE, bSample = FALSE;
     int                 bShowGCPs = TRUE, bShowMetadata = TRUE, bShowRAT=TRUE;
-    int                 bStats = FALSE, bApproxStats = TRUE, iMDD;
+    int                 bStats = FALSE, bApproxStats = TRUE;
     int                 bShowColorTable = TRUE, bComputeChecksum = FALSE;
     int                 bReportHistograms = FALSE;
     int                 bReportProj4 = FALSE;
@@ -397,125 +403,8 @@
 /*      Report metadata.                                                */
 /* -------------------------------------------------------------------- */
 
-    if( bListMDD )
-    {
-        char** papszMDDList = GDALGetMetadataDomainList( hDataset );
-        char** papszIter = papszMDDList;
-
-        if( papszMDDList != NULL )
-            printf( "Metadata domains:\n" );
-        while( papszIter != NULL && *papszIter != NULL )
-        {
-            if( EQUAL(*papszIter, "") )
-                printf( "  (default)\n");
-            else
-                printf( "  %s\n", *papszIter );
-            papszIter ++;
-        }
-        CSLDestroy(papszMDDList);
-    }
-
-    papszMetadata = (bShowMetadata) ? GDALGetMetadata( hDataset, NULL ) : NULL;
-    if( bShowMetadata && CSLCount(papszMetadata) > 0 )
-    {
-        printf( "Metadata:\n" );
-        for( i = 0; papszMetadata[i] != NULL; i++ )
-        {
-            printf( "  %s\n", papszMetadata[i] );
-        }
-    }
-
-    if( papszExtraMDDomains != NULL && EQUAL(papszExtraMDDomains[0], "all") &&
-        papszExtraMDDomains[1] == NULL )
-    {
-        char** papszMDDList = GDALGetMetadataDomainList( hDataset );
-        char** papszIter = papszMDDList;
-
-        CSLDestroy(papszExtraMDDomains);
-        papszExtraMDDomains = NULL;
-
-        while( papszIter != NULL && *papszIter != NULL )
-        {
-            if( !EQUAL(*papszIter, "") &&
-                !EQUAL(*papszIter, "IMAGE_STRUCTURE") &&
-                !EQUAL(*papszIter, "SUBDATASETS") &&
-                !EQUAL(*papszIter, "GEOLOCATION") &&
-                !EQUAL(*papszIter, "RPC") )
-            {
-                papszExtraMDDomains = CSLAddString(papszExtraMDDomains, *papszIter);
-            }
-            papszIter ++;
-        }
-        CSLDestroy(papszMDDList);
-    }
-
-    for( iMDD = 0; bShowMetadata && iMDD < CSLCount(papszExtraMDDomains); iMDD++ )
-    {
-        papszMetadata = GDALGetMetadata( hDataset, papszExtraMDDomains[iMDD] );
-        if( CSLCount(papszMetadata) > 0 )
-        {
-            printf( "Metadata (%s):\n", papszExtraMDDomains[iMDD]);
-            for( i = 0; papszMetadata[i] != NULL; i++ )
-            {
-                if (EQUALN(papszExtraMDDomains[iMDD], "xml:", 4))
-                    printf( "%s\n", papszMetadata[i] );
-                else
-                    printf( "  %s\n", papszMetadata[i] );
-            }
-        }
-    }
-
-/* -------------------------------------------------------------------- */
-/*      Report "IMAGE_STRUCTURE" metadata.                              */
-/* -------------------------------------------------------------------- */
-    papszMetadata = (bShowMetadata) ? GDALGetMetadata( hDataset, "IMAGE_STRUCTURE" ) : NULL;
-    if( bShowMetadata && CSLCount(papszMetadata) > 0 )
-    {
-        printf( "Image Structure Metadata:\n" );
-        for( i = 0; papszMetadata[i] != NULL; i++ )
-        {
-            printf( "  %s\n", papszMetadata[i] );
-        }
-    }
-
-/* -------------------------------------------------------------------- */
-/*      Report subdatasets.                                             */
-/* -------------------------------------------------------------------- */
-    papszMetadata = GDALGetMetadata( hDataset, "SUBDATASETS" );
-    if( CSLCount(papszMetadata) > 0 )
-    {
-        printf( "Subdatasets:\n" );
-        for( i = 0; papszMetadata[i] != NULL; i++ )
-        {
-            printf( "  %s\n", papszMetadata[i] );
-        }
-    }
-
-/* -------------------------------------------------------------------- */
-/*      Report geolocation.                                             */
-/* -------------------------------------------------------------------- */
-    papszMetadata = (bShowMetadata) ? GDALGetMetadata( hDataset, "GEOLOCATION" ) : NULL;
-    if( bShowMetadata && CSLCount(papszMetadata) > 0 )
-    {
-        printf( "Geolocation:\n" );
-        for( i = 0; papszMetadata[i] != NULL; i++ )
-        {
-            printf( "  %s\n", papszMetadata[i] );
-        }
-    }
+    GDALInfoReportMetadata( hDataset, bListMDD, bShowMetadata, papszExtraMDDomains, FALSE);
 
-/* -------------------------------------------------------------------- */
-/*      Report RPCs                                                     */
-/* -------------------------------------------------------------------- */
-    papszMetadata = (bShowMetadata) ? GDALGetMetadata( hDataset, "RPC" ) : NULL;
-    if( bShowMetadata && CSLCount(papszMetadata) > 0 )
-    {
-        printf( "RPC Metadata:\n" );
-        for( i = 0; papszMetadata[i] != NULL; i++ )
-        {
-            printf( "  %s\n", papszMetadata[i] );
-        }
-    }
 
 /* -------------------------------------------------------------------- */
 /*      Setup projected to lat/long transform if appropriate.           */
@@ -796,42 +685,7 @@
                     GDALGetRasterOffset( hBand, &bSuccess ),
                     GDALGetRasterScale( hBand, &bSuccess ) );
 
-        if( bListMDD )
-        {
-            char** papszMDDList = GDALGetMetadataDomainList( hBand );
-            char** papszIter = papszMDDList;
-            if( papszMDDList != NULL )
-                printf( "  Metadata domains:\n" );
-            while( papszIter != NULL && *papszIter != NULL )
-            {
-                if( EQUAL(*papszIter, "") )
-                    printf( "    (default)\n");
-                else
-                    printf( "    %s\n", *papszIter );
-                papszIter ++;
-            }
-            CSLDestroy(papszMDDList);
-        }
-
-        papszMetadata = (bShowMetadata) ? GDALGetMetadata( hBand, NULL ) : NULL;
-        if( bShowMetadata && CSLCount(papszMetadata) > 0 )
-        {
-            printf( "  Metadata:\n" );
-            for( i = 0; papszMetadata[i] != NULL; i++ )
-            {
-                printf( "    %s\n", papszMetadata[i] );
-            }
-        }
-
-        papszMetadata = (bShowMetadata) ? GDALGetMetadata( hBand, "IMAGE_STRUCTURE" ) : NULL;
-        if( bShowMetadata && CSLCount(papszMetadata) > 0 )
-        {
-            printf( "  Image Structure Metadata:\n" );
-            for( i = 0; papszMetadata[i] != NULL; i++ )
-            {
-                printf( "    %s\n", papszMetadata[i] );
-            }
-        }
+        GDALInfoReportMetadata( hBand, bListMDD, bShowMetadata, papszExtraMDDomains, TRUE);
 
         if( GDALGetRasterColorInterpretation(hBand) == GCI_PaletteIndex 
             && (hTable = GDALGetRasterColorTable( hBand )) != NULL )
@@ -944,3 +798,126 @@
 
     return TRUE;
 }
+
+
+/************************************************************************/
+/*                       GDALInfoPrintMetadata()                        */
+/************************************************************************/
+static void GDALInfoPrintMetadata( GDALMajorObjectH hObject,
+                                   const char *pszDomain,
+                                   const char *pszDisplayedname )
+{
+    int i;
+    char **papszMetadata;
+    int bIsxml = FALSE;
+
+    if (pszDomain != NULL && EQUALN(pszDomain, "xml:", 4))
+        bIsxml = TRUE;
+
+    papszMetadata = GDALGetMetadata( hObject, pszDomain );
+    if( CSLCount(papszMetadata) > 0 )
+    {
+        printf( "%s:\n", pszDisplayedname );
+        for( i = 0; papszMetadata[i] != NULL; i++ )
+        {
+            if (bIsxml)
+                printf( "%s\n", papszMetadata[i] );
+            else
+                printf( "  %s\n", papszMetadata[i] );
+        }
+    }
+}
+
+/************************************************************************/
+/*                       GDALInfoReportMetadata()                       */
+/************************************************************************/
+static void GDALInfoReportMetadata( GDALMajorObjectH hObject,
+                                    int bListMDD,
+                                    int bShowMetadata,
+                                    char **papszExtraMDDomains,
+                                    int bIsBand )
+{
+    /* -------------------------------------------------------------------- */
+    /*      Report Metadata domains                                         */
+    /* -------------------------------------------------------------------- */
+    if( bListMDD )
+    {
+        char** papszMDDList = GDALGetMetadataDomainList( hObject );
+        char** papszIter = papszMDDList;
+
+        if( papszMDDList != NULL )
+            printf( "Metadata domains:\n" );
+        while( papszIter != NULL && *papszIter != NULL )
+        {
+            if( EQUAL(*papszIter, "") )
+                printf( "  (default)\n");
+            else
+                printf( "  %s\n", *papszIter );
+            papszIter ++;
+        }
+        CSLDestroy(papszMDDList);
+    }
+
+    if (!bShowMetadata)
+        return;
+
+    /* -------------------------------------------------------------------- */
+    /*      Report default Metadata                                         */
+    /* -------------------------------------------------------------------- */
+    GDALInfoPrintMetadata( hObject, NULL, "Metadata" );
+
+    /* -------------------------------------------------------------------- */
+    /*      Report extra Metadata                                           */
+    /* -------------------------------------------------------------------- */
+    if (papszExtraMDDomains != NULL) {
+        char **papszExtraMDDomainsExpanded = NULL;
+        int iMDD;
+
+        if( EQUAL(papszExtraMDDomains[0], "all") &&
+            papszExtraMDDomains[1] == NULL )
+        {
+            char** papszMDDList = GDALGetMetadataDomainList( hObject );
+            char** papszIter = papszMDDList;
+
+            while( papszIter != NULL && *papszIter != NULL )
+            {
+                if( !EQUAL(*papszIter, "") &&
+                    !EQUAL(*papszIter, "IMAGE_STRUCTURE") &&
+                    !EQUAL(*papszIter, "SUBDATASETS") &&
+                    !EQUAL(*papszIter, "GEOLOCATION") &&
+                    !EQUAL(*papszIter, "RPC") )
+                {
+                    papszExtraMDDomainsExpanded = CSLAddString(papszExtraMDDomainsExpanded, *papszIter);
+                }
+                papszIter ++;
+            }
+            CSLDestroy(papszMDDList);
+        }
+        else
+        {
+            papszExtraMDDomainsExpanded = CSLDuplicate(papszExtraMDDomains);
+        }
+
+        for( iMDD = 0; iMDD < CSLCount(papszExtraMDDomainsExpanded); iMDD++ )
+        {
+            char pszDisplayedname[256];
+            snprintf(pszDisplayedname, 256, "Metadata (%s)", papszExtraMDDomainsExpanded[iMDD]);
+            GDALInfoPrintMetadata( hObject, papszExtraMDDomainsExpanded[iMDD], pszDisplayedname );
+        }
+
+        CSLDestroy(papszExtraMDDomainsExpanded);
+    }
+
+    /* -------------------------------------------------------------------- */
+    /*      Report various named metadata.                                  */
+    /* -------------------------------------------------------------------- */
+    GDALInfoPrintMetadata( hObject, "IMAGE_STRUCTURE", "Image Structure Metadata" );
+
+    if (!bIsBand)
+    {
+    	GDALInfoPrintMetadata( hObject, "SUBDATASETS", "Subdatasets" );
+    	GDALInfoPrintMetadata( hObject, "GEOLOCATION", "Geolocation" );
+    	GDALInfoPrintMetadata( hObject, "RPC", "RPC Metadata" );
+    }
+
+}


More information about the gdal-dev mailing list