36 #include "ogr_spatialref.h"
37 #include "ogr_srs_api.h"
41 #include <boost/algorithm/string/predicate.hpp>
162 itkDebugMacro(<<
"No filename specified.");
174 os << indent <<
"IsComplex (otb side) : " <<
m_IsComplex <<
"\n";
187 unsigned char *p =
static_cast<unsigned char *
>(buffer);
192 itkExceptionMacro(<<
"GDAL : Bad alloc");
202 GDALDataset* dataset =
m_Dataset->GetDataSet();
300 std::streamoff lNbPixels = (
static_cast<std::streamoff
>(lNbColumns))
301 * (
static_cast<std::streamoff
>(lNbLines));
302 std::streamoff lBufferSize =
static_cast<std::streamoff
>(
m_BytePerPixel) * lNbPixels;
308 CPLErr lCrGdal = dataset->GetRasterBand(1)->RasterIO(GF_Read,
319 if (lCrGdal == CE_Failure)
321 itkExceptionMacro(<<
"Error while reading image (GDAL format) " <<
m_FileName.c_str() <<
".");
324 std::streamoff cpt(0);
325 GDALColorTable* colorTable = dataset->GetRasterBand(1)->GetColorTable();
326 for (std::streamoff i = 0; i < lBufferSize; i = i + static_cast<std::streamoff>(
m_BytePerPixel))
328 GDALColorEntry color;
329 colorTable->GetColorEntryAsRGB(value[i], &color);
331 p[cpt + 1] = color.c2;
332 p[cpt + 2] = color.c3;
333 p[cpt + 3] = color.c4;
349 lineOffset = pixelOffset * lNbColumns;
356 <<
" indX = " << lFirstColumn <<
"\n"
357 <<
" indY = " << lFirstLine <<
"\n"
358 <<
" sizeX = " << lNbColumns <<
"\n"
359 <<
" sizeY = " << lNbLines <<
"\n"
360 <<
" GDAL Data Type = " << GDALGetDataTypeName(
m_PxType->
pixType) <<
"\n"
361 <<
" pixelOffset = " << pixelOffset <<
"\n"
362 <<
" lineOffset = " << lineOffset <<
"\n"
363 <<
" bandOffset = " << bandOffset);
367 CPLErr lCrGdal =
m_Dataset->GetDataSet()->RasterIO(GF_Read,
386 if (lCrGdal == CE_Failure)
388 itkExceptionMacro(<<
"Error while reading image (GDAL format) " <<
m_FileName.c_str() <<
".");
398 char** papszMetadata;
399 papszMetadata =
m_Dataset->GetDataSet()->GetMetadata(
"SUBDATASETS");
403 if ( (CSLCount(papszMetadata) > 0) &&
404 ( (strcmp(
m_Dataset->GetDataSet()->GetDriver()->GetDescription(),
"HDF4") == 0) ||
405 (strcmp(
m_Dataset->GetDataSet()->GetDriver()->GetDescription(),
"HDF5") == 0) ) )
407 for (
int cpt = 0; papszMetadata[cpt] !=
NULL; ++cpt)
409 std::string key, name;
415 if (key.find(
"_NAME") != std::string::npos) names.push_back(name);
417 if (key.find(
"_DESC") != std::string::npos) desc.push_back(name);
425 if (names.empty() || desc.empty())
return false;
426 if (names.size() != desc.size())
455 if (
m_Dataset->GetDataSet()->GetRasterCount() == 0)
459 char** papszMetadata;
460 papszMetadata =
m_Dataset->GetDataSet()->GetMetadata(
"SUBDATASETS");
462 std::vector<std::string> names;
463 if( CSLCount(papszMetadata) > 0 )
465 for(
int cpt = 0; papszMetadata[cpt] !=
NULL; ++cpt )
467 std::string key, name;
473 if (key.find(
"_NAME") != std::string::npos) names.push_back(name);
484 itkExceptionMacro(<<
"Dataset requested does not exist (" << names.size() <<
" datasets)");
488 GDALDataset* dataset =
m_Dataset->GetDataSet();
491 if ( dataset->GetRasterXSize() == 0 || dataset->GetRasterYSize() == 0 )
493 itkExceptionMacro(<<
"Dimension is undefined.");
567 itkExceptionMacro(<<
"Pixel type unknown");
633 itkExceptionMacro(<<
"Component type unknown");
646 otbMsgDevMacro( <<
"GDALtypeIO= Complex and IFReader::InternalPixelType= Scalar and IFReader::PixelType= Vector");
682 if (dataset->GetRasterCount() > 0)
687 dataset->GetRasterBand(1)->GetBlockSize(&blockSizeX, &blockSizeY);
689 if(blockSizeX > 0 && blockSizeY > 0)
706 char** papszMetadata;
707 papszMetadata = dataset->GetMetadata(
NULL);
714 hDriver = dataset->GetDriver();
716 std::string driverShortName =
static_cast<std::string
>(GDALGetDriverShortName(hDriver));
717 std::string driverLongName =
static_cast<std::string
>(GDALGetDriverLongName(hDriver));
725 if (dataset->GetProjectionRef() !=
NULL && !std::string(dataset->GetProjectionRef()).empty())
727 OGRSpatialReferenceH pSR = OSRNewSpatialReference(
NULL);
729 const char * pszProjection =
NULL;
730 pszProjection = dataset->GetProjectionRef();
732 if (OSRImportFromWkt(pSR, (
char **) (&pszProjection)) == OGRERR_NONE)
734 char * pszPrettyWkt =
NULL;
735 OSRExportToPrettyWkt(pSR, &pszPrettyWkt, FALSE);
738 static_cast<std::string
>(pszPrettyWkt));
740 CPLFree(pszPrettyWkt);
745 static_cast<std::string
>(dataset->GetProjectionRef()));
759 unsigned int gcpCount = 0;
760 gcpCount = dataset->GetGCPCount();
763 std::string gcpProjectionKey =
static_cast<std::string
>(dataset->GetGCPProjection());
766 if (gcpProjectionKey.empty())
776 for (
unsigned int cpt = 0; cpt < gcpCount; ++cpt)
779 const GDAL_GCP *psGCP;
780 psGCP = dataset->GetGCPs() + cpt;
783 pOtbGCP.
m_Id = std::string(psGCP->pszId);
784 pOtbGCP.
m_Info = std::string(psGCP->pszInfo);
785 pOtbGCP.
m_GCPRow = psGCP->dfGCPLine;
786 pOtbGCP.
m_GCPCol = psGCP->dfGCPPixel;
787 pOtbGCP.
m_GCPX = psGCP->dfGCPX;
788 pOtbGCP.
m_GCPY = psGCP->dfGCPY;
789 pOtbGCP.
m_GCPZ = psGCP->dfGCPZ;
792 std::ostringstream lStream;
796 itk::EncapsulateMetaData<OTB_GCP>(dict, key, pOtbGCP);
806 double adfGeoTransform[6];
809 if (dataset->GetGeoTransform(adfGeoTransform) == CE_None)
811 for (
int cpt = 0; cpt < 6; ++cpt)
812 VadfGeoTransform.push_back(adfGeoTransform[cpt]);
818 m_Origin[1] = VadfGeoTransform[3];
825 otbMsgDevMacro(<<
"**** ReadImageInformation() DATASET INFO: ****" );
826 otbMsgDevMacro(<<
"Projection Ref: "<< dataset->GetProjectionRef() );
828 if (dataset->GetGeoTransform(GT) == CE_None)
830 otbMsgDevMacro( <<
"Geo Transform: "<< GT[0] <<
", " << GT[1] <<
", "
831 << GT[2] <<
", " << GT[3] <<
", "
832 << GT[4] <<
", " << GT[5] );
838 otbMsgDevMacro(<<
"GCP Projection Ref: "<< dataset->GetGCPProjection() );
845 papszMetadata = dataset->GetMetadata(
NULL);
846 if (CSLCount(papszMetadata) > 0)
850 for (
int cpt = 0; papszMetadata[cpt] !=
NULL; ++cpt)
852 std::ostringstream lStream;
856 itk::EncapsulateMetaData<std::string>(dict, key,
857 static_cast<std::string
>(papszMetadata[cpt]));
865 papszMetadata = dataset->GetMetadata(
"SUBDATASETS");
866 if (CSLCount(papszMetadata) > 0)
870 for (
int cpt = 0; papszMetadata[cpt] !=
NULL; ++cpt)
872 std::ostringstream lStream;
876 itk::EncapsulateMetaData<std::string>(dict, key,
877 static_cast<std::string
>(papszMetadata[cpt]));
885 double GeoX(0), GeoY(0);
889 VGeo.push_back(GeoX);
890 VGeo.push_back(GeoY);
897 VGeo.push_back(GeoX);
898 VGeo.push_back(GeoY);
905 VGeo.push_back(GeoX);
906 VGeo.push_back(GeoY);
913 VGeo.push_back(GeoX);
914 VGeo.push_back(GeoY);
924 for (
int iBand = 0; iBand < dataset->GetRasterCount(); iBand++)
926 GDALColorTableH hTable;
927 GDALRasterBandH hBand;
928 hBand = GDALGetRasterBand(dataset, iBand + 1);
929 if ((GDALGetRasterColorInterpretation(hBand) == GCI_PaletteIndex)
930 && (hTable = GDALGetRasterColorTable(hBand)) !=
NULL)
934 unsigned int ColorEntryCount = GDALGetColorEntryCount(hTable);
937 static_cast<std::string
>(GDALGetPaletteInterpretationName(
938 GDALGetPaletteInterpretation(hTable))));
942 for (
int i = 0; i < GDALGetColorEntryCount(hTable); ++i)
944 GDALColorEntry sEntry;
947 GDALGetColorEntryAsRGB(hTable, i, &sEntry);
949 VColorEntry.push_back(sEntry.c1);
950 VColorEntry.push_back(sEntry.c2);
951 VColorEntry.push_back(sEntry.c3);
952 VColorEntry.push_back(sEntry.c4);
974 itkDebugMacro(<<
"No filename specified.");
980 if (gdalDriverShortName ==
"NOT-FOUND")
987 if ( GDALGetMetadataItem( driver, GDAL_DCAP_CREATE,
NULL ) ==
NULL
988 && GDALGetMetadataItem( driver, GDAL_DCAP_CREATECOPY,
NULL ) ==
NULL )
990 itkDebugMacro(<<
"The driver " << GDALGetDriverShortName(driver) <<
" does not support writing");
1004 itkDebugMacro(<<
"Unable to instantiate driver " << gdalDriverShortName);
1007 if ( GDALGetMetadataItem( driver, GDAL_DCAP_CREATE,
NULL ) !=
NULL )
1031 itkExceptionMacro(<<
"GDAL : Bad alloc");
1059 "\n, lNbColumns =" << lNbColumns <<
1060 "\n, lNbLines =" << lNbLines <<
1070 CPLErr lCrGdal =
m_Dataset->GetDataSet()->RasterIO(GF_Write,
1075 const_cast<void *>(buffer),
1094 if (lCrGdal == CE_Failure)
1096 itkExceptionMacro(<<
"Error while writing image (GDAL format) " <<
m_FileName.c_str() <<
".");
1111 itkExceptionMacro(<<
"Unable to instantiate driver " << gdalDriverShortName <<
" to write " <<
m_FileName);
1115 GDALDataset* hOutputDS = driver->CreateCopy( realFileName.c_str(),
m_Dataset->GetDataSet(), FALSE,
1118 GDALClose(hOutputDS);
1138 char ** papszOptions =
NULL;
1139 std::string driverShortName;
1144 itkExceptionMacro(<<
"Dimensions are not defined.");
1213 itkWarningMacro(<<
"Cast a long (64 bits) image into an int (32 bits) one.")
1222 itkWarningMacro(<<
"Cast an unsigned long (64 bits) image into an unsigned int (32 bits) one.")
1247 if (driverShortName ==
"NOT-FOUND")
1250 <<
"GDAL Writing failed : the image file name '" <<
m_FileName.c_str() <<
"' is not recognized by GDAL.");
1318 std::ostringstream stream;
1320 <<
"DATAPOINTER=" << (
unsigned long)(buffer) <<
","
1335 <<
"GDAL Writing failed : Impossible to create the image file name '" <<
m_FileName <<
"'.");
1344 std::ostringstream oss;
1345 GDALDataset* dataset =
m_Dataset->GetDataSet();
1347 std::string projectionRef;
1353 const double Epsilon = 1E-10;
1354 if (projectionRef.empty()
1358 || vcl_abs(
m_Spacing[1] - 1) > Epsilon) )
1364 otbMsgDevMacro(<<
"Skipping GCPs saving to prevent GDAL from assigning a WGS84 projection ref to the file")
1368 unsigned int gcpCount = 0;
1374 GDAL_GCP * gdalGcps =
new GDAL_GCP[gcpCount];
1376 for (
unsigned int gcpIndex = 0; gcpIndex < gcpCount; ++gcpIndex)
1379 std::ostringstream lStream;
1381 std::string key = lStream.str();
1384 itk::ExposeMetaData<OTB_GCP>(dict, key, gcp);
1386 gdalGcps[gcpIndex].pszId =
const_cast<char *
>(gcp.
m_Id.c_str());
1387 gdalGcps[gcpIndex].pszInfo =
const_cast<char *
>(gcp.
m_Info.c_str());
1388 gdalGcps[gcpIndex].dfGCPPixel = gcp.
m_GCPCol;
1389 gdalGcps[gcpIndex].dfGCPLine = gcp.
m_GCPRow;
1390 gdalGcps[gcpIndex].dfGCPX = gcp.
m_GCPX;
1391 gdalGcps[gcpIndex].dfGCPY = gcp.
m_GCPY;
1392 gdalGcps[gcpIndex].dfGCPZ = gcp.
m_GCPZ;
1395 std::string gcpProjectionRef;
1398 dataset->SetGCPs(gcpCount, gdalGcps, gcpProjectionRef.c_str());
1407 if (!projectionRef.empty())
1409 dataset->SetProjection(projectionRef.c_str());
1423 geoTransform[2] = 0.;
1424 geoTransform[4] = 0.;
1425 dataset->SetGeoTransform(const_cast<double*>(geoTransform.
GetDataPointer()));
1431 std::string svalue =
"";
1432 std::vector<std::string> keys = dict.GetKeys();
1435 for (
unsigned int itkey = 0; itkey < keys.size(); ++itkey)
1438 if (keys[itkey].compare(0, metadataKey.length(), metadataKey) == 0)
1440 itk::ExposeMetaData<std::string>(dict, keys[itkey], svalue);
1441 unsigned int equalityPos = svalue.find_first_of(
'=');
1442 std::string tag = svalue.substr(0, equalityPos);
1443 std::string value = svalue.substr(equalityPos + 1);
1445 dataset->SetMetadataItem(tag.c_str(), value.c_str(),
NULL);
1451 otbMsgDevMacro( <<
"**** WriteImageInformation() DATASET INFO: ****" );
1452 otbMsgDevMacro( <<
"Projection Ref: "<<dataset->GetProjectionRef() );
1454 if (dataset->GetGeoTransform(GT) == CE_None)
1456 otbMsgDevMacro( <<
"Geo Transform: "<< GT[0] <<
", " << GT[1] <<
", "
1457 << GT[2] <<
", " << GT[3] <<
", "
1458 << GT[4] <<
", " << GT[5] );
1465 otbMsgDevMacro( <<
"GCP Projection Ref: "<< dataset->GetGCPProjection() );
1472 std::string extension;
1473 std::string gdalDriverShortName;
1478 if ( extension ==
"tif" || extension ==
"tiff" )
1479 gdalDriverShortName =
"GTiff";
1480 else if ( extension ==
"hdr" )
1481 gdalDriverShortName =
"ENVI";
1482 else if ( extension ==
"img" )
1483 gdalDriverShortName =
"HFA";
1484 else if ( extension ==
"ntf" )
1485 gdalDriverShortName =
"NITF";
1486 else if ( extension ==
"png" )
1487 gdalDriverShortName=
"PNG";
1488 else if ( extension ==
"jpg" || extension==
"jpeg" )
1489 gdalDriverShortName=
"JPEG";
1490 else if ( extension ==
"pix" )
1491 gdalDriverShortName=
"PCIDSK";
1492 else if ( extension ==
"lbl" || extension ==
"pds" )
1493 gdalDriverShortName=
"ISIS2";
1495 gdalDriverShortName =
"NOT-FOUND";
1497 return gdalDriverShortName;
1502 std::string gdalFileName;
1504 gdalFileName = filename;
1506 if (gdalDriverShortName ==
"ENVI")
1510 return gdalFileName;
1515 double adfGeoTransform[6];
1521 if (
m_Dataset->GetDataSet()->GetGeoTransform(adfGeoTransform) == CE_None)
1523 GeoX = adfGeoTransform[0] + adfGeoTransform[1] * x + adfGeoTransform[2] * y;
1524 GeoY = adfGeoTransform[3] + adfGeoTransform[4] * x + adfGeoTransform[5] * y;