Changeset 14005
- Timestamp:
- 03/14/08 08:53:34 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/warmerdam/1.4-esri/gdal/frmts/idrisi/IdrisiDataset.cpp
r11455 r14005 34 34 #include "ogr_spatialref.h" 35 35 #include "gdal_pam.h" 36 #include "gdal_alg.h" 36 37 #include "gdal_rat.h" 37 #include "gdal_alg.h"38 38 39 39 CPL_CVSID( "$Id$" ); … … 114 114 #define rstREAL "real" 115 115 #define rstRGB24 "rgb24" 116 #define rstDEGREE "deg rees"117 #define rstMETER "m eters"116 #define rstDEGREE "deg" 117 #define rstMETER "m" 118 118 #define rstLATLONG "latlong" 119 119 #define rstPLANE "plane" … … 198 198 struct ConvertionTab { 199 199 const char *pszName; 200 int nDefault; 200 int nDefaultI; 201 int nDefaultG; 201 202 double dfConv; 202 203 }; … … 204 205 //----- Linear Unit Conversion Table 205 206 static 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} 219 234 }; 220 235 #define LINEAR_UNITS_COUNT (sizeof(aoLinearUnitsConv) / sizeof(ConvertionTab)) … … 224 239 225 240 //----- Get the defaut name 226 const char *GetUnitDefault( const char *pszUnitName ); 241 char *GetUnitDefault( const char *pszUnitName, const char *pszToMeter = NULL ); 242 243 //----- Get the "to meter" 244 int GetToMeterIndex( const char *pszToMeter ); 245 246 //----- FormatCRLF 247 void FormatCRLF( const char *pszFilename ); 227 248 228 249 //----- Classes pre-definition: … … 255 276 256 277 CPLErr Wkt2GeoReference( const char *pszProjString, 257 c onst char **pszRefSystem,258 c onst char **pszRefUnit );278 char **pszRefSystem, 279 char **pszRefUnit ); 259 280 260 281 protected: … … 323 344 virtual const GDALRasterAttributeTable *GetDefaultRAT(); 324 345 virtual CPLErr SetDefaultRAT( const GDALRasterAttributeTable * ); 346 347 float dfMaximum; 348 float dfMinimum; 349 bool bFirstVal; 325 350 }; 326 351 … … 363 388 { 364 389 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 366 399 CSLSetNameValueSeparator( papszRDC, ": " ); 367 400 CSLSave( papszRDC, pszDocFilename ); 401 402 #ifndef WIN32 403 FormatCRLF( pszDocFilename ); 404 #endif 368 405 } 369 406 CSLDestroy( papszRDC ); … … 371 408 372 409 if( poColorTable ) 410 { 373 411 delete poColorTable; 374 412 } 375 413 CPLFree( pszFilename ); 376 414 CPLFree( pszDocFilename ); … … 463 501 poDS->SetBand( 1, new IdrisiRasterBand( poDS, 1, GDT_Byte ) ); 464 502 } 465 if( EQUAL( pszDataType, rstINTEGER ) )503 else if( EQUAL( pszDataType, rstINTEGER ) ) 466 504 { 467 505 poDS->nBands = 1; 468 506 poDS->SetBand( 1, new IdrisiRasterBand( poDS, 1, GDT_Int16 ) ); 469 507 } 470 if( EQUAL( pszDataType, rstREAL ) )508 else if( EQUAL( pszDataType, rstREAL ) ) 471 509 { 472 510 poDS->nBands = 1; 473 511 poDS->SetBand( 1, new IdrisiRasterBand( poDS, 1, GDT_Float32 ) ); 474 512 } 475 if( EQUAL( pszDataType, rstRGB24 ) )513 else if( EQUAL( pszDataType, rstRGB24 ) ) 476 514 { 477 515 poDS->nBands = 3; … … 479 517 poDS->SetBand( 2, new IdrisiRasterBand( poDS, 2, GDT_Byte ) ); 480 518 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 } 481 537 } 482 538 … … 733 789 CSLSetNameValueSeparator( papszLRDC, ": " ); 734 790 CSLSave( papszLRDC, pszLDocFilename ); 791 #ifndef WIN32 792 FormatCRLF( pszLDocFilename ); 793 #endif 735 794 CSLDestroy( papszLRDC ); 736 795 … … 750 809 } 751 810 VSIFCloseL( fp ); 752 753 // --------------------------------------------------------------------754 // Delete pre-existent .smp and .ref file with the same base name755 // --------------------------------------------------------------------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 }770 811 771 812 return (IdrisiDataset *) GDALOpen( pszFilename, GA_Update ); … … 814 855 GDALDataType eType = poSrcDS->GetRasterBand( i )->GetRasterDataType(); 815 856 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 } 829 886 } 830 887 } … … 837 894 GDALDataType eType = poBand->GetRasterDataType(); 838 895 896 int bSuccessMin = FALSE; 897 int bSuccessMax = FALSE; 898 839 899 double dfMin; 840 900 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 } 845 909 846 910 if(!( ( eType == GDT_Byte ) || … … 907 971 if( poDS->nBands == 1 ) 908 972 { 909 poBand->SetCategoryNames( poSrcBand->GetCategoryNames() );910 poBand->SetColorTable( poSrcBand->GetColorTable() );911 973 poBand->SetUnitType( poSrcBand->GetUnitType() ); 912 974 913 // ------------------------------------------------------------914 // Copy Attribute Table information915 // ------------------------------------------------------------916 917 975 const GDALRasterAttributeTable *poRAT = poSrcBand->GetDefaultRAT(); 976 918 977 if( poRAT != NULL ) 919 978 { 920 979 poBand->SetDefaultRAT( poRAT ); 921 980 } 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 ); 926 996 dfNoDataValue = poSrcBand->GetNoDataValue( &bHasNoDataValue ); 927 997 if( bHasNoDataValue ) … … 939 1009 poDS->GetRasterBand( 1 )->GetBlockSize( &nBlockXSize, &nBlockYSize ); 940 1010 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 941 1022 for( int iBand = 1; iBand <= poSrcDS->GetRasterCount(); iBand++ ) 942 1023 { … … 945 1026 946 1027 int iYOffset, iXOffset; 947 void *pData;948 1028 CPLErr eErr = CE_None; 949 950 pData = CPLMalloc( nBlockXSize * nBlockYSize * GDALGetDataTypeSize( eType ) / 8 );951 1029 952 1030 for( iYOffset = 0; iYOffset < nYSize; iYOffset += nBlockYSize ) … … 980 1058 } 981 1059 } 982 CPLFree( pData ); 983 } 1060 } 1061 1062 CPLFree( pData ); 984 1063 985 1064 // -------------------------------------------------------------------- … … 1079 1158 CPLErr eResult = CE_None; 1080 1159 1081 c onst char *pszRefSystem;1082 c onst char *pszRefUnit;1160 char *pszRefSystem = NULL; 1161 char *pszRefUnit = NULL; 1083 1162 1084 1163 eResult = Wkt2GeoReference( pszProjString, &pszRefSystem, &pszRefUnit ); … … 1086 1165 CSLSetNameValue( papszRDC, rdcREF_SYSTEM, pszRefSystem ); 1087 1166 CSLSetNameValue( papszRDC, rdcREF_UNITS, pszRefUnit ); 1167 1168 CPLFree( pszRefSystem ); 1169 CPLFree( pszRefUnit ); 1088 1170 1089 1171 return eResult; … … 1102 1184 this->eDataType = eDataType; 1103 1185 this->poDefaultRAT = NULL; 1186 this->dfMinimum = 0.0; 1187 this->dfMaximum = 0.0; 1188 this->bFirstVal = true; 1104 1189 1105 1190 // -------------------------------------------------------------------- … … 1114 1199 // -------------------------------------------------------------------- 1115 1200 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; 1118 1213 } 1119 1214 … … 1137 1232 1138 1233 CPLErr IdrisiRasterBand::IReadBlock( int nBlockXOff, 1139 int nBlockYOff,1140 void *pImage )1234 int nBlockYOff, 1235 void *pImage ) 1141 1236 { 1142 1237 IdrisiDataset *poGDS = (IdrisiDataset *) poDS; … … 1184 1279 1185 1280 CPLErr IdrisiRasterBand::IWriteBlock( int nBlockXOff, 1186 int nBlockYOff,1187 void *pImage )1281 int nBlockYOff, 1282 void *pImage ) 1188 1283 { 1189 1284 IdrisiDataset *poGDS = (IdrisiDataset *) poDS; … … 1229 1324 } 1230 1325 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 1231 1386 return CE_None; 1232 1387 } … … 1245 1400 1246 1401 if( pbSuccess ) 1247 *pbSuccess = TRUE; 1402 { 1403 *pbSuccess = true; 1404 } 1248 1405 1249 1406 return adfMinValue[this->nBand - 1]; … … 1263 1420 1264 1421 if( pbSuccess ) 1265 *pbSuccess = TRUE; 1422 { 1423 *pbSuccess = true; 1424 } 1266 1425 1267 1426 return adfMaxValue[this->nBand - 1]; … … 1294 1453 { 1295 1454 dfNoData = atof_nz( CSLFetchNameValue( poGDS->papszRDC, rdcFLAG_VALUE ) ); 1296 *pbSuccess = TRUE; 1455 if( pbSuccess ) 1456 *pbSuccess = TRUE; 1297 1457 } 1298 1458 else 1299 1459 { 1300 1460 dfNoData = -9999.0; /* this value should be ignored */ 1301 *pbSuccess = FALSE; 1461 if( pbSuccess ) 1462 *pbSuccess = FALSE; 1302 1463 } 1303 1464 … … 1574 1735 CPLErr IdrisiRasterBand::SetDefaultRAT( const GDALRasterAttributeTable *poRAT ) 1575 1736 { 1737 if( ! poRAT ) 1738 { 1739 return CE_Failure; 1740 } 1741 1576 1742 // ---------------------------------------------------------- 1577 // Update Bands Attribute Table1743 // Get field indecies 1578 1744 // ---------------------------------------------------------- 1579 1745 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 1580 1920 if( poDefaultRAT ) 1581 1921 { … … 1584 1924 1585 1925 poDefaultRAT = poRAT->Clone(); 1586 1587 int nEntryCount = poDefaultRAT->GetRowCount();1588 1589 // ----------------------------------------------------------1590 // Get the Color Table from the Attribute Table1591 // ----------------------------------------------------------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 Table1633 // ------------------------------------------------------1634 1635 SetColorTable(poCT);1636 delete poCT;1637 }1638 1639 // ----------------------------------------------------------1640 // Get the Catagory Names from the Attribute Table1641 // ----------------------------------------------------------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 File1668 // ----------------------------------------------------------1669 1670 SetCategoryNames( papszCategoryNames );1671 CSLDestroy( papszCategoryNames );1672 1926 1673 1927 return CE_None; … … 1682 1936 IdrisiDataset *poGDS = (IdrisiDataset *) poDS; 1683 1937 1684 if( poGDS->papszCategories == NULL && 1685 ( poGDS->poColorTable->GetColorEntryCount() == 0 ) ) 1938 if( poGDS->papszCategories == NULL ) 1686 1939 { 1687 1940 return NULL; 1688 1941 } 1942 1943 bool bHasColorTable = poGDS->poColorTable->GetColorEntryCount() > 0; 1689 1944 1690 1945 // ---------------------------------------------------------- … … 1700 1955 1701 1956 // ---------------------------------------------------------- 1702 // Set the Color Table to the Attribute Table1957 // Create (Value, Red, Green, Blue, Alpha, Class_Name) fields 1703 1958 // ---------------------------------------------------------- 1704 1959 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 ); 1706 1971 1707 1972 // ---------------------------------------------------------- 1708 // Set the Catagory Names to the Attribute Table1973 // Loop throught the Category Names 1709 1974 // ---------------------------------------------------------- 1710 1975 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 ); 1716 1979 int iEntry = 0; 1717 int iRow = 0;1980 int iRows = 0; 1718 1981 1719 1982 for( iEntry = 0; iEntry < nEntryCount; iEntry++ ) 1720 1983 { 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 1732 2003 return poDefaultRAT; 1733 2004 } … … 1766 2037 1767 2038 CPLErr IdrisiDataset::GeoReference2Wkt( const char *pszRefSystem, 1768 const char *pszRefUnits,1769 char **pszProjString )2039 const char *pszRefUnits, 2040 char **pszProjString ) 1770 2041 { 1771 2042 OGRSpatialReference oSRS; … … 1778 2049 { 1779 2050 oSRS.SetLocalCS( "Plane" ); 1780 int nUnit = GetUnitIndex( GetUnitDefault( pszRefUnits ));2051 int nUnit = GetUnitIndex( pszRefUnits ); 1781 2052 if( nUnit > -1 ) 1782 2053 { 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 ); 1785 2057 } 1786 2058 oSRS.exportToWkt( pszProjString ); … … 1837 2109 nZone = ( nZone == 1 ? nSPCode : nSPCode + nZone - 1 ); 1838 2110 1839 if( oSRS.SetStatePlane( nZone, ( nNAD == 83 ) ) != OGRERR_FAILURE )2111 if( oSRS.SetStatePlane( nZone, ( nNAD == 83 ) ) != OGRERR_FAILURE ) 1840 2112 { 1841 2113 oSRS.exportToWkt( pszProjString ); … … 1885 2157 { 1886 2158 oSRS.SetLocalCS( "Unknown" ); 1887 int nUnit = GetUnitIndex( GetUnitDefault( pszRefUnits ));2159 int nUnit = GetUnitIndex( pszRefUnits ); 1888 2160 if( nUnit > -1 ) 1889 2161 { 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 ); 1892 2165 } 1893 2166 } … … 1903 2176 CSLSetNameValueSeparator( papszRef, ":" ); 1904 2177 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 ) ); 1911 2191 double dfCenterLat = atof_nz( CSLFetchNameValue( papszRef, refORIGIN_LAT ) ); 1912 2192 double dfCenterLong = atof_nz( CSLFetchNameValue( papszRef, refORIGIN_LONG ) ); … … 2000 2280 { 2001 2281 oSRS.exportToWkt( pszProjString ); 2282 2283 CPLFree( pszGeorefName ); 2284 CPLFree( pszProjName ); 2285 CPLFree( pszDatum ); 2286 CPLFree( pszEllipsoid ); 2287 2002 2288 return CE_None; 2003 2289 } … … 2050 2336 { 2051 2337 oSRS.SetProjection( pszProjName ); 2052 oSRS.Set NormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat );2053 oSRS.Set NormProjParm( SRS_PP_CENTRAL_MERIDIAN, dfCenterLong );2054 oSRS.Set NormProjParm( SRS_PP_FALSE_EASTING, dfFalseEasting );2055 oSRS.Set NormProjParm( 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 ); 2056 2342 } 2057 2343 else if( EQUALN( pszProjName, "Lambert North Polar Azimuthal Equal Area", 15 ) || … … 2092 2378 oSRS.Clear(); 2093 2379 oSRS.exportToWkt( pszProjString ); 2380 2381 CPLFree( pszGeorefName ); 2382 CPLFree( pszProjName ); 2383 CPLFree( pszDatum ); 2384 CPLFree( pszEllipsoid ); 2385 2094 2386 return CE_Warning; 2095 2387 } … … 2099 2391 // ---------------------------------------------------------------------- 2100 2392 2101 int nUnit = GetUnitIndex( GetUnitDefault( pszRefUnits ));2393 int nUnit = GetUnitIndex( pszRefUnits ); 2102 2394 if( nUnit > -1 ) 2103 2395 { 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 ); 2106 2403 } 2107 2404 … … 2113 2410 2114 2411 oSRS.exportToWkt( pszProjString ); 2412 2413 CPLFree( pszGeorefName ); 2414 CPLFree( pszProjName ); 2415 CPLFree( pszDatum ); 2416 CPLFree( pszEllipsoid ); 
