Changeset 14005

Show
Ignore:
Timestamp:
03/14/08 08:53:34 (4 months ago)
Author:
ilucena
Message:

Merged from trunk, no VSIMalloc2,3() and no GetFileList?()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/warmerdam/1.4-esri/gdal/frmts/idrisi/IdrisiDataset.cpp

    r11455 r14005  
    3434#include "ogr_spatialref.h" 
    3535#include "gdal_pam.h" 
     36#include "gdal_alg.h" 
    3637#include "gdal_rat.h" 
    37 #include "gdal_alg.h" 
    3838 
    3939CPL_CVSID( "$Id$" ); 
     
    114114#define rstREAL         "real" 
    115115#define rstRGB24        "rgb24" 
    116 #define rstDEGREE       "degrees
    117 #define rstMETER        "meters
     116#define rstDEGREE       "deg
     117#define rstMETER        "m
    118118#define rstLATLONG      "latlong" 
    119119#define rstPLANE        "plane" 
     
    198198struct ConvertionTab { 
    199199    const char *pszName; 
    200     int nDefault; 
     200    int nDefaultI; 
     201    int nDefaultG; 
    201202    double dfConv; 
    202203}; 
     
    204205//----- Linear Unit Conversion Table 
    205206static ConvertionTab aoLinearUnitsConv[] = { 
    206     {"Meters",      0,  1.0}, 
    207     {"Meter",       0,  1.0}, 
    208     {"Metre",       0,  1.0}, 
    209     {"M",           0,  1.0}, 
    210     {"Feet",        4,  0.3048}, 
    211     {"Foot",        4,  0.3048}, 
    212     {"Ft",          4,  0.3048}, 
    213     {"Miles",       7,  1612.9}, 
    214     {"Mi",          7,  1612.9}, 
    215     {"Kilometers",  9,  1000.0}, 
    216     {"Kilometer",   9,  1000.0},  
    217     {"Kilometre",   9,  1000.0}, 
    218     {"Km",          9,  1000.0} 
     207    {"m",            /*  0 */  0,   1,  1.0}, 
     208    {SRS_UL_METER,   /*  1 */  0,   1,  1.0}, 
     209    {"meters",       /*  2 */  0,   1,  1.0}, 
     210    {"metre",        /*  3 */  0,   1,  1.0}, 
     211 
     212    {"ft",           /*  4 */  4,   5,  0.3048}, 
     213    {SRS_UL_FOOT,    /*  5 */  4,   5,  0.3048}, 
     214    {"feet",         /*  6 */  4,   5,  0.3048}, 
     215    {"foot_us",      /*  7 */  4,   5,  0.3048006}, 
     216    {"u.s. foot",    /*  8 */  4,   5,  0.3048006}, 
     217 
     218    {"mi",           /*  9 */  9,  10,  1612.9}, 
     219    {"mile",         /* 10 */  9,  10,  1612.9}, 
     220    {"miles",        /* 11 */  9,  10,  1612.9}, 
     221 
     222    {"km",           /* 12 */ 12,  13,  1000.0}, 
     223    {"kilometers",   /* 13 */ 12,  13,  1000.0}, 
     224    {"kilometer",    /* 14 */ 12,  13,  1000.0}, 
     225    {"kilometre",    /* 15 */ 12,  13,  1000.0}, 
     226 
     227    {"deg",          /* 16 */ 16,  17,  0.0}, 
     228    {SRS_UA_DEGREE,  /* 17 */ 16,  17,  0.0}, 
     229    {"degrees",      /* 18 */ 16,  17,  0.0}, 
     230 
     231    {"rad",          /* 19 */ 19,  20,  0.0}, 
     232    {SRS_UA_RADIAN,  /* 20 */ 19,  20,  0.0}, 
     233    {"radians",      /* 21 */ 19,  20,  0.0} 
    219234}; 
    220235#define LINEAR_UNITS_COUNT (sizeof(aoLinearUnitsConv) / sizeof(ConvertionTab)) 
     
    224239 
    225240//----- Get the defaut name 
    226 const char *GetUnitDefault( const char *pszUnitName ); 
     241char *GetUnitDefault( const char *pszUnitName, const char *pszToMeter = NULL ); 
     242 
     243//----- Get the "to meter" 
     244int GetToMeterIndex( const char *pszToMeter ); 
     245 
     246//----- FormatCRLF 
     247void FormatCRLF( const char *pszFilename ); 
    227248 
    228249//----- Classes pre-definition: 
     
    255276 
    256277    CPLErr Wkt2GeoReference( const char *pszProjString, 
    257         const char **pszRefSystem,  
    258         const char **pszRefUnit ); 
     278        char **pszRefSystem,  
     279        char **pszRefUnit ); 
    259280 
    260281protected: 
     
    323344    virtual const GDALRasterAttributeTable *GetDefaultRAT(); 
    324345    virtual CPLErr SetDefaultRAT( const GDALRasterAttributeTable * ); 
     346 
     347    float  dfMaximum; 
     348    float  dfMinimum; 
     349    bool   bFirstVal; 
    325350}; 
    326351 
     
    363388    { 
    364389        if( eAccess == GA_Update ) 
    365         {             
     390        {        
     391            int i; 
     392 
     393            for( i = 0; i < nBands; i++ ) 
     394            { 
     395                IdrisiRasterBand *poBand = (IdrisiRasterBand*) GetRasterBand( i + 1 ); 
     396                poBand->SetStatistics( poBand->dfMinimum, poBand->dfMaximum, 0.0, 0.0 ); 
     397            } 
     398 
    366399            CSLSetNameValueSeparator( papszRDC, ": " ); 
    367400            CSLSave( papszRDC, pszDocFilename ); 
     401 
     402#ifndef WIN32         
     403            FormatCRLF( pszDocFilename ); 
     404#endif  
    368405        } 
    369406        CSLDestroy( papszRDC ); 
     
    371408 
    372409    if( poColorTable ) 
     410    { 
    373411        delete poColorTable; 
    374  
     412    } 
    375413    CPLFree( pszFilename ); 
    376414    CPLFree( pszDocFilename ); 
     
    463501        poDS->SetBand( 1, new IdrisiRasterBand( poDS, 1, GDT_Byte ) ); 
    464502    } 
    465     if( EQUAL( pszDataType, rstINTEGER ) ) 
     503    else if( EQUAL( pszDataType, rstINTEGER ) ) 
    466504    { 
    467505        poDS->nBands = 1; 
    468506        poDS->SetBand( 1, new IdrisiRasterBand( poDS, 1, GDT_Int16 ) ); 
    469507    } 
    470     if( EQUAL( pszDataType, rstREAL ) ) 
     508    else if( EQUAL( pszDataType, rstREAL ) ) 
    471509    { 
    472510        poDS->nBands = 1; 
    473511        poDS->SetBand( 1, new IdrisiRasterBand( poDS, 1, GDT_Float32 ) ); 
    474512    } 
    475     if( EQUAL( pszDataType, rstRGB24 ) ) 
     513    else if( EQUAL( pszDataType, rstRGB24 ) ) 
    476514    { 
    477515        poDS->nBands = 3; 
     
    479517        poDS->SetBand( 2, new IdrisiRasterBand( poDS, 2, GDT_Byte ) ); 
    480518        poDS->SetBand( 3, new IdrisiRasterBand( poDS, 3, GDT_Byte ) ); 
     519    } 
     520    else 
     521    { 
     522        CPLError(CE_Failure, CPLE_AppDefined, 
     523                 "Unknown data type : %s", pszDataType); 
     524        delete poDS; 
     525        return NULL; 
     526    } 
     527 
     528    int i; 
     529    for(i=0;i<poDS->nBands;i++) 
     530    { 
     531        IdrisiRasterBand* band = (IdrisiRasterBand*) poDS->GetRasterBand(i+1); 
     532        if (band->pabyScanLine == NULL) 
     533        { 
     534            delete poDS; 
     535            return NULL; 
     536        } 
    481537    } 
    482538 
     
    733789    CSLSetNameValueSeparator( papszLRDC, ": " ); 
    734790    CSLSave( papszLRDC, pszLDocFilename ); 
     791#ifndef WIN32         
     792    FormatCRLF( pszLDocFilename ); 
     793#endif  
    735794    CSLDestroy( papszLRDC ); 
    736795 
     
    750809    } 
    751810    VSIFCloseL( fp ); 
    752  
    753     // -------------------------------------------------------------------- 
    754     // Delete pre-existent .smp and .ref file with the same base name 
    755     // -------------------------------------------------------------------- 
    756  
    757     const char *pszPalletFName = CPLResetExtension( pszFilename, extSMP ); 
    758  
    759     if( FileExists( pszPalletFName ) ) 
    760     { 
    761         VSIUnlink( pszPalletFName ); 
    762     } 
    763  
    764     const char *pszRefereceFName = CPLResetExtension( pszFilename, extREF ); 
    765  
    766     if( FileExists( pszRefereceFName ) ) 
    767     { 
    768         VSIUnlink( pszRefereceFName ); 
    769     } 
    770811 
    771812    return (IdrisiDataset *) GDALOpen( pszFilename, GA_Update ); 
     
    814855        GDALDataType eType = poSrcDS->GetRasterBand( i )->GetRasterDataType(); 
    815856 
    816         if( eType != GDT_Byte &&  
    817             eType != GDT_Int16 &&  
    818             eType != GDT_UInt16 &&  
    819             eType != GDT_UInt32 &&  
    820             eType != GDT_Int32 &&  
    821             eType != GDT_Float32 && 
    822             eType != GDT_Float64 ) 
    823         { 
    824             CPLError( CE_Failure, CPLE_AppDefined, 
    825                 "Attempt to create IDRISI dataset with an illegal " 
    826                 "data type(%s).\n", 
    827                 GDALGetDataTypeName( eType ) ); 
    828             return NULL; 
     857        if( bStrict ) 
     858        { 
     859            if( eType != GDT_Byte &&  
     860                eType != GDT_Int16 &&  
     861                eType != GDT_Float32 ) 
     862            { 
     863                CPLError( CE_Failure, CPLE_AppDefined, 
     864                    "Attempt to create IDRISI dataset in strict mode " 
     865                    "with an illegal data type(%s).\n", 
     866                    GDALGetDataTypeName( eType ) ); 
     867                return NULL; 
     868            } 
     869        } 
     870        else 
     871        { 
     872            if( eType != GDT_Byte &&  
     873                eType != GDT_Int16 &&  
     874                eType != GDT_UInt16 &&  
     875                eType != GDT_UInt32 &&  
     876                eType != GDT_Int32 &&  
     877                eType != GDT_Float32 && 
     878                eType != GDT_Float64 ) 
     879            { 
     880                CPLError( CE_Failure, CPLE_AppDefined, 
     881                    "Attempt to create IDRISI dataset with an illegal " 
     882                    "data type(%s).\n", 
     883                    GDALGetDataTypeName( eType ) ); 
     884                return NULL; 
     885            } 
    829886        } 
    830887    } 
     
    837894    GDALDataType eType = poBand->GetRasterDataType(); 
    838895 
     896        int bSuccessMin = FALSE; 
     897        int bSuccessMax = FALSE; 
     898 
    839899    double dfMin; 
    840900    double dfMax; 
    841     double dfMean; 
    842     double dfStdDev; 
    843  
    844     poBand->GetStatistics( true, false, &dfMin, &dfMax, NULL, NULL ); 
     901 
     902    dfMin = poBand->GetMinimum( &bSuccessMin ); 
     903    dfMax = poBand->GetMaximum( &bSuccessMax ); 
     904 
     905    if( ! ( bSuccessMin && bSuccessMax ) ) 
     906    { 
     907            poBand->GetStatistics( false, false, &dfMin, &dfMax, NULL, NULL ); 
     908        } 
    845909 
    846910    if(!( ( eType == GDT_Byte ) ||  
     
    907971        if( poDS->nBands == 1 ) 
    908972        { 
    909             poBand->SetCategoryNames( poSrcBand->GetCategoryNames() ); 
    910             poBand->SetColorTable( poSrcBand->GetColorTable() ); 
    911973            poBand->SetUnitType( poSrcBand->GetUnitType() ); 
    912974 
    913             // ------------------------------------------------------------ 
    914             //      Copy Attribute Table information 
    915             // ------------------------------------------------------------ 
    916  
    917975            const GDALRasterAttributeTable *poRAT = poSrcBand->GetDefaultRAT(); 
     976 
    918977            if( poRAT != NULL ) 
    919978            { 
    920979                poBand->SetDefaultRAT( poRAT ); 
    921980            } 
    922         } 
    923  
    924         poSrcBand->GetStatistics( false, true, &dfMin, &dfMax, &dfMean, &dfStdDev ); 
    925         poBand->SetStatistics( dfMin, dfMax, dfMean, dfStdDev ); 
     981            else 
     982            { 
     983                poBand->SetCategoryNames( poSrcBand->GetCategoryNames() ); 
     984            } 
     985 
     986            if( poDS->poColorTable->GetColorEntryCount() == 0 ) 
     987            { 
     988                poBand->SetColorTable( poSrcBand->GetColorTable() ); 
     989            } 
     990 
     991        } 
     992 
     993                dfMin = poSrcBand->GetMinimum( NULL ); 
     994                dfMax = poSrcBand->GetMaximum( NULL ); 
     995                poBand->SetStatistics( dfMin, dfMax, 0.0, 0.0 ); 
    926996        dfNoDataValue = poSrcBand->GetNoDataValue( &bHasNoDataValue ); 
    927997        if( bHasNoDataValue ) 
     
    9391009    poDS->GetRasterBand( 1 )->GetBlockSize( &nBlockXSize, &nBlockYSize ); 
    9401010 
     1011    void *pData = VSIMalloc( nBlockXSize * nBlockYSize *  
     1012                              GDALGetDataTypeSize( eType ) / 8 ); 
     1013 
     1014    if( pData == NULL ) 
     1015    { 
     1016        CPLError( CE_Failure, CPLE_OutOfMemory, 
     1017                "IdrisiRasterBand::CreateCopy : Out of memory " ); 
     1018        delete poDS; 
     1019        return NULL; 
     1020    } 
     1021 
    9411022    for( int iBand = 1; iBand <= poSrcDS->GetRasterCount(); iBand++ ) 
    9421023    { 
     
    9451026 
    9461027        int    iYOffset, iXOffset; 
    947         void *pData; 
    9481028        CPLErr eErr = CE_None; 
    949  
    950         pData = CPLMalloc( nBlockXSize * nBlockYSize * GDALGetDataTypeSize( eType ) / 8 ); 
    9511029 
    9521030        for( iYOffset = 0; iYOffset < nYSize; iYOffset += nBlockYSize ) 
     
    9801058            } 
    9811059        } 
    982         CPLFree( pData ); 
    983     } 
     1060    } 
     1061 
     1062    CPLFree( pData ); 
    9841063 
    9851064    // -------------------------------------------------------------------- 
     
    10791158    CPLErr eResult = CE_None; 
    10801159 
    1081     const char *pszRefSystem
    1082     const char *pszRefUnit
     1160    char *pszRefSystem = NULL
     1161    char *pszRefUnit = NULL
    10831162 
    10841163    eResult = Wkt2GeoReference( pszProjString, &pszRefSystem, &pszRefUnit ); 
     
    10861165    CSLSetNameValue( papszRDC, rdcREF_SYSTEM, pszRefSystem ); 
    10871166    CSLSetNameValue( papszRDC, rdcREF_UNITS,  pszRefUnit ); 
     1167 
     1168    CPLFree( pszRefSystem ); 
     1169    CPLFree( pszRefUnit ); 
    10881170 
    10891171    return eResult; 
     
    11021184    this->eDataType = eDataType; 
    11031185    this->poDefaultRAT = NULL; 
     1186    this->dfMinimum = 0.0; 
     1187    this->dfMaximum = 0.0; 
     1188    this->bFirstVal = true; 
    11041189 
    11051190    // --------------------------------------------------------------------  
     
    11141199    // --------------------------------------------------------------------  
    11151200 
    1116     nRecordSize = poDS->GetRasterXSize() * GDALGetDataTypeSize( eDataType ) / 8 * poDS->nBands; 
    1117     pabyScanLine = ( GByte * ) CPLMalloc( nRecordSize ); 
     1201    nRecordSize  = poDS->GetRasterXSize() * GDALGetDataTypeSize( eDataType ) / 8; 
     1202 
     1203    pabyScanLine = (GByte*) VSIMalloc( nRecordSize * poDS->nBands ); 
     1204 
     1205    if( pabyScanLine == NULL ) 
     1206    { 
     1207        CPLError(CE_Failure, CPLE_OutOfMemory, 
     1208                 "IdrisiRasterBand::IdrisiRasterBand : Out of memory (nRasterXSize = %d)", 
     1209                  poDS->GetRasterXSize()); 
     1210    } 
     1211 
     1212    nRecordSize *= poDS->nBands; 
    11181213} 
    11191214 
     
    11371232 
    11381233CPLErr IdrisiRasterBand::IReadBlock( int nBlockXOff,  
    1139                                     int nBlockYOff, 
    1140                                     void *pImage ) 
     1234                                    int nBlockYOff, 
     1235                                    void *pImage ) 
    11411236{ 
    11421237    IdrisiDataset *poGDS = (IdrisiDataset *) poDS; 
     
    11841279 
    11851280CPLErr IdrisiRasterBand::IWriteBlock( int nBlockXOff,  
    1186                                      int nBlockYOff, 
    1187                                      void *pImage ) 
     1281                                      int nBlockYOff, 
     1282                                      void *pImage ) 
    11881283{ 
    11891284    IdrisiDataset *poGDS = (IdrisiDataset *) poDS; 
     
    12291324    } 
    12301325 
     1326    // --------------------------------------------------------------------  
     1327    //      Store the first value as mimunm and maximum 
     1328    // --------------------------------------------------------------------  
     1329 
     1330    if( bFirstVal ) 
     1331    { 
     1332        switch( eDataType ) 
     1333        { 
     1334            case GDT_Float32: 
     1335                dfMinimum = dfMaximum = (double) ((float*) pabyScanLine)[0]; 
     1336                break; 
     1337            case GDT_Int16: 
     1338                dfMinimum = dfMaximum = (double) ((GInt16*) pabyScanLine)[0]; 
     1339                break; 
     1340            default: 
     1341                dfMinimum = dfMaximum = (double) ((GByte*) pabyScanLine)[  
     1342                    poGDS->nBands == 1 ? 0 : 3 - this->nBand ]; 
     1343        } 
     1344        bFirstVal = false; 
     1345    } 
     1346 
     1347    // --------------------------------------------------------------------  
     1348    //      Search for the minimum and maximum values 
     1349    // --------------------------------------------------------------------  
     1350 
     1351    int i, j; 
     1352 
     1353    if( eDataType == GDT_Float32 ) 
     1354    { 
     1355        for( i = 0; i < nBlockXSize; i++ ) 
     1356        { 
     1357            dfMinimum = MIN( dfMinimum, ((float*) pabyScanLine)[i] ); 
     1358            dfMaximum = MAX( dfMaximum, ((float*) pabyScanLine)[i] ); 
     1359        } 
     1360    } 
     1361    else if( eDataType == GDT_Int16 ) 
     1362    { 
     1363        for( i = 0; i < nBlockXSize; i++ ) 
     1364        { 
     1365            dfMinimum = MIN( dfMinimum, ((GInt16*) pabyScanLine)[i] ); 
     1366            dfMaximum = MAX( dfMaximum, ((GInt16*) pabyScanLine)[i] ); 
     1367        } 
     1368    } 
     1369    else if( poGDS->nBands == 1 ) 
     1370    { 
     1371        for( i = 0; i < nBlockXSize; i++ ) 
     1372        { 
     1373            dfMinimum = MIN( dfMinimum, ((GByte*) pabyScanLine)[i] ); 
     1374            dfMaximum = MAX( dfMaximum, ((GByte*) pabyScanLine)[i] ); 
     1375        } 
     1376    } 
     1377    else 
     1378    { 
     1379        for( i = 0, j = ( 3 - nBand ); i < nBlockXSize; i++, j += 3 ) 
     1380        { 
     1381            dfMinimum = MIN( dfMinimum, ((GByte*) pabyScanLine)[j] ); 
     1382            dfMaximum = MAX( dfMaximum, ((GByte*) pabyScanLine)[j] ); 
     1383        } 
     1384    } 
     1385 
    12311386    return CE_None; 
    12321387} 
     
    12451400 
    12461401    if( pbSuccess ) 
    1247         *pbSuccess = TRUE; 
     1402        { 
     1403        *pbSuccess = true; 
     1404        } 
    12481405 
    12491406    return adfMinValue[this->nBand - 1]; 
     
    12631420 
    12641421    if( pbSuccess ) 
    1265         *pbSuccess = TRUE; 
     1422        { 
     1423        *pbSuccess = true; 
     1424        } 
    12661425 
    12671426    return adfMaxValue[this->nBand - 1]; 
     
    12941453    { 
    12951454        dfNoData = atof_nz( CSLFetchNameValue( poGDS->papszRDC, rdcFLAG_VALUE ) ); 
    1296         *pbSuccess = TRUE; 
     1455        if( pbSuccess ) 
     1456            *pbSuccess = TRUE; 
    12971457    } 
    12981458    else 
    12991459    { 
    13001460        dfNoData = -9999.0;    /* this value should be ignored */ 
    1301         *pbSuccess = FALSE; 
     1461        if( pbSuccess ) 
     1462            *pbSuccess = FALSE; 
    13021463    } 
    13031464 
     
    15741735CPLErr IdrisiRasterBand::SetDefaultRAT( const GDALRasterAttributeTable *poRAT ) 
    15751736{ 
     1737    if( ! poRAT ) 
     1738    { 
     1739        return CE_Failure; 
     1740    } 
     1741 
    15761742    // ---------------------------------------------------------- 
    1577     // Update Bands Attribute Table 
     1743    // Get field indecies 
    15781744    // ---------------------------------------------------------- 
    15791745 
     1746    int iValue = -1; 
     1747    int iRed   = poRAT->GetColOfUsage( GFU_Red ); 
     1748    int iGreen = poRAT->GetColOfUsage( GFU_Green ); 
     1749    int iBlue  = poRAT->GetColOfUsage( GFU_Blue ); 
     1750    int iName  = poRAT->GetColOfUsage( GFU_Name ); 
     1751    int i; 
     1752 
     1753    // ---------------------------------------------------------- 
     1754    // Seek for "Value" field index (AGIS standards field name) 
     1755    // ---------------------------------------------------------- 
     1756 
     1757    if( iValue == -1 ) 
     1758    { 
     1759        for( i = 0; i < poRAT->GetColumnCount(); i++ ) 
     1760        { 
     1761            if EQUALN( "Value", poRAT->GetNameOfCol( i ), 5 )  
     1762            { 
     1763                iValue = i; 
     1764                break; 
     1765            } 
     1766        } 
     1767    } 
     1768 
     1769    /* if still can't find it use the first Integer column */ 
     1770 
     1771    if( iValue == -1 ) 
     1772    {    
     1773        for( i = 1; i < poRAT->GetColumnCount(); i++ ) 
     1774        { 
     1775            if( poRAT->GetTypeOfCol( i ) == GFT_Integer ) 
     1776            { 
     1777                iValue = i; 
     1778                break; 
     1779            } 
     1780        } 
     1781    } 
     1782 
     1783    // ---------------------------------------------------------- 
     1784    // Seek for Name field index 
     1785    // ---------------------------------------------------------- 
     1786 
     1787    if( iName == -1 ) 
     1788    { 
     1789        for( i = 0; i < poRAT->GetColumnCount(); i++ ) 
     1790        { 
     1791            if EQUALN( "Class_Name", poRAT->GetNameOfCol( i ), 10 ) 
     1792            { 
     1793                iName = i; 
     1794                break; 
     1795            }  
     1796            else if EQUALN( "Categor", poRAT->GetNameOfCol( i ), 7 ) 
     1797            { 
     1798                iName = i; 
     1799                break; 
     1800            }  
     1801            else if EQUALN( "Name",  poRAT->GetNameOfCol( i ), 4 ) 
     1802            { 
     1803                iName = i; 
     1804                break; 
     1805            } 
     1806        } 
     1807    } 
     1808 
     1809    /* if still can't find it use the first String column */ 
     1810 
     1811    if( iName == -1 ) 
     1812    {    
     1813        for( i = 0; i < poRAT->GetColumnCount(); i++ ) 
     1814        { 
     1815            if( poRAT->GetTypeOfCol( i ) == GFT_String ) 
     1816            { 
     1817                iName = i; 
     1818                break; 
     1819            } 
     1820        } 
     1821    } 
     1822 
     1823    // ---------------------------------------------------------- 
     1824    // Incomplete Attribute Table; 
     1825    // ---------------------------------------------------------- 
     1826 
     1827    if( iName == -1 ) 
     1828    { 
     1829        iName = iValue; 
     1830    } 
     1831 
     1832    // ---------------------------------------------------------- 
     1833    // Initialization 
     1834    // ---------------------------------------------------------- 
     1835 
     1836    GDALColorTable *poCT = NULL; 
     1837    char **papszNames = NULL; 
     1838 
     1839        double dRed     = 0.0; 
     1840        double dGreen   = 0.0; 
     1841        double dBlue    = 0.0; 
     1842    int nFact       = 1; 
     1843 
     1844    if( iRed != -1 && iGreen != -1 && iBlue != -1 ) 
     1845    { 
     1846        poCT  = new GDALColorTable(); 
     1847        nFact = poRAT->GetTypeOfCol( iRed ) == GFT_Real ? 255 : 1; 
     1848    } 
     1849 
     1850    // ---------------------------------------------------------- 
     1851    // Load values 
     1852    // ---------------------------------------------------------- 
     1853 
     1854    GDALColorEntry  sColor; 
     1855    int iEntry      = 0; 
     1856    int iOut        = 0; 
     1857    int nEntryCount = poRAT->GetRowCount(); 
     1858    int nValue      = poRAT->GetValueAsInt( iEntry, iValue ); 
     1859 
     1860    for( iOut = 0; iOut < 65535 && ( iEntry < nEntryCount ); iOut++ ) 
     1861    { 
     1862        if( iOut == nValue ) 
     1863        { 
     1864            if( poCT ) 
     1865            { 
     1866                            dRed    = poRAT->GetValueAsDouble( iEntry, iRed ); 
     1867                            dGreen  = poRAT->GetValueAsDouble( iEntry, iGreen ); 
     1868                            dBlue   = poRAT->GetValueAsDouble( iEntry, iBlue ); 
     1869                sColor.c1  = (short) ( dRed   * nFact ); 
     1870                sColor.c2  = (short) ( dGreen * nFact ); 
     1871                sColor.c3  = (short) ( dBlue  * nFact ); 
     1872                sColor.c4  = (short) ( 255    / nFact );     
     1873                poCT->SetColorEntry( iEntry, &sColor ); 
     1874            } 
     1875            papszNames = CSLAddString( papszNames,  
     1876                poRAT->GetValueAsString( iEntry, iName ) ); 
     1877 
     1878            /* Advance on the table */ 
     1879 
     1880            if( ( ++iEntry ) < nEntryCount ) 
     1881            { 
     1882                nValue = poRAT->GetValueAsInt( iEntry, iValue ); 
     1883            } 
     1884        } 
     1885        else if( iOut < nValue ) 
     1886        { 
     1887            if( poCT ) 
     1888            { 
     1889                sColor.c1  = (short) 0; 
     1890                sColor.c2  = (short) 0; 
     1891                sColor.c3  = (short) 0; 
     1892                sColor.c4  = (short) 255;     
     1893                poCT->SetColorEntry( iEntry, &sColor ); 
     1894            } 
     1895                papszNames = CSLAddString( papszNames, "" ); 
     1896        } 
     1897        } 
     1898 
     1899    // ---------------------------------------------------------- 
     1900    // Set Color Table 
     1901    // ---------------------------------------------------------- 
     1902 
     1903    if( poCT ) 
     1904    { 
     1905        SetColorTable( poCT ); 
     1906        delete poCT; 
     1907    } 
     1908 
     1909    // ---------------------------------------------------------- 
     1910    // Update Category Names 
     1911    // ---------------------------------------------------------- 
     1912 
     1913    SetCategoryNames( papszNames ); 
     1914    CSLDestroy( papszNames ); 
     1915 
     1916    // ---------------------------------------------------------- 
     1917    // Update Attribute Table 
     1918    // ---------------------------------------------------------- 
     1919 
    15801920    if( poDefaultRAT )  
    15811921    { 
     
    15841924 
    15851925    poDefaultRAT = poRAT->Clone(); 
    1586  
    1587     int nEntryCount = poDefaultRAT->GetRowCount(); 
    1588  
    1589     // ---------------------------------------------------------- 
    1590     // Get the Color Table from the Attribute Table 
    1591     // ---------------------------------------------------------- 
    1592  
    1593     int iRed   = poDefaultRAT->GetColOfUsage( GFU_Red ); 
    1594     int iGreen = poDefaultRAT->GetColOfUsage( GFU_Green ); 
    1595     int iBlue  = poDefaultRAT->GetColOfUsage( GFU_Blue ); 
    1596  
    1597     if( iRed != -1 && iGreen != -1 && iBlue != -1 ) 
    1598     { 
    1599         GDALColorTable *poCT = new GDALColorTable(); 
    1600         int nEntryCount = poDefaultRAT->GetRowCount(); 
    1601         int iEntry = 0; 
    1602  
    1603         double dRed   = 0.0;    
    1604         double dGreen = 0.0; 
    1605         double dBlue  = 0.0; 
    1606  
    1607         int nFact = poDefaultRAT->GetTypeOfCol( iRed ) == GFT_Real ? 255 : 1; 
    1608  
    1609         for( iEntry = 0; iEntry < nEntryCount; iEntry++ ) 
    1610         { 
    1611             GDALColorEntry sColor; 
    1612             int iRow = poDefaultRAT->GetRowOfValue( iEntry ); 
    1613  
    1614             if( iRow == -1 ) 
    1615             { 
    1616                 iRow = iEntry; // This decision is questionable. 
    1617             } 
    1618  
    1619             dRed   = poDefaultRAT->GetValueAsDouble( iRow, iRed ); 
    1620             dGreen = poDefaultRAT->GetValueAsDouble( iRow, iGreen ); 
    1621             dBlue  = poDefaultRAT->GetValueAsDouble( iRow, iBlue ); 
    1622  
    1623             sColor.c1 = (short) ( dRed   * nFact ); 
    1624             sColor.c2 = (short) ( dGreen * nFact ); 
    1625             sColor.c3 = (short) ( dBlue  * nFact ); 
    1626             sColor.c4 = (short) ( 255    / nFact ); 
    1627              
    1628             poCT->SetColorEntry( iEntry, &sColor ); 
    1629         } 
    1630  
    1631         // ------------------------------------------------------ 
    1632         // Set Color Table 
    1633         // ------------------------------------------------------ 
    1634  
    1635         SetColorTable(poCT); 
    1636         delete poCT; 
    1637     } 
    1638  
    1639     // ---------------------------------------------------------- 
    1640     // Get the Catagory Names from the Attribute Table 
    1641     // ---------------------------------------------------------- 
    1642  
    1643     char **papszCategoryNames = NULL; 
    1644  
    1645     int iNameCol = poDefaultRAT->GetColOfUsage( GFU_Name ); 
    1646  
    1647     if( iNameCol != -1 ) 
    1648     { 
    1649         int iEntry = 0; 
    1650         int iRow = 0; 
    1651  
    1652         for( iEntry = 0; iEntry < nEntryCount; iEntry++ ) 
    1653         { 
    1654             iRow = poDefaultRAT->GetRowOfValue( iEntry ); 
    1655  
    1656             if( iRow == -1 ) 
    1657             { 
    1658                 iRow = iEntry; // This decision is questionable. 
    1659             } 
    1660  
    1661             papszCategoryNames = CSLAddString( papszCategoryNames,  
    1662                 poDefaultRAT->GetValueAsString( iRow, iNameCol ) ); 
    1663         } 
    1664     } 
    1665  
    1666     // ---------------------------------------------------------- 
    1667     // Update Header File 
    1668     // ---------------------------------------------------------- 
    1669  
    1670     SetCategoryNames( papszCategoryNames ); 
    1671     CSLDestroy( papszCategoryNames ); 
    16721926 
    16731927    return CE_None; 
     
    16821936    IdrisiDataset *poGDS = (IdrisiDataset *) poDS; 
    16831937 
    1684     if( poGDS->papszCategories == NULL &&  
    1685       ( poGDS->poColorTable->GetColorEntryCount() == 0 ) ) 
     1938    if( poGDS->papszCategories == NULL ) 
    16861939    { 
    16871940        return NULL; 
    16881941    } 
     1942 
     1943    bool bHasColorTable = poGDS->poColorTable->GetColorEntryCount() > 0; 
    16891944 
    16901945    // ---------------------------------------------------------- 
     
    17001955 
    17011956    // ---------------------------------------------------------- 
    1702     // Set the Color Table to the Attribute Table 
     1957    // Create (Value, Red, Green, Blue, Alpha, Class_Name) fields 
    17031958    // ---------------------------------------------------------- 
    17041959 
    1705     poDefaultRAT->InitializeFromColorTable(poGDS->poColorTable); 
     1960    poDefaultRAT->CreateColumn( "Value",      GFT_Integer, GFU_Generic ); 
     1961    poDefaultRAT->CreateColumn( "Value_1",    GFT_Integer, GFU_MinMax ); 
     1962 
     1963    if( bHasColorTable ) 
     1964    { 
     1965        poDefaultRAT->CreateColumn( "Red",        GFT_Integer, GFU_Red ); 
     1966        poDefaultRAT->CreateColumn( "Green",  GFT_Integer, GFU_Green ); 
     1967        poDefaultRAT->CreateColumn( "Blue",   GFT_Integer, GFU_Blue ); 
     1968        poDefaultRAT->CreateColumn( "Alpha",  GFT_Integer, GFU_Alpha ); 
     1969    } 
     1970    poDefaultRAT->CreateColumn( "Class_name", GFT_String,  GFU_Name ); 
    17061971 
    17071972    // ---------------------------------------------------------- 
    1708     // Set the Catagory Names to the Attribute Table 
     1973    // Loop throught the Category Names 
    17091974    // ---------------------------------------------------------- 
    17101975 
    1711     poDefaultRAT->CreateColumn( "Class_Names", GFT_String, GFU_Name ); 
    1712  
    1713     int iNameCol = poDefaultRAT->GetColOfUsage( GFU_Name ); 
    1714  
    1715     int nEntryCount = CSLCount(poGDS->papszCategories); 
     1976    GDALColorEntry sEntry; 
     1977    int iName = poDefaultRAT->GetColOfUsage( GFU_Name ); 
     1978    int nEntryCount = CSLCount( poGDS->papszCategories ); 
    17161979    int iEntry = 0; 
    1717     int iRow = 0; 
     1980    int iRows = 0; 
    17181981 
    17191982    for( iEntry = 0; iEntry < nEntryCount; iEntry++ ) 
    17201983    { 
    1721         iRow = poDefaultRAT->GetRowOfValue( iEntry ); 
    1722  
    1723         if( iRow == -1 ) 
    1724         { 
    1725             iRow = iEntry; // This decision is questionable. 
    1726         } 
    1727         
    1728         poDefaultRAT->SetValue( iRow, iNameCol,  
    1729             poGDS->papszCategories[iEntry] ); 
    1730     } 
    1731  
     1984        if EQUAL( poGDS->papszCategories[iEntry], "" ) 
     1985        { 
     1986            continue; // Eliminate the empty ones 
     1987        } 
     1988        poDefaultRAT->SetRowCount( poDefaultRAT->GetRowCount() + 1 ); 
     1989        poDefaultRAT->SetValue( iRows, 0, iEntry ); 
     1990        poDefaultRAT->SetValue( iRows, 1, iEntry ); 
     1991        if( bHasColorTable ) 
     1992        { 
     1993            poGDS->poColorTable->GetColorEntryAsRGB( iEntry, &sEntry ); 
     1994            poDefaultRAT->SetValue( iRows, 2, sEntry.c1 ); 
     1995            poDefaultRAT->SetValue( iRows, 3, sEntry.c2 ); 
     1996            poDefaultRAT->SetValue( iRows, 4, sEntry.c3 ); 
     1997            poDefaultRAT->SetValue( iRows, 5, sEntry.c4 ); 
     1998        } 
     1999        poDefaultRAT->SetValue( iRows, iName, poGDS->papszCategories[iEntry] ); 
     2000        iRows++; 
     2001    } 
     2002         
    17322003    return poDefaultRAT; 
    17332004} 
     
    17662037 
    17672038CPLErr IdrisiDataset::GeoReference2Wkt( const char *pszRefSystem, 
    1768                                        const char *pszRefUnits, 
    1769                                        char **pszProjString ) 
     2039                                        const char *pszRefUnits, 
     2040                                        char **pszProjString ) 
    17702041{ 
    17712042    OGRSpatialReference oSRS; 
     
    17782049    { 
    17792050        oSRS.SetLocalCS( "Plane" ); 
    1780         int nUnit = GetUnitIndex( GetUnitDefault( pszRefUnits ) ); 
     2051        int nUnit = GetUnitIndex( pszRefUnits ); 
    17812052        if( nUnit > -1 ) 
    17822053        { 
    1783             oSRS.SetLinearUnits( aoLinearUnitsConv[nUnit].pszName, 
    1784                 aoLinearUnitsConv[nUnit].dfConv ); 
     2054            int nDeft = aoLinearUnitsConv[nUnit].nDefaultG; 
     2055            oSRS.SetLinearUnits( aoLinearUnitsConv[nDeft].pszName, 
     2056                aoLinearUnitsConv[nDeft].dfConv ); 
    17852057        } 
    17862058        oSRS.exportToWkt( pszProjString ); 
     
    18372109            nZone = ( nZone == 1 ? nSPCode : nSPCode + nZone - 1 ); 
    18382110 
    1839             if( oSRS.SetStatePlane( nZone,( nNAD == 83 ) ) != OGRERR_FAILURE ) 
     2111            if( oSRS.SetStatePlane( nZone, ( nNAD == 83 ) ) != OGRERR_FAILURE ) 
    18402112            { 
    18412113                oSRS.exportToWkt( pszProjString ); 
     
    18852157        { 
    18862158            oSRS.SetLocalCS( "Unknown" ); 
    1887             int nUnit = GetUnitIndex( GetUnitDefault( pszRefUnits ) ); 
     2159            int nUnit = GetUnitIndex( pszRefUnits ); 
    18882160            if( nUnit > -1 ) 
    18892161            { 
    1890                 oSRS.SetLinearUnits( aoLinearUnitsConv[nUnit].pszName, 
    1891                     aoLinearUnitsConv[nUnit].dfConv ); 
     2162                int nDeft = aoLinearUnitsConv[nUnit].nDefaultG; 
     2163                oSRS.SetLinearUnits( aoLinearUnitsConv[nDeft].pszName, 
     2164                    aoLinearUnitsConv[nDeft].dfConv ); 
    18922165            } 
    18932166        } 
     
    19032176    CSLSetNameValueSeparator( papszRef, ":" ); 
    19042177 
    1905     const char *pszGeorefName   = CPLStrdup( CSLFetchNameValue( papszRef, refREF_SYSTEM ) ); 
    1906     if EQUAL( pszGeorefName, "" )  
    1907         pszGeorefName   = CPLStrdup( CSLFetchNameValue( papszRef, refREF_SYSTEM2 ) ); 
    1908     const char *pszProjName     = CPLStrdup( CSLFetchNameValue( papszRef, refPROJECTION ) ); 
    1909     const char *pszDatum        = CPLStrdup( CSLFetchNameValue( papszRef, refDATUM ) ); 
    1910     const char *pszEllipsoid    = CPLStrdup( CSLFetchNameValue( papszRef, refELLIPSOID ) ); 
     2178    char *pszGeorefName; 
     2179     
     2180    if( EQUAL( CSLFetchNameValue( papszRef, refREF_SYSTEM ), "" ) == FALSE ) 
     2181    { 
     2182        pszGeorefName           = CPLStrdup( CSLFetchNameValue( papszRef, refREF_SYSTEM ) ); 
     2183    } 
     2184    else 
     2185    { 
     2186        pszGeorefName           = CPLStrdup( CSLFetchNameValue( papszRef, refREF_SYSTEM2 ) ); 
     2187    } 
     2188    char *pszProjName           = CPLStrdup( CSLFetchNameValue( papszRef, refPROJECTION ) ); 
     2189    char *pszDatum              = CPLStrdup( CSLFetchNameValue( papszRef, refDATUM ) ); 
     2190    char *pszEllipsoid          = CPLStrdup( CSLFetchNameValue( papszRef, refELLIPSOID ) ); 
    19112191    double dfCenterLat          = atof_nz( CSLFetchNameValue( papszRef, refORIGIN_LAT ) ); 
    19122192    double dfCenterLong         = atof_nz( CSLFetchNameValue( papszRef, refORIGIN_LONG ) ); 
     
    20002280    { 
    20012281        oSRS.exportToWkt( pszProjString ); 
     2282         
     2283        CPLFree( pszGeorefName ); 
     2284        CPLFree( pszProjName ); 
     2285        CPLFree( pszDatum ); 
     2286        CPLFree( pszEllipsoid ); 
     2287         
    20022288        return CE_None; 
    20032289    } 
     
    20502336    { 
    20512337        oSRS.SetProjection( pszProjName ); 
    2052         oSRS.SetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN,  dfCenterLat ); 
    2053         oSRS.SetNormProjParm( SRS_PP_CENTRAL_MERIDIAN,    dfCenterLong ); 
    2054         oSRS.SetNormProjParm( SRS_PP_FALSE_EASTING,       dfFalseEasting ); 
    2055         oSRS.SetNormProjParm( SRS_PP_FALSE_NORTHING,      dfFalseNorthing ); 
     2338        oSRS.SetProjParm( SRS_PP_LATITUDE_OF_ORIGIN,  dfCenterLat ); 
     2339        oSRS.SetProjParm( SRS_PP_CENTRAL_MERIDIAN,    dfCenterLong ); 
     2340        oSRS.SetProjParm( SRS_PP_FALSE_EASTING,       dfFalseEasting ); 
     2341        oSRS.SetProjParm( SRS_PP_FALSE_NORTHING,      dfFalseNorthing ); 
    20562342    } 
    20572343    else if( EQUALN( pszProjName, "Lambert North Polar Azimuthal Equal Area", 15 ) || 
     
    20922378        oSRS.Clear(); 
    20932379        oSRS.exportToWkt( pszProjString ); 
     2380         
     2381        CPLFree( pszGeorefName ); 
     2382        CPLFree( pszProjName ); 
     2383        CPLFree( pszDatum ); 
     2384        CPLFree( pszEllipsoid ); 
     2385         
    20942386        return CE_Warning; 
    20952387    } 
     
    20992391    // ---------------------------------------------------------------------- 
    21002392 
    2101     int nUnit = GetUnitIndex( GetUnitDefault( pszRefUnits ) ); 
     2393    int nUnit = GetUnitIndex( pszRefUnits ); 
    21022394    if( nUnit > -1 ) 
    21032395    { 
    2104         oSRS.SetLinearUnits( aoLinearUnitsConv[nUnit].pszName, 
    2105             aoLinearUnitsConv[nUnit].dfConv ); 
     2396        int nDeft = aoLinearUnitsConv[nUnit].nDefaultG; 
     2397        oSRS.SetLinearUnits( aoLinearUnitsConv[nDeft].pszName, 
     2398            aoLinearUnitsConv[nDeft].dfConv ); 
     2399    } 
     2400    else 
     2401    { 
     2402        oSRS.SetLinearUnits( "unknown",  1.0 ); 
    21062403    } 
    21072404 
     
    21132410 
    21142411    oSRS.exportToWkt( pszProjString ); 
     2412         
     2413    CPLFree( pszGeorefName ); 
     2414    CPLFree( pszProjName ); 
     2415    CPLFree( pszDatum ); 
     2416    CPLFree( pszEllipsoid );