Changeset 14800

Show
Ignore:
Timestamp:
07/02/08 16:19:49 (5 months ago)
Author:
rouault
Message:

Improve PAM metadata support with GeoTIFF in PROFILE=BASELINE/GeoTIFF mode (#2448)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/frmts/gtiff/geotiff.cpp

    r14796 r14800  
    202202 
    203203    // only needed by createcopy and close code. 
    204     static void           WriteMetadata( GDALDataset *, TIFF *, int, const char *, 
    205                                    const char *, char ** ); 
     204    static int            WriteMetadata( GDALDataset *, TIFF *, int, const char *, 
     205                                   const char *, char **, int bExcludeRPBandIMGFileWriting = FALSE ); 
    206206    static void     WriteNoDataValue( TIFF *, double ); 
    207207 
     
    29072907/************************************************************************/ 
    29082908 
    2909 void GTiffDataset::WriteMetadata( GDALDataset *poSrcDS, TIFF *hTIFF, 
     2909int GTiffDataset::WriteMetadata( GDALDataset *poSrcDS, TIFF *hTIFF, 
    29102910                                  int bSrcIsGeoTIFF, 
    29112911                                  const char *pszProfile, 
    29122912                                  const char *pszTIFFFilename, 
    2913                                   char **papszCreationOptions ) 
     2913                                  char **papszCreationOptions, 
     2914                                  int bExcludeRPBandIMGFileWriting) 
    29142915 
    29152916{ 
     
    29422943/* -------------------------------------------------------------------- */ 
    29432944    char **papszRPCMD = poSrcDS->GetMetadata("RPC"); 
    2944     if( papszRPCMD != NULL
     2945    if( papszRPCMD != NULL && !bExcludeRPBandIMGFileWriting
    29452946    { 
    29462947        if( EQUAL(pszProfile,"GDALGeoTIFF") ) 
     
    29582959/* -------------------------------------------------------------------- */ 
    29592960    char **papszIMDMD = poSrcDS->GetMetadata("IMD"); 
    2960     if( papszIMDMD != NULL
     2961    if( papszIMDMD != NULL && !bExcludeRPBandIMGFileWriting
    29612962    { 
    29622963        GDALWriteIMDFile( pszTIFFFilename, papszIMDMD ); 
     
    30133014    if( psRoot != NULL ) 
    30143015    { 
     3016        int bRet = TRUE; 
     3017 
    30153018        if( EQUAL(pszProfile,"GDALGeoTIFF") ) 
    30163019        { 
     
    30203023                if( bSrcIsGeoTIFF ) 
    30213024                    ((GTiffDataset *) poSrcDS)->PushMetadataToPam(); 
     3025                else 
     3026                    bRet = FALSE; 
    30223027                CPLError( CE_Warning, CPLE_AppDefined,  
    30233028                          "Lost metadata writing to GeoTIFF ... too large to fit in tag." ); 
     
    30333038            if( bSrcIsGeoTIFF ) 
    30343039                ((GTiffDataset *) poSrcDS)->PushMetadataToPam(); 
     3040            else 
     3041                bRet = FALSE; 
    30353042        } 
    30363043 
    30373044        CPLDestroyXMLNode( psRoot ); 
    3038     } 
     3045 
     3046        return bRet; 
     3047    } 
     3048 
     3049    return TRUE; 
    30393050} 
    30403051 
     
    30873098            papszMD = CSLDuplicate(papszMD); 
    30883099 
    3089             for( i = CSLCount(papszMD)-1; i > 0; i-- ) 
     3100            for( i = CSLCount(papszMD)-1; i >= 0; i-- ) 
    30903101            { 
    30913102                if( EQUALN(papszMD[i],"TIFFTAG_",8) 
     
    30963107 
    30973108            if( nBand == 0 ) 
    3098                 ((GDALPamDataset *) this)-> 
    3099                     SetMetadata( papszMD, papszDomainList[iDomain]); 
     3109                GDALPamDataset::SetMetadata( papszMD, papszDomainList[iDomain]); 
    31003110            else 
    3101                 ((GDALPamRasterBand *) poBand)-> 
    3102                     SetMetadata( papszMD, papszDomainList[iDomain]); 
     3111                poBand->GDALPamRasterBand::SetMetadata( papszMD, papszDomainList[iDomain]); 
    31033112 
    31043113            CSLDestroy( papszMD ); 
     
    31163125            if( bSuccess && (dfOffset != 0.0 || dfScale != 1.0) ) 
    31173126            { 
    3118                 ((GDALPamRasterBand *) poBand)->SetScale( dfScale ); 
    3119                 ((GDALPamRasterBand *) poBand)->SetOffset( dfOffset ); 
     3127                poBand->GDALPamRasterBand::SetScale( dfScale ); 
     3128                poBand->GDALPamRasterBand::SetOffset( dfOffset ); 
    31203129            } 
    31213130        } 
     
    34893498/* -------------------------------------------------------------------- */ 
    34903499    char **papszPamDomains = oMDMD.GetDomainList(); 
    3491     int i; 
    3492  
    3493     for( i = 0; papszPamDomains && papszPamDomains[i] != NULL; i++ ) 
    3494     { 
    3495         const char *pszDomain = papszPamDomains[i]; 
     3500 
     3501    for( int iDomain = 0; papszPamDomains && papszPamDomains[iDomain] != NULL; iDomain++ ) 
     3502    { 
     3503        const char *pszDomain = papszPamDomains[iDomain]; 
    34963504        char **papszGT_MD = oGTiffMDMD.GetMetadata( pszDomain ); 
    34973505        char **papszPAM_MD = CSLDuplicate(oMDMD.GetMetadata( pszDomain )); 
     
    35013509        oGTiffMDMD.SetMetadata( papszPAM_MD, pszDomain ); 
    35023510        CSLDestroy( papszPAM_MD ); 
     3511    } 
     3512 
     3513    for( int i = 1; i <= GetRasterCount(); i++) 
     3514    { 
     3515        GTiffRasterBand* poBand = (GTiffRasterBand *)GetRasterBand(i); 
     3516        papszPamDomains = poBand->oMDMD.GetDomainList(); 
     3517 
     3518        for( int iDomain = 0; papszPamDomains && papszPamDomains[iDomain] != NULL; iDomain++ ) 
     3519        { 
     3520            const char *pszDomain = papszPamDomains[iDomain]; 
     3521            char **papszGT_MD = poBand->oGTiffMDMD.GetMetadata( pszDomain ); 
     3522            char **papszPAM_MD = CSLDuplicate(poBand->oMDMD.GetMetadata( pszDomain )); 
     3523 
     3524            papszPAM_MD = CSLMerge( papszPAM_MD, papszGT_MD ); 
     3525 
     3526            poBand->oGTiffMDMD.SetMetadata( papszPAM_MD, pszDomain ); 
     3527            CSLDestroy( papszPAM_MD ); 
     3528        } 
    35033529    } 
    35043530} 
     
    51315157 
    51325158/* -------------------------------------------------------------------- */ 
    5133 /*      Transfer some TIFF specific metadata, if available.  Should     */ 
    5134 /*      we push this into .pam if we are avoiding GDAL tags?            */ 
    5135 /* -------------------------------------------------------------------- */ 
    5136     GTiffDataset::WriteMetadata( poSrcDS, hTIFF, FALSE, pszProfile, 
     5159/*      Transfer some TIFF specific metadata, if available.             */ 
     5160/*      The return value will tell us if we need to try again later with*/ 
     5161/*      PAM because the profile doesn't allow to write some metadata    */ 
     5162/*      as TIFF tag                                                     */ 
     5163/* -------------------------------------------------------------------- */ 
     5164    int bHasWrittenMDInGeotiffTAG = 
     5165            GTiffDataset::WriteMetadata( poSrcDS, hTIFF, FALSE, pszProfile, 
    51375166                                 pszFilename, papszOptions ); 
    51385167 
     
    53075336 
    53085337    poDS->osProfile = pszProfile; 
    5309     if( EQUAL(pszProfile,"GDALGeoTIFF") ) 
    5310     { 
    5311         poDS->CloneInfo( poSrcDS, GCIF_PAM_DEFAULT ); 
     5338    poDS->CloneInfo( poSrcDS, GCIF_PAM_DEFAULT ); 
     5339 
     5340/* -------------------------------------------------------------------- */ 
     5341/*      CloneInfo() doesn't merge metadata, it just replaces it totally */ 
     5342/*      So we have to merge it                                          */ 
     5343/* -------------------------------------------------------------------- */ 
     5344 
     5345    char **papszSRC_MD = poSrcDS->GetMetadata(); 
     5346    char **papszDST_MD = CSLDuplicate(poDS->GetMetadata()); 
     5347 
     5348    papszDST_MD = CSLMerge( papszDST_MD, papszSRC_MD ); 
     5349 
     5350    poDS->SetMetadata( papszDST_MD ); 
     5351    CSLDestroy( papszDST_MD ); 
     5352 
     5353    for( int nBand = 1; nBand <= poDS->GetRasterCount(); nBand++ ) 
     5354    { 
     5355        char **papszSRC_MD = poSrcDS->GetRasterBand(nBand)->GetMetadata(); 
     5356        char **papszDST_MD = CSLDuplicate(poDS->GetRasterBand(nBand)->GetMetadata()); 
     5357 
     5358        papszDST_MD = CSLMerge( papszDST_MD, papszSRC_MD ); 
     5359 
     5360        poDS->GetRasterBand(nBand)->SetMetadata( papszDST_MD ); 
     5361        CSLDestroy( papszDST_MD ); 
    53125362    } 
    53135363 
    53145364    hTIFF = (TIFF*) poDS->GetInternalHandle(NULL); 
     5365 
     5366/* -------------------------------------------------------------------- */ 
     5367/*      Second chance : now that we have a PAM dataset, it is possible  */ 
     5368/*      to write metadata that we couldn't be writen as TIFF tag        */ 
     5369/* -------------------------------------------------------------------- */ 
     5370    if (!bHasWrittenMDInGeotiffTAG) 
     5371        GTiffDataset::WriteMetadata( poDS, hTIFF, TRUE, pszProfile, 
     5372                                     pszFilename, papszOptions, TRUE /* don't write RPC and IMG file again */); 
    53155373 
    53165374    /* We must re-set the compression level at this point, since it has */