本文整理汇总了C++中GDALRasterBand::GetColorInterpretation方法的典型用法代码示例。如果您正苦于以下问题:C++ GDALRasterBand::GetColorInterpretation方法的具体用法?C++ GDALRasterBand::GetColorInterpretation怎么用?C++ GDALRasterBand::GetColorInterpretation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GDALRasterBand
的用法示例。
在下文中一共展示了GDALRasterBand::GetColorInterpretation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: loadFile
void RasterImageLayer::loadFile()
{
GDALDataset *ds = static_cast<GDALDataset*>(GDALOpen(m_filename.toLocal8Bit(), GA_ReadOnly));
if (ds == nullptr)
{
qWarning() << "Error opening file:" << m_filename;
}
else
{
projection().setGeogCS(new OGRSpatialReference(ds->GetProjectionRef()));
projection().setProjCS(new OGRSpatialReference(ds->GetProjectionRef()));
projection().setDomain({-180., -90., 360., 180.});
std::vector<double> geoTransform(6);
int xsize = ds->GetRasterXSize();
int ysize = ds->GetRasterYSize();
ds->GetGeoTransform(geoTransform.data());
vertData.resize(4);
vertData[0] = QVector2D(geoTransform[0], geoTransform[3]);
vertData[1] = QVector2D(geoTransform[0] + geoTransform[2] * ysize,
geoTransform[3] + geoTransform[5] * ysize);
vertData[2] = QVector2D(geoTransform[0] + geoTransform[1] * xsize + geoTransform[2] * ysize,
geoTransform[3] + geoTransform[4] * xsize + geoTransform[5] * ysize);
vertData[3] = QVector2D(geoTransform[0] + geoTransform[1] * xsize,
geoTransform[3] + geoTransform[4] * xsize);
texData = {{0., 0.}, {0., 1.}, {1., 1.}, {1., 0.}};
int numBands = ds->GetRasterCount();
qDebug() << "Bands:" << numBands;
imData = QImage(xsize, ysize, QImage::QImage::Format_RGBA8888);
imData.fill(QColor(255, 255, 255, 255));
// Bands start at 1
for (int i = 1; i <= numBands; ++i)
{
GDALRasterBand *band = ds->GetRasterBand(i);
switch(band->GetColorInterpretation())
{
case GCI_RedBand:
band->RasterIO(GF_Read, 0, 0, xsize, ysize, imData.bits(),
xsize, ysize, GDT_Byte, 4, 0);
break;
case GCI_GreenBand:
band->RasterIO(GF_Read, 0, 0, xsize, ysize, imData.bits() + 1,
xsize, ysize, GDT_Byte, 4, 0);
break;
case GCI_BlueBand:
band->RasterIO(GF_Read, 0, 0, xsize, ysize, imData.bits() + 2,
xsize, ysize, GDT_Byte, 4, 0);
break;
default:
qWarning() << "Unhandled color interpretation:" << band->GetColorInterpretation();
}
}
GDALClose(ds);
newFile = true;
}
}
示例2: import_raster
Raster* import_raster(string raster_filename, int band_number) {
GDALAllRegister();
GDALDataset* poDataset = (GDALDataset *) GDALOpen( raster_filename.c_str(), GA_ReadOnly );
if( poDataset == NULL ) {
cerr << "Error: Could not open raster data file" << endl;
exit(1);
}
fprintf(stderr, "Driver: %s/%s\n", poDataset->GetDriver()->GetDescription(), poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
fprintf(stderr, "Size is %dx%dx%d\n", poDataset->GetRasterXSize(), poDataset->GetRasterYSize(), poDataset->GetRasterCount() );
if( poDataset->GetProjectionRef() != NULL ) cerr << "Projection is `" << poDataset->GetProjectionRef() << "'" << endl;;
GDALRasterBand* poBand = poDataset->GetRasterBand( band_number );
int nBlockXSize, nBlockYSize;
poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );
fprintf(stderr, "Block=%dx%d Type=%s, ColorInterp=%s\n",
nBlockXSize, nBlockYSize,
GDALGetDataTypeName(poBand->GetRasterDataType()),
GDALGetColorInterpretationName( poBand->GetColorInterpretation()) );
Raster* raster = extract_raster_attributes( poDataset );
raster->band = poBand;
return raster;
}
示例3: CPLAssert
PLCLine *PLCContext::getOutputLine(int line)
{
CPLAssert( outputDS != NULL );
int i, width = outputDS->GetRasterXSize();
PLCLine *lineObj = new PLCLine(width);
for( i=0; i < outputDS->GetRasterCount(); i++ )
{
CPLErr eErr;
GDALRasterBand *band = outputDS->GetRasterBand(i+1);
if( band->GetColorInterpretation() == GCI_AlphaBand )
eErr = band->RasterIO(GF_Read, 0, line, width, 1,
lineObj->getAlpha(), width, 1, GDT_Byte,
0, 0);
else
eErr = band->RasterIO(GF_Read, 0, line, width, 1,
lineObj->getBand(i), width, 1, GDT_Int16,
0, 0);
if( eErr != CE_None )
exit(1);
}
return lineObj;
}
示例4: MetaDataInfo
void DemReader::MetaDataInfo(GDALDataset* gdalDataset, MapControllerPtr mapController)
{
FileMetaData* header = mapController->GetMapModel()->GetMetaData();
header->driverDesc = gdalDataset->GetDriver()->GetDescription();
header->driverMetaData = gdalDataset->GetDriver()->GetMetadataItem(GDAL_DMD_LONGNAME);
Log::Inst().Write("Driver: " + header->driverDesc + " \\ " + header->driverMetaData);
header->xSize = gdalDataset->GetRasterXSize();
header->ySize = gdalDataset->GetRasterYSize();
header->bands = gdalDataset->GetRasterCount();
Log::Inst().Write("Size (x,y): " +
StringTools::ToString(header->xSize) + ", " +
StringTools::ToString(header->ySize));
Log::Inst().Write("Bands: " + StringTools::ToString(header->bands));
header->projection = std::string(gdalDataset->GetProjectionRef());
Log::Inst().Write("Projection: " + header->projection);
double adfGeoTransform[6];
gdalDataset->GetGeoTransform( adfGeoTransform );
header->originX = adfGeoTransform[0];
header->originY = adfGeoTransform[3];
Log::Inst().Write("Origin: " +
StringTools::ToString(adfGeoTransform[0]) + ", " +
StringTools::ToString(adfGeoTransform[3]));
header->pixelSizeX = adfGeoTransform[1];
header->pixelSizeY = adfGeoTransform[5];
Log::Inst().Write("Pixel Size: " +
StringTools::ToString(adfGeoTransform[1]) + ", " +
StringTools::ToString(adfGeoTransform[5]));
GDALRasterBand* gdalBand = gdalDataset->GetRasterBand(1);
int nBlockXSize, nBlockYSize;
gdalBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
header->dataType = std::string(GDALGetDataTypeName(gdalBand->GetRasterDataType()));
Log::Inst().Write("Block, Type: " + StringTools::ToString(nBlockXSize) + ", " +
StringTools::ToString(nBlockYSize) + ", " +
std::string(header->dataType));
header->colourInterpretation = std::string(GDALGetColorInterpretationName(gdalBand->GetColorInterpretation()));
Log::Inst().Write("Color Interpretation: " + header->colourInterpretation);
header->extents.x = adfGeoTransform[0];
header->extents.y = adfGeoTransform[3] + gdalBand->GetYSize()*adfGeoTransform[5];
Log::Inst().Write("Lower, Left (x,y): " +
StringTools::ToString(header->extents.x) + ", " +
StringTools::ToString(header->extents.y));
header->extents.dx = adfGeoTransform[0]+gdalBand->GetXSize()*adfGeoTransform[1];
header->extents.dy = adfGeoTransform[3];
Log::Inst().Write("Upper, Right (x,y): " +
StringTools::ToString(header->extents.dx) + ", " +
StringTools::ToString(header->extents.dy));
Log::Inst().Write("");
}
示例5: getRawValuesFromFile
bool getRawValuesFromFile(string fname,vector<vector<float>>& vecs)
{
//vector<float> temp = vector<float>()
GDALDataset *poDataset;
GDALAllRegister();
poDataset= (GDALDataset*) GDALOpen(fname.c_str(),GA_ReadOnly);
if(poDataset == NULL)
{
cout << "OUCH!" << endl;
return false;
}
cout << "Data size: " << GDALGetRasterXSize(poDataset) << " " << GDALGetRasterYSize(poDataset) << endl;
GDALRasterBand *poBand;
int nBlockXSize, nBlockYSize;
int bGotMin, bGotMax;
double adfMinMax[2];
poBand = poDataset->GetRasterBand( 1 );
poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );
printf( "Block=%dx%d Type=%s, ColorInterp=%s\n",
nBlockXSize, nBlockYSize,
GDALGetDataTypeName(poBand->GetRasterDataType()),
GDALGetColorInterpretationName(
poBand->GetColorInterpretation()) );
adfMinMax[0] = poBand->GetMinimum( &bGotMin );
adfMinMax[1] = poBand->GetMaximum( &bGotMax );
if( ! (bGotMin && bGotMax) )
GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);
int width = poBand->GetXSize();
int height = poBand->GetYSize();
int bands = poDataset->GetRasterCount();
float *pafScanline;
std::cout << "Before allocation" << adfMinMax[0] << " " << adfMinMax[1] << endl;
int dsize = 256;
pafScanline = (float *) CPLMalloc(sizeof(float)*width*height);
vector<vector<float>> out = vector<vector<float>>(height,vector<float> (width,0));
poBand->RasterIO(GF_Read,0,0,width,height,pafScanline,width,height,GDT_Float32,0,0);
cout << "After allocation" << endl;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
//cout << i << j << endl << pafS;
out[i][j] = pafScanline[i*width+j];
}
}
CPLFree(pafScanline);
//for(auto i : out)
//for(auto j : i)
// cout << j << endl;
cout << "After allocation" << endl;
vecs = out;
return true;
}
示例6: main
int main()
{
GDALDataset *poDataset;
GDALAllRegister();
poDataset = (GDALDataset *) GDALOpen( "GE01.tif", GA_ReadOnly );
printf("Working! \n");
if( poDataset != NULL ){
//Get Dataset Information
double adfGeoTransform[6];
printf( "Driver: %s/%s\n", poDataset->GetDriver()->GetDescription(), poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
printf( "Size is %dx%dx%d\n", poDataset->GetRasterXSize(), poDataset->GetRasterYSize(), poDataset->GetRasterCount() );
if( poDataset->GetProjectionRef() != NULL )
printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );
if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None ){
printf( "Origin = (%.6f,%.6f)\n",
adfGeoTransform[0], adfGeoTransform[3] );
printf( "Pixel Size = (%.6f,%.6f)\n",
adfGeoTransform[1], adfGeoTransform[5] );
}
//Fetch Raster Band
GDALRasterBand *poBand;
int nBlockXSize, nBlockYSize;
int bGotMin, bGotMax;
double adfMinMax[2];
poBand = poDataset->GetRasterBand( 1 );
poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );
printf( "Block=%dx%d Type=%s, ColorInterp=%s\n",
nBlockXSize, nBlockYSize,
GDALGetDataTypeName(poBand->GetRasterDataType()),
GDALGetColorInterpretationName(
poBand->GetColorInterpretation()) );
adfMinMax[0] = poBand->GetMinimum( &bGotMin );
adfMinMax[1] = poBand->GetMaximum( &bGotMax );
if( ! (bGotMin && bGotMax) )
GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);
printf( "Min=%.3fd, Max=%.3f\n", adfMinMax[0], adfMinMax[1] );
if( poBand->GetOverviewCount() > 0 )
printf( "Band has %d overviews.\n", poBand->GetOverviewCount() );
if( poBand->GetColorTable() != NULL )
printf( "Band has a color table with %d entries.\n",
poBand->GetColorTable()->GetColorEntryCount() );
//Close Dataset
GDALClose(poDataset);
//Exit
return 0;
}
}
示例7: getGDALRasterPtr
SEXP
RGDAL_GetColorInterp(SEXP sxpRasterBand) {
GDALRasterBand *pRasterBand = getGDALRasterPtr(sxpRasterBand);
GDALColorInterp eCI = pRasterBand->GetColorInterpretation();
return(mkString_safe(GDALGetColorInterpretationName(eCI)));
}
示例8: topLeft
int
NBHeightMapper::loadTiff(const std::string& file) {
#ifdef HAVE_GDAL
GDALAllRegister();
GDALDataset* poDataset = (GDALDataset*)GDALOpen(file.c_str(), GA_ReadOnly);
if (poDataset == 0) {
WRITE_ERROR("Cannot load GeoTIFF file.");
return 0;
}
const int xSize = poDataset->GetRasterXSize();
const int ySize = poDataset->GetRasterYSize();
double adfGeoTransform[6];
if (poDataset->GetGeoTransform(adfGeoTransform) == CE_None) {
Position topLeft(adfGeoTransform[0], adfGeoTransform[3]);
mySizeOfPixel.set(adfGeoTransform[1], adfGeoTransform[5]);
const double horizontalSize = xSize * mySizeOfPixel.x();
const double verticalSize = ySize * mySizeOfPixel.y();
myBoundary.add(topLeft);
myBoundary.add(topLeft.x() + horizontalSize, topLeft.y() + verticalSize);
} else {
WRITE_ERROR("Could not parse geo information from " + file + ".");
return 0;
}
const int picSize = xSize * ySize;
myRaster = (int16_t*)CPLMalloc(sizeof(int16_t) * picSize);
for (int i = 1; i <= poDataset->GetRasterCount(); i++) {
GDALRasterBand* poBand = poDataset->GetRasterBand(i);
if (poBand->GetColorInterpretation() != GCI_GrayIndex) {
WRITE_ERROR("Unknown color band in " + file + ".");
clearData();
break;
}
if (poBand->GetRasterDataType() != GDT_Int16) {
WRITE_ERROR("Unknown data type in " + file + ".");
clearData();
break;
}
assert(xSize == poBand->GetXSize() && ySize == poBand->GetYSize());
if (poBand->RasterIO(GF_Read, 0, 0, xSize, ySize, myRaster, xSize, ySize, GDT_Int16, 0, 0) == CE_Failure) {
WRITE_ERROR("Failure in reading " + file + ".");
clearData();
break;
}
}
GDALClose(poDataset);
return picSize;
#else
UNUSED_PARAMETER(file);
WRITE_ERROR("Cannot load GeoTIFF file since SUMO was compiled without GDAL support.");
return 0;
#endif
}
示例9: if
wxGISRasterLayer::wxGISRasterLayer(const wxString &sName, wxGISDataset* pwxGISDataset) : wxGISLayer(sName, pwxGISDataset)
{
wxGISRasterDataset* pwxGISRasterDataset = wxDynamicCast(pwxGISDataset, wxGISRasterDataset);
if(pwxGISRasterDataset)
{
if(m_sName.IsEmpty())
m_sName = pwxGISRasterDataset->GetName();
m_SpatialReference = pwxGISRasterDataset->GetSpatialReference();
m_FullEnvelope = pwxGISRasterDataset->GetEnvelope();
//TODO: load or get all renderers and check if i render can draw this dataset. If yes - set it as current
if(pwxGISRasterDataset->GetBandCount() >= 3)
{
m_pRenderer = new wxGISRasterRGBARenderer(pwxGISDataset);
}
else
{
GDALDataset* poGDALDataset = pwxGISRasterDataset->GetMainRaster();
if(!poGDALDataset)
poGDALDataset = pwxGISRasterDataset->GetRaster();
if(!poGDALDataset)
return;
GDALRasterBand* pBand = poGDALDataset->GetRasterBand(1);
GDALColorInterp eColorInterpretation = pBand->GetColorInterpretation();
if( eColorInterpretation == GCI_PaletteIndex )
{
m_pRenderer = new wxGISRasterRasterColormapRenderer(pwxGISDataset);
}
else if(pBand->GetRasterDataType() == GDT_Int32)
{
m_pRenderer = new wxGISRasterPackedRGBARenderer(pwxGISDataset);
}
else// if( eColorInterpretation == GCI_GrayIndex )
{
//TODO: else RasterStretchColorRampRenderer
m_pRenderer = new wxGISRasterGreyScaleRenderer(pwxGISDataset);
}
}
}
}
示例10: writeOutputLine
void PLCContext::writeOutputLine(int line, PLCLine *lineObj)
{
CPLAssert( outputDS != NULL );
int i, width = outputDS->GetRasterXSize();
for( i=0; i < outputDS->GetRasterCount(); i++ )
{
CPLErr eErr;
GDALRasterBand *band = outputDS->GetRasterBand(i+1);
if( band->GetColorInterpretation() == GCI_AlphaBand )
eErr = band->RasterIO(GF_Write, 0, line, width, 1,
lineObj->getAlpha(), width, 1, GDT_Byte,
0, 0);
else
eErr = band->RasterIO(GF_Write, 0, line, width, 1,
lineObj->getBand(i), width, 1, GDT_Int16,
0, 0);
if( eErr != CE_None )
exit(1);
if( sourceTraceDS != NULL )
{
eErr = sourceTraceDS->GetRasterBand(1)->
RasterIO(GF_Write, 0, line, width, 1,
lineObj->getSource(), width, 1, GDT_UInt16,
0, 0);
}
if( qualityDS != NULL )
{
eErr = qualityDS->GetRasterBand(1)->
RasterIO(GF_Write, 0, line, width, 1,
lineObj->getQuality(), width, 1, GDT_Float32,
0, 0);
}
}
}
示例11: readFrames
//.........这里部分代码省略.........
size_t nBlockXSize, nBlockYSize, nBands;
int bGotMin, bGotMax;
double adfMinMax[2];
float *pafScanline;
while( true ) {
pfs::FrameFile ff = it.getNextFrameFile();
if( ff.fh == NULL ) break; // No more frames
it.closeFrameFile( ff );
VERBOSE_STR << "reading file '" << ff.fileName << "'" << std::endl;
if( !( poDataset = (GDALDataset *) GDALOpen( ff.fileName, GA_ReadOnly ) ) ) {
std::cerr << "input does not seem to be in a format supported by GDAL" << std::endl;
throw QuietException();
}
VERBOSE_STR << "GDAL driver: " << poDataset->GetDriver()->GetDescription()
<< " / " << poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) << std::endl;
nBlockXSize = poDataset->GetRasterXSize();
nBlockYSize = poDataset->GetRasterYSize();
nBands = poDataset->GetRasterCount();
VERBOSE_STR << "Data size " << nBlockXSize << "x" << nBlockYSize << "x" << nBands << std::endl;
if( poDataset->GetProjectionRef() ) {
VERBOSE_STR << "Projection " << poDataset->GetProjectionRef() << std::endl;
}
if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None ) {
VERBOSE_STR << "Origin = (" << adfGeoTransform[0] << ", " << adfGeoTransform[3] << ")" << std::endl;
VERBOSE_STR << "Pixel Size = (" << adfGeoTransform[1] << ", " << adfGeoTransform[5] << ")" << std::endl;
}
if( nBlockXSize==0 || nBlockYSize==0
|| ( SIZE_MAX / nBlockYSize < nBlockXSize )
|| ( SIZE_MAX / (nBlockXSize * nBlockYSize ) < 4 ) ) {
std::cerr << "input data has invalid size" << std::endl;
throw QuietException();
}
if( !(pafScanline = (float *) CPLMalloc( sizeof(float) * nBlockXSize ) ) ) {
std::cerr << "not enough memory" << std::endl;
throw QuietException();
}
pfs::Frame *frame = pfsio.createFrame( nBlockXSize, nBlockYSize );
pfs::Channel *C[nBands];
char channel_name[32];
frame->getTags()->setString( "X-GDAL_DRIVER_SHORTNAME", poDataset->GetDriver()->GetDescription() );
frame->getTags()->setString( "X-GDAL_DRIVER_LONGNAME", poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
frame->getTags()->setString( "X-PROJECTION", poDataset->GetProjectionRef() );
if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None ) {
frame->getTags()->setString( "X-ORIGIN_X", stringify(adfGeoTransform[0]).c_str() );
frame->getTags()->setString( "X-ORIGIN_Y", stringify(adfGeoTransform[3]).c_str() );
frame->getTags()->setString( "X-PIXEL_WIDTH", stringify(adfGeoTransform[1]).c_str() );
frame->getTags()->setString( "X-PIXEL_HEIGHT", stringify(adfGeoTransform[5]).c_str() );
}
for ( size_t band = 1; band <= nBands; band++) {
size_t nBandXSize, nBandYSize;
VERBOSE_STR << "Band " << band << ": " << std::endl;
snprintf( channel_name, 32, "X-GDAL%zu", band );
C[band - 1] = frame->createChannel( channel_name );
poBand = poDataset->GetRasterBand( band );
nBandXSize = poBand->GetXSize();
nBandYSize = poBand->GetYSize();
VERBOSE_STR << " " << nBandXSize << "x" << nBandYSize << std::endl;
if( nBandXSize != (int)nBlockXSize || nBandYSize != (int)nBlockYSize ) {
std::cerr << "data in band " << band << " has different size" << std::endl;
throw QuietException();
}
VERBOSE_STR << " Type " << GDALGetDataTypeName( poBand->GetRasterDataType() ) << ", "
<< "Color Interpretation " << GDALGetColorInterpretationName( poBand->GetColorInterpretation() ) << std::endl;
adfMinMax[0] = poBand->GetMinimum( &bGotMin );
adfMinMax[1] = poBand->GetMaximum( &bGotMax );
if( ! (bGotMin && bGotMax) ) {
GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);
}
VERBOSE_STR << " Min " << adfMinMax[0] << ", Max " << adfMinMax[1] << std::endl;
C[band - 1]->getTags()->setString( "X-TYPE",
GDALGetDataTypeName( poBand->GetRasterDataType() ) );
C[band - 1]->getTags()->setString( "X-COLOR_INTERPRETATION",
GDALGetColorInterpretationName( poBand->GetColorInterpretation() ) );
C[band - 1]->getTags()->setString( "X-MIN", stringify(adfMinMax[0]).c_str() );
C[band - 1]->getTags()->setString( "X-MAX", stringify(adfMinMax[1]).c_str() );
for( size_t y = 0; y < nBlockYSize; y++ ) {
if( poBand->RasterIO( GF_Read, 0, y, nBlockXSize, 1, pafScanline,
nBlockXSize, 1, GDT_Float32, 0, 0) != CE_None ) {
std::cerr << "input error" << std::endl;
throw QuietException();
}
memcpy( C[band - 1]->getRawData() + y * nBlockXSize, pafScanline, nBlockXSize * sizeof(float) );
}
}
CPLFree( pafScanline );
GDALClose( poDataset );
const char *fileNameTag = strcmp( "-", ff.fileName )==0 ? "stdin" : ff.fileName;
frame->getTags()->setString( "FILE_NAME", fileNameTag );
pfsio.writeFrame( frame, stdout );
pfsio.freeFrame( frame );
}
}
示例12: loadImage
//.........这里部分代码省略.........
theProjection = srsProj;
}
isLatLon = (theSrs->IsGeographic() == TRUE);
}
}
if (theProjection.isEmpty()) {
theProjection = ProjectionChooser::getProjection(QCoreApplication::translate("ImportExportGdal", "Unable to set projection; please specify one"));
if (theProjection.isEmpty()) {
GDALClose((GDALDatasetH)poDataset);
return false;
}
}
qDebug( "Driver: %s/%s\n",
poDataset->GetDriver()->GetDescription(),
poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
qDebug( "Size is %dx%dx%d\n",
poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
poDataset->GetRasterCount() );
GdalAdapter::ImgType theType = GdalAdapter::Unknown;
int bandCount = poDataset->GetRasterCount();
int ixA = -1;
int ixR, ixG, ixB;
int ixH, ixS, ixL;
int ixC, ixM, ixY, ixK;
int ixYuvY, ixYuvU, ixYuvV;
double adfMinMax[2];
double UnknownUnit;
GDALColorTable* colTable = NULL;
for (int i=0; i<bandCount; ++i) {
GDALRasterBand *poBand = poDataset->GetRasterBand( i+1 );
GDALColorInterp bandtype = poBand->GetColorInterpretation();
qDebug() << "Band " << i+1 << " Color: " << GDALGetColorInterpretationName(poBand->GetColorInterpretation());
switch (bandtype)
{
case GCI_Undefined:
theType = GdalAdapter::Unknown;
int bGotMin, bGotMax;
adfMinMax[0] = poBand->GetMinimum( &bGotMin );
adfMinMax[1] = poBand->GetMaximum( &bGotMax );
if( ! (bGotMin && bGotMax) )
GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);
UnknownUnit = (adfMinMax[1] - adfMinMax[0]) / 256;
break;
case GCI_GrayIndex:
theType = GdalAdapter::GrayScale;
break;
case GCI_RedBand:
theType = GdalAdapter::Rgb;
ixR = i;
break;
case GCI_GreenBand:
theType = GdalAdapter::Rgb;
ixG = i;
break;
case GCI_BlueBand :
theType = GdalAdapter::Rgb;
ixB = i;
break;
case GCI_HueBand:
theType = GdalAdapter::Hsl;
ixH = i;
break;
示例13: generateTexture
void generateTexture(string fname, GLuint& tex, int bandnum)
{
if(bandnum <= 0 )
{
bandnum = 1;
}
GDALDataset *poDataset;
GDALAllRegister();
poDataset= (GDALDataset*) GDALOpen(fname.c_str(),GA_ReadOnly);
if(poDataset == NULL)
{
cout << "OUCH!" << endl;
//exit(0);
return;
}
cout << "Data size: " << GDALGetRasterXSize(poDataset) << " " << GDALGetRasterYSize(poDataset) << endl;
GDALRasterBand *poBand;
int nBlockXSize, nBlockYSize;
int bGotMin, bGotMax;
double adfMinMax[2];
int bands = poDataset->GetRasterCount();
bandnum = bandnum % bands + 1;
if(bandnum > bands)
{
bandnum = 1;
}
poBand = poDataset->GetRasterBand( bandnum );
poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );
printf( "Block=%dx%d Type=%s, ColorInterp=%s\n",
nBlockXSize, nBlockYSize,
GDALGetDataTypeName(poBand->GetRasterDataType()),
GDALGetColorInterpretationName(
poBand->GetColorInterpretation()) );
float max = adfMinMax[0] = poBand->GetMinimum( &bGotMin );
float min = adfMinMax[1] = poBand->GetMaximum( &bGotMax );
if( ! (bGotMin && bGotMax) )
GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);
int width = poBand->GetXSize();
int height = poBand->GetYSize();
float *pafScanline;
std::cout << "Before allocation" << adfMinMax[0] << " " << adfMinMax[1] << endl;
min = adfMinMax[0];
max = adfMinMax[1];
int dsize = 256;
pafScanline = (float *) CPLMalloc(sizeof(float)*512*512);
vector<vector<float>> out = vector<vector<float>>(height,vector<float> (width,0));
//vector<vector<unsigned char>> texs = vector<vector<unsigned char>>(height,vector<unsigned char> (width,0));
unsigned char texs[512*512];
poBand->RasterIO(GF_Read,0,0,width,height,pafScanline,512,512,GDT_Float32,0,0);
float no = poBand->GetNoDataValue();
cout << "After allocation" << endl;
for(int i = 0; i < 512; i++)
{
for(int j = 0; j < 512; j++)
{
//cout << i << j << endl << pafS;
if(pafScanline[i*width+j] != no)
{
// set tex val
texs[i*512+j] = (unsigned char)(255*((pafScanline[i*512+j] - min)/(max-min)));
//if((int)texs[i*width] < 0)
//cout << (int)texs[i*512 +j] << " " << pafScanline[i*512+j] << " " << no << " " << fname << " " << min << " " << max << endl;
}
else
{
// Set zero val
texs[i*512+j] = 0;
//cout << (int)texs[i*512 +j] << fname << endl;
}
//texs[i*512+j] = 255;
//ut[i][j] = pafScanline[i*width+j];
}
}
CPLFree(pafScanline);
//exit(0);
// Create a texture
glGenTextures(1,&tex);
glBindTexture(GL_TEXTURE_2D,tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 512,512, 0, GL_RED, GL_UNSIGNED_BYTE,texs);
GDALClose( (GDALDatasetH) poDataset);
return;
}
示例14: readRaster
int readRaster(const char* pszFilename)
{
// Set config options for GDAL (needs >= 2.0). Setting GTIFF_VIRTUAL_MEM_IO to "YES" can cause things bleed to
// swap if enough RAM is not available. Use "IF_ENOUGH_RAM" for safer performance if unsure. NOTE that faster
// mem io only works with *uncompressed* GeoTIFFs.
// New in GDAL 2.0, from https://2015.foss4g-na.org/sites/default/files/slides/GDAL%202.0%20overview.pdf
// GeoTIFF driver (with i7-4700 HQ (8 vCPUs)):with i7-4700 HQ (8 vCPUs)
// - Default: time ./testblockcache -ondisk: 7.5s
// - GTIFF_DIRECT_IO=YES: short circuit the block cache&libtiff for most RasterIO() operations (restricted to
// uncompressed stripped GeoTIFF). time ./testblockcache -ondisk: 2s
// - GTIFF_VIRTUAL_MEM_IO=YES: same as above, with tiled GeoTIFF as well. Uses memory-mapped file access. Linux
// only for now, 64bit recommended (could be extended to other platforms possible).
// time ./testblockcache -ondisk: 0.3s
//
CPLSetConfigOption("GTIFF_VIRTUAL_MEM_IO", "YES" );
// Open the dataset
GDALDataset *poDataset;
GDALAllRegister();
poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );
if( poDataset == NULL )
{
std::cout << "Dataset " << pszFilename << " could not be opened." << std::endl;
return -1;
}
else
{
GDALRasterBand *poBand;
int nBlockXSize, nBlockYSize;
int bGotMin, bGotMax;
double adfMinMax[2];
// Get raster band and its size
poBand = poDataset->GetRasterBand(1);
poBand->GetBlockSize( &nBlockXSize, &nBlockYSize);
std::cout << "Dataset: " << pszFilename << std::endl;
std::cout << "Block=" << nBlockXSize << "x" << nBlockYSize << " Type=" <<
GDALGetDataTypeName(poBand->GetRasterDataType()) << " ColorInterp=" <<
GDALGetColorInterpretationName(poBand->GetColorInterpretation()) << std::endl;
// Calculate some stats
adfMinMax[0] = poBand->GetMinimum(&bGotMin);
adfMinMax[1] = poBand->GetMaximum(&bGotMax);
if(!(bGotMin && bGotMax)) {
GDALComputeRasterMinMax((GDALRasterBandH) poBand, TRUE, adfMinMax);
}
std::cout << "Min=" << adfMinMax[0] << " Max=" << adfMinMax[1] << std::endl;
if(poBand->GetOverviewCount() > 0) {
std::cout << "Band has " << poBand->GetOverviewCount() << " overviews." << std::endl;
}
if( poBand->GetColorTable() != NULL ) {
std::cout << "Band has a color table with " << poBand->GetColorTable()->GetColorEntryCount() <<
" entries." << std::endl;
}
// Get the actual data
float *pafScanline;
int nXSize = poBand->GetXSize();
pafScanline = (float *) CPLMalloc(sizeof(float) * nXSize);
// RasterIO has a new argument psExtraArg in GDAL > 2.0. NOTE: that GDALRasterBand::ReadBlock() probably has
// better performance for reading the whole data at one go.
#ifdef USE_GDAL_2
GDALRasterIOExtraArg* arg = NULL;
poBand->RasterIO(GF_Read, 0, 0, nXSize, 1, pafScanline, nXSize, 1, GDT_Float3GDALRasterBand::ReadBlock 2,
0, 0, arg);
#else
poBand->RasterIO(GF_Read, 0, 0, nXSize, 1, pafScanline, nXSize, 1, GDT_Float32, 0, 0);
#endif
// ... do something with the data ...
// Free resources
CPLFree(pafScanline);
GDALClose((GDALDatasetH) poDataset);
std::cout << std::endl;
}
return 0;
}
示例15:
/**
* read data
*/
bool KGDAL2CV::readData(cv::Mat img){
// make sure the image is the proper size
if (img.size().height != m_height){
return false;
}
if (img.size().width != m_width){
return false;
}
// make sure the raster is alive
if (m_dataset == NULL || m_driver == NULL){
return false;
}
// set the image to zero
img = 0;
// iterate over each raster band
// note that OpenCV does bgr rather than rgb
int nChannels = m_dataset->GetRasterCount();
GDALColorTable* gdalColorTable = NULL;
if (m_dataset->GetRasterBand(1)->GetColorTable() != NULL){
gdalColorTable = m_dataset->GetRasterBand(1)->GetColorTable();
}
const GDALDataType gdalType = m_dataset->GetRasterBand(1)->GetRasterDataType();
int nRows, nCols;
//if (nChannels > img.channels()){
// nChannels = img.channels();
//}
for (int c = 0; c < img.channels(); c++){
int realBandIndex = c;
// get the GDAL Band
GDALRasterBand* band = m_dataset->GetRasterBand(c + 1);
//if (hasColorTable == false){
if (GCI_RedBand == band->GetColorInterpretation()) realBandIndex = 2;
if (GCI_GreenBand == band->GetColorInterpretation()) realBandIndex = 1;
if (GCI_BlueBand == band->GetColorInterpretation()) realBandIndex = 0;
//}
if (hasColorTable && gdalColorTable->GetPaletteInterpretation() == GPI_RGB) c = img.channels() - 1;
// make sure the image band has the same dimensions as the image
if (band->GetXSize() != m_width || band->GetYSize() != m_height){ return false; }
// grab the raster size
nRows = band->GetYSize();
nCols = band->GetXSize();
// create a temporary scanline pointer to store data
double* scanline = new double[nCols];
// iterate over each row and column
for (int y = 0; y<nRows; y++){
// get the entire row
band->RasterIO(GF_Read, 0, y, nCols, 1, scanline, nCols, 1, GDT_Float64, 0, 0);
// set inside the image
for (int x = 0; x<nCols; x++){
// set depending on image types
// given boost, I would use enable_if to speed up. Avoid for now.
if (hasColorTable == false){
write_pixel(scanline[x], gdalType, nChannels, img, y, x, realBandIndex);
}
else{
write_ctable_pixel(scanline[x], gdalType, gdalColorTable, img, y, x, c);
}
}
}
// delete our temp pointer
delete[] scanline;
}
return true;
}