32 #include <cpl_string.h>
35 #include <gdal_priv.h>
36 #include <ogr_spatialref.h>
48 : eStatus(
QVCT::UNDEFINED )
49 , poGDALDataset( NULL )
50 , pGDALProjectionTransformer( NULL )
51 , piGDALBandMap( NULL )
70 poGDALDataset = (GDALDataset*)GDALOpen( _rqsFileName.toLocal8Bit().constData(), GA_ReadOnly );
73 qCritical(
"ERROR[%s]: Failed to open dataset (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
81 OGRSpatialReference __oOGRSpatialReference;
82 char* __pcProjectionDefinitionString;
83 if( __oOGRSpatialReference.importFromProj4(
"+proj=longlat +ellps=WGS84 +datum=WGS84" ) != OGRERR_NONE
84 || __oOGRSpatialReference.exportToWkt( &__pcProjectionDefinitionString ) != OGRERR_NONE )
86 qCritical(
"ERROR[%s]: Failed to initialize projection system", Q_FUNC_INFO );
89 char **__ppcGDALProjectionOptions = CSLSetNameValue( NULL,
"DST_SRS", __pcProjectionDefinitionString );
90 CPLFree( __pcProjectionDefinitionString );
92 CSLDestroy( __ppcGDALProjectionOptions );
95 qCritical(
"ERROR[%s]: Failed to create projection transformer for dataset (%s)\n[...]: Use 'gdalinfo' to check your dataset projection data", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
101 int __iSuccess = TRUE;
103 if( __iSuccess != TRUE )
105 qCritical(
"ERROR[%s]: Failed to perform dataset projection transformation (%s)\n[...]: Use 'gdalinfo' to check your dataset projection data", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
108 if( __fdX < -180 || __fdX > 180 || __fdY < -90 || __fdY > 90 )
110 qCritical(
"ERROR[%s]: Dataset projection transformation leads to invalid results (%s)\n[...]: Use 'gdalinfo' to check your dataset projection data", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
115 bool __bHasGDALBandA =
false, __bHasGDALBandR =
false, __bHasGDALBandG =
false, __bHasGDALBandB =
false;
116 bool __bHasGDALBandY =
false, __bHasGDALBandCb =
false, __bHasGDALBandCr =
false;
117 GDALRasterBand* __poGDALRasterBand = NULL;
118 GDALColorTable* __poGDALColorTable = NULL;
120 switch( __iGDALBandCount )
124 qCritical(
"ERROR[%s]: Dataset has no raster band (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
129 if( !__poGDALRasterBand )
131 qCritical(
"ERROR[%s]: Failed to retrieve raster band (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
134 switch( __poGDALRasterBand->GetColorInterpretation() )
138 switch( __poGDALRasterBand->GetRasterDataType() )
148 qCritical(
"ERROR[%s]: Unsupported data type (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
153 case GCI_PaletteIndex:
154 __poGDALColorTable = __poGDALRasterBand->GetColorTable();
155 if( !__poGDALColorTable )
157 qCritical(
"ERROR[%s]: Failed to retrieve color table (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
160 switch( __poGDALColorTable->GetPaletteInterpretation() )
167 qCritical(
"ERROR[%s]: Unsupported palette interpretation for single-band dataset (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
173 qCritical(
"ERROR[%s]: Unsupported color interpretation for single-band dataset (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
180 for(
int __iGDALBandIndex = 1; __iGDALBandIndex <= __iGDALBandCount; __iGDALBandIndex++ )
182 __poGDALRasterBand =
poGDALDataset->GetRasterBand( __iGDALBandIndex );
183 if( !__poGDALRasterBand )
185 qCritical(
"ERROR[%s]: Failed to retrieve raster band (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
188 switch( __poGDALRasterBand->GetColorInterpretation() )
190 case GCI_AlphaBand: __bHasGDALBandA =
true;
break;
191 case GCI_RedBand: __bHasGDALBandR =
true;
break;
192 case GCI_GreenBand: __bHasGDALBandG =
true;
break;
193 case GCI_BlueBand: __bHasGDALBandB =
true;
break;
194 case GCI_YCbCr_YBand: __bHasGDALBandY =
true;
break;
195 case GCI_YCbCr_CbBand: __bHasGDALBandCb =
true;
break;
196 case GCI_YCbCr_CrBand: __bHasGDALBandCr =
true;
break;
200 if( __bHasGDALBandR && __bHasGDALBandG && __bHasGDALBandB )
204 else if( __bHasGDALBandY && __bHasGDALBandCb && __bHasGDALBandCr )
208 qCritical(
"ERROR[%s]: YCbCr color interpretation not implemented (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
213 qCritical(
"ERROR[%s]: Unsupported color interpretations for multi-band dataset (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
220 const GDALColorEntry* __poGDALColorEntry;
234 for(
int __iColorComponent = 0; __iColorComponent < 256; __iColorComponent++ )
236 qColorTable.append( qRgba( __iColorComponent, __iColorComponent, __iColorComponent, 255 ) );
241 for(
int __iPaletteIndex = 0; __iPaletteIndex < __poGDALColorTable->GetColorEntryCount(); __iPaletteIndex++ )
243 __poGDALColorEntry = __poGDALColorTable->GetColorEntry( __iPaletteIndex );
244 if( !__poGDALColorEntry )
246 qCritical(
"ERROR[%s]: Failed to retrieve color table entry (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
249 qColorTable.append( qRgb( __poGDALColorEntry->c1, __poGDALColorEntry->c1, __poGDALColorEntry->c1 ) );
254 for(
int __iPaletteIndex = 0; __iPaletteIndex < __poGDALColorTable->GetColorEntryCount(); __iPaletteIndex++ )
256 __poGDALColorEntry = __poGDALColorTable->GetColorEntry( __iPaletteIndex );
257 if( !__poGDALColorEntry )
259 qCritical(
"ERROR[%s]: Failed to retrieve color table entry (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
262 qColorTable.append( qRgba( __poGDALColorEntry->c1, __poGDALColorEntry->c2, __poGDALColorEntry->c3, __poGDALColorEntry->c4 ) );
267 for(
int __iPaletteIndex = 0; __iPaletteIndex < __poGDALColorTable->GetColorEntryCount(); __iPaletteIndex++ )
269 __poGDALColorEntry = __poGDALColorTable->GetColorEntry( __iPaletteIndex );
270 if( !__poGDALColorEntry )
272 qCritical(
"ERROR[%s]: Failed to retrieve color table entry (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
275 qColorTable.append( QColor::fromCmyk( __poGDALColorEntry->c1, __poGDALColorEntry->c2, __poGDALColorEntry->c3, __poGDALColorEntry->c4 ).rgb() );
280 for(
int __iPaletteIndex = 0; __iPaletteIndex < __poGDALColorTable->GetColorEntryCount(); __iPaletteIndex++ )
282 __poGDALColorEntry = __poGDALColorTable->GetColorEntry( __iPaletteIndex );
283 if( !__poGDALColorEntry )
285 qCritical(
"ERROR[%s]: Failed to retrieve color table entry (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
288 qColorTable.append( QColor::fromHsl( __poGDALColorEntry->c1, __poGDALColorEntry->c3, __poGDALColorEntry->c2 ).rgb() );
294 QRgb __qRgbPixel = qRgba( GCI_RedBand, GCI_GreenBand, GCI_BlueBand, GCI_AlphaBand );
295 for(
int __iGDALBandIndex = 1; __iGDALBandIndex <= __iGDALBandCount; __iGDALBandIndex++ )
297 GDALColorInterp __eGDALColorInterp =
poGDALDataset->GetRasterBand( __iGDALBandIndex )->GetColorInterpretation();
298 switch( __eGDALColorInterp )
304 for(
int __iGDALBandOffset = 0; __iGDALBandOffset < 4 ; __iGDALBandOffset++ )
305 if( __eGDALColorInterp == *(((quint8 *)&__qRgbPixel)+__iGDALBandOffset) )
316 QRgb __qRgbPixel = qRgba( GCI_RedBand, GCI_GreenBand, GCI_BlueBand, GCI_AlphaBand );
317 for(
int __iGDALBandIndex = 1; __iGDALBandIndex <= __iGDALBandCount; __iGDALBandIndex++ )
319 GDALColorInterp __eGDALColorInterp =
poGDALDataset->GetRasterBand( __iGDALBandIndex )->GetColorInterpretation();
320 switch( __eGDALColorInterp )
325 for(
int __iGDALBandOffset = 0; __iGDALBandOffset < 4 ; __iGDALBandOffset++ )
326 if( __eGDALColorInterp == *(((quint8 *)&__qRgbPixel)+__iGDALBandOffset) )
337 qCritical(
"ERROR[%s]: YCbCr color interpretation not implemented (%s)", Q_FUNC_INFO, qPrintable( _rqsFileName ) );
360 double __fdX = (double)_rqPointFDatPosition.x(), __fdY = (double)_rqPointFDatPosition.y(), __fdZ = 0;
361 int __iSuccess = TRUE;
363 if( __iSuccess != TRUE )
return __oGeoPosition;
365 return __oGeoPosition;
370 QPointF __qPointFDatPosition;
375 int __iSuccess = TRUE;
377 if( __iSuccess != TRUE )
return __qPointFDatPosition;
378 __qPointFDatPosition.setX( __fdX );
379 __qPointFDatPosition.setY( __fdY );
380 return __qPointFDatPosition;
385 QPointF __qPointFDatPositionNorth( _rqPointFDatPosition.x(), _rqPointFDatPosition.y() - 50.0 );
386 QPointF __qPointFDatPositionSouth( _rqPointFDatPosition.x(), _rqPointFDatPosition.y() + 50.0 );
392 void CChartGDAL::draw( QPainter* _pqPainter,
const QPointF& _rqPointFDatPosition,
double _fdZoom )
396 if( _fdZoom <= 0 ) _fdZoom = 1;
398 QSize __qSizePixmapWished( _pqPainter->device()->width(), _pqPainter->device()->height() );
415 QSize __qSizeGDALWished( (
double)__qSizePixmapWished.width() / _fdZoom + 0.5, (
double)__qSizePixmapWished.height() / _fdZoom + 0.5 );
420 QRect __qRectGDALActual( __qPointDatPositionGDALWished.x(), __qPointDatPositionGDALWished.y(), __qSizeGDALWished.width(), __qSizeGDALWished.height() );
421 __qRectGDALActual = __qRectGDALActual.intersected(
qRectGeometry );
423 QSize __qSizePixmapActual( (
double)__qRectGDALActual.width() * _fdZoom + 0.5, (
double)__qRectGDALActual.height() * _fdZoom + 0.5 );
426 QPoint __qPointPixmapActual( (
double)( __qRectGDALActual.topLeft().x() - __qPointDatPositionGDALWished.x() ) * _fdZoom + 0.5, (
double)( __qRectGDALActual.topLeft().y() - __qPointDatPositionGDALWished.y() ) * _fdZoom + 0.5 );
431 qPointFDatPositionActual.setX( (
double)__qRectGDALActual.topLeft().x() + ( (
double)__qSizePixmapWished.width() / _fdZoom )/2.0 - ( (
double)__qPointPixmapActual.x() / _fdZoom ) );
432 qPointFDatPositionActual.setY( (
double)__qRectGDALActual.topLeft().y() + ( (
double)__qSizePixmapWished.height() / _fdZoom )/2.0 - ( (
double)__qPointPixmapActual.y() / _fdZoom ) );
438 QSize __qSizeImageActual( ( __qSizePixmapActual.width() + 3 ) & 0xFFFFFFFC, __qSizePixmapActual.height() );
442 if( __qSizePixmapActual.isValid() )
450 QImage __qImage( __qSizeImageActual, QImage::Format_Indexed8 );
453 QVector<quint16> __qVector( __qImage.width()*__qImage.height() );
454 GDALRasterBand* __poGDALRasterBand =
poGDALDataset->GetRasterBand( 1 );
455 CPLErr tCPLErr = __poGDALRasterBand->RasterIO( GF_Read,
456 __qRectGDALActual.topLeft().x(), __qRectGDALActual.topLeft().y(),
457 __qRectGDALActual.width(), __qRectGDALActual.height(),
459 __qSizePixmapActual.width(), __qSizePixmapActual.height(),
461 sizeof(quint16),
sizeof(quint16)*__qImage.width() );
462 if( tCPLErr != CE_None )
464 qCritical(
"ERROR[%s]: Failed to rasterize typed dataset", Q_FUNC_INFO );
469 __qPainter.drawImage( __qPointPixmapActual, __qImage );
475 QImage __qImage( __qSizeImageActual, QImage::Format_Indexed8 );
478 QVector<qint16> __qVector( __qImage.width()*__qImage.height() );
479 GDALRasterBand* __poGDALRasterBand =
poGDALDataset->GetRasterBand( 1 );
480 CPLErr tCPLErr = __poGDALRasterBand->RasterIO( GF_Read,
481 __qRectGDALActual.topLeft().x(), __qRectGDALActual.topLeft().y(),
482 __qRectGDALActual.width(), __qRectGDALActual.height(),
484 __qSizePixmapActual.width(), __qSizePixmapActual.height(),
486 sizeof(qint16),
sizeof(qint16)*__qImage.width() );
487 if( tCPLErr != CE_None )
489 qCritical(
"ERROR[%s]: Failed to rasterize typed dataset", Q_FUNC_INFO );
494 __qPainter.drawImage( __qPointPixmapActual, __qImage );
500 QImage __qImage( __qSizeImageActual, QImage::Format_Indexed8 );
503 QVector<quint32> __qVector( __qImage.width()*__qImage.height() );
504 GDALRasterBand* __poGDALRasterBand =
poGDALDataset->GetRasterBand( 1 );
505 CPLErr tCPLErr = __poGDALRasterBand->RasterIO( GF_Read,
506 __qRectGDALActual.topLeft().x(), __qRectGDALActual.topLeft().y(),
507 __qRectGDALActual.width(), __qRectGDALActual.height(),
509 __qSizePixmapActual.width(), __qSizePixmapActual.height(),
511 sizeof(quint32),
sizeof(quint32)*__qImage.width() );
512 if( tCPLErr != CE_None )
514 qCritical(
"ERROR[%s]: Failed to rasterize typed dataset", Q_FUNC_INFO );
519 __qPainter.drawImage( __qPointPixmapActual, __qImage );
525 QImage __qImage( __qSizeImageActual, QImage::Format_Indexed8 );
528 QVector<qint32> __qVector( __qImage.width()*__qImage.height() );
529 GDALRasterBand* __poGDALRasterBand =
poGDALDataset->GetRasterBand( 1 );
530 CPLErr tCPLErr = __poGDALRasterBand->RasterIO( GF_Read,
531 __qRectGDALActual.topLeft().x(), __qRectGDALActual.topLeft().y(),
532 __qRectGDALActual.width(), __qRectGDALActual.height(),
534 __qSizePixmapActual.width(), __qSizePixmapActual.height(),
536 sizeof(qint32),
sizeof(qint32)*__qImage.width() );
537 if( tCPLErr != CE_None )
539 qCritical(
"ERROR[%s]: Failed to rasterize typed dataset", Q_FUNC_INFO );
544 __qPainter.drawImage( __qPointPixmapActual, __qImage );
550 QImage __qImage( __qSizeImageActual, QImage::Format_Indexed8 );
553 QVector<float> __qVector( __qImage.width()*__qImage.height() );
554 GDALRasterBand* __poGDALRasterBand =
poGDALDataset->GetRasterBand( 1 );
555 CPLErr tCPLErr = __poGDALRasterBand->RasterIO( GF_Read,
556 __qRectGDALActual.topLeft().x(), __qRectGDALActual.topLeft().y(),
557 __qRectGDALActual.width(), __qRectGDALActual.height(),
559 __qSizePixmapActual.width(), __qSizePixmapActual.height(),
561 sizeof(
float),
sizeof(
float)*__qImage.width() );
562 if( tCPLErr != CE_None )
564 qCritical(
"ERROR[%s]: Failed to rasterize typed dataset", Q_FUNC_INFO );
569 __qPainter.drawImage( __qPointPixmapActual, __qImage );
575 QImage __qImage( __qSizeImageActual, QImage::Format_Indexed8 );
578 QVector<double> __qVector( __qImage.width()*__qImage.height() );
579 GDALRasterBand* __poGDALRasterBand =
poGDALDataset->GetRasterBand( 1 );
580 CPLErr tCPLErr = __poGDALRasterBand->RasterIO( GF_Read,
581 __qRectGDALActual.topLeft().x(), __qRectGDALActual.topLeft().y(),
582 __qRectGDALActual.width(), __qRectGDALActual.height(),
584 __qSizePixmapActual.width(), __qSizePixmapActual.height(),
586 sizeof(
double),
sizeof(
double)*__qImage.width() );
587 if( tCPLErr != CE_None )
589 qCritical(
"ERROR[%s]: Failed to rasterize typed dataset", Q_FUNC_INFO );
594 __qPainter.drawImage( __qPointPixmapActual, __qImage );
604 QImage __qImage( __qSizeImageActual, QImage::Format_Indexed8 );
607 GDALRasterBand* __poGDALRasterBand =
poGDALDataset->GetRasterBand( 1 );
608 CPLErr tCPLErr = __poGDALRasterBand->RasterIO( GF_Read,
609 __qRectGDALActual.topLeft().x(), __qRectGDALActual.topLeft().y(),
610 __qRectGDALActual.width(), __qRectGDALActual.height(),
612 __qSizePixmapActual.width(), __qSizePixmapActual.height(),
614 1, __qImage.width() );
615 if( tCPLErr != CE_None )
617 qCritical(
"ERROR[%s]: Failed to rasterize palette dataset", Q_FUNC_INFO );
621 __qPainter.drawImage( __qPointPixmapActual, __qImage );
627 QImage __qImage( __qSizeImageActual, QImage::Format_ARGB32 );
630 __qRectGDALActual.topLeft().x(), __qRectGDALActual.topLeft().y(),
631 __qRectGDALActual.width(), __qRectGDALActual.height(),
633 __qSizePixmapActual.width(), __qSizePixmapActual.height(),
636 if( tCPLErr != CE_None )
638 qCritical(
"ERROR[%s]: Failed to rasterize ARGB dataset", Q_FUNC_INFO );
642 __qPainter.drawImage( __qPointPixmapActual, __qImage );
648 QImage __qImage( __qSizeImageActual, QImage::Format_RGB32 );
651 __qRectGDALActual.topLeft().x(), __qRectGDALActual.topLeft().y(),
652 __qRectGDALActual.width(), __qRectGDALActual.height(),
654 __qSizePixmapActual.width(), __qSizePixmapActual.height(),
657 if( tCPLErr != CE_None )
659 qCritical(
"ERROR[%s]: Failed to rasterize ARGB dataset", Q_FUNC_INFO );
663 __qPainter.drawImage( __qPointPixmapActual, __qImage );
669 qCritical(
"ERROR[%s]: YCbCr color interpretation not implemented", Q_FUNC_INFO );
692 uchar* __pqImageBit = _pqImage->bits();
693 const quint16* __pqVectorBit = _rqVector.data();
694 for(
int x=_pqImage->width()-1; x>=0; x-- )
695 for(
int y=_pqImage->height()-1; y>=0; y-- )
696 *__pqImageBit++ = *__pqVectorBit++ >> 8;
701 uchar* __pqImageBit = _pqImage->bits();
702 const qint16* __pqVectorBit = _rqVector.data();
703 for(
int x=_pqImage->width()-1; x>=0; x-- )
704 for(
int y=_pqImage->height()-1; y>=0; y-- )
705 *__pqImageBit++ = ( *__pqVectorBit++ >> 8 ) + 127;
710 uchar* __pqImageBit = _pqImage->bits();
711 const quint32* __pqVectorBit = _rqVector.data();
712 for(
int x=_pqImage->width()-1; x>=0; x-- )
713 for(
int y=_pqImage->height()-1; y>=0; y-- )
714 *__pqImageBit++ = *__pqVectorBit++ >> 24;
719 uchar* __pqImageBit = _pqImage->bits();
720 const qint32* __pqVectorBit = _rqVector.data();
721 for(
int x=_pqImage->width()-1; x>=0; x-- )
722 for(
int y=_pqImage->height()-1; y>=0; y-- )
723 *__pqImageBit++ = ( *__pqVectorBit++ >> 24 ) + 127;
728 uchar* __pqImageBit = _pqImage->bits();
729 const float* __pqVectorBit = _rqVector.data();
730 for(
int x=_pqImage->width()-1; x>=0; x-- )
731 for(
int y=_pqImage->height()-1; y>=0; y-- )
732 *__pqImageBit++ = *__pqVectorBit++;
737 uchar* __pqImageBit = _pqImage->bits();
738 const double* __pqVectorBit = _rqVector.data();
739 for(
int x=_pqImage->width()-1; x>=0; x-- )
740 for(
int y=_pqImage->height()-1; y>=0; y-- )
741 *__pqImageBit++ = *__pqVectorBit++;
virtual void rasterBuffer(QImage *_pqImage, const QVector< quint16 > &_rqVector) const
Rasterizes UInt16 buffer.
QString qsFileName
Chart (GDAL dataset) file name.
void open(const QString &_rqsFileName)
Opens the underlying GDAL dataset.
void draw(QPainter *_pqPainter, const QPointF &_rqPointFDatPosition, double _fdZoom)
Draws chart, centered on given chart (GDAL dataset) point [px] and zoomed according to given factor.
CDataPosition toGeoPosition(const QPointF &_rqPointFDatPosition) const
Converts the given chart pixel position to geographical position.
void move(const QPointF &_rqPointFDatPosition, double _fdZoom)
Moves the chart, centered on given chart (GDAL dataset) point [px] and zoomed according to given fact...
void * pGDALProjectionTransformer
Underlying GDAL dataset projection transformer (to internal WGS84)
int * piGDALBandMap
Underlying GDAL dataset band mapping (for ARGB/RGB/YUV color encodings)
double getResolution(const QPointF &_rqPointFDatPosition) const
Returns the resolution at given chart (GDAL dataset) point [px], in meters per pixel [m/px].
QPointF qPointFDatPositionActual
Last drawn chart (GDAL dataset) center pixel position (actually rendered)
double fdZoomActual
Last drawn chart (GDAL dataset) zoom factor (actually rendered)
QRect qRectGeometry
Chart (GDAL dataset) geometry (size)
EColorEncoding eColorEncoding
Chart (GDAL dataset) color encoding.
QPixmap qPixmapBuffer
Pixmap buffer where the chart (GDAL dataset) is rendered (rasterized)
QPointF toDatPosition(const CDataPosition &_roGeoPosition) const
Converts the given (geographical) position [long,lat,elev] to chart (GDAL dataset) point [px].
double fdZoomWished
Last drawn chart (GDAL dataset) zoom factor (requested by user)
QVCT::EStatus eStatus
Chart internal status.
GDALDataset * poGDALDataset
Underlying GDAL dataset.
QVector< QRgb > qColorTable
Color table (for GRAY/PALETTE_* color encodings)
QPointF qPointFDatPositionWished
Last drawn chart (GDAL dataset) center pixel position (requested by user)
(Geographical) Position data [long,lat,elev]
double getLongitude() const
Returns this position's longitude, in degrees.
double getElevation() const
Returns this position's elevation, in meters.
double getLatitude() const
Returns this position's latitude, in degrees.
static const CDataPosition UNDEFINED
Specific value for an undefined position.
void setPosition(double _fdLongitude, double _fdLatitude, double _fdElevation=UNDEFINED_ELEVATION)
Sets new coordinates.
static constexpr double UNDEFINED_ELEVATION
Specific value for an undefined elevation.
Global/base application container.