-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsgdalproviderbase.cpp
350 lines (313 loc) · 12.3 KB
/
qgsgdalproviderbase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/***************************************************************************
qgsgdalproviderbase.cpp - Common base class for GDAL and WCS provider
-------------------
begin : November, 2010
copyright : (C) 2010 by Radim Blazek
email : radim dot blazek at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef CPL_SUPRESS_CPLUSPLUS
#define CPL_SUPRESS_CPLUSPLUS
#endif
#include "cpl_conv.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgsgdalproviderbase.h"
#include <QSettings>
QgsGdalProviderBase::QgsGdalProviderBase()
{
// first get the GDAL driver manager
QgsGdalProviderBase::registerGdalDrivers();
}
/**
* @param theBandNumber the number of the band for which you want a color table
* @param theList a pointer the object that will hold the color table
* @return true of a color table was able to be read, false otherwise
*/
QList<QgsColorRampShader::ColorRampItem> QgsGdalProviderBase::colorTable( GDALDatasetH gdalDataset, int theBandNumber )const
{
QList<QgsColorRampShader::ColorRampItem> ct;
//Invalid band number, segfault prevention
if ( 0 >= theBandNumber )
{
QgsDebugMsg( "Invalid parameter" );
return ct;
}
GDALRasterBandH myGdalBand = GDALGetRasterBand( gdalDataset, theBandNumber );
GDALColorTableH myGdalColorTable = GDALGetRasterColorTable( myGdalBand );
if ( myGdalColorTable )
{
QgsDebugMsg( "Color table found" );
// load category labels
char ** categoryNames = GDALGetRasterCategoryNames( myGdalBand );
QVector<QString> labels;
if ( categoryNames )
{
int i = 0;
while ( categoryNames[i] )
{
labels.append( QString( categoryNames[i] ) );
i++;
}
}
int myEntryCount = GDALGetColorEntryCount( myGdalColorTable );
GDALColorInterp myColorInterpretation = GDALGetRasterColorInterpretation( myGdalBand );
QgsDebugMsg( "Color Interpretation: " + QString::number( static_cast< int >( myColorInterpretation ) ) );
GDALPaletteInterp myPaletteInterpretation = GDALGetPaletteInterpretation( myGdalColorTable );
QgsDebugMsg( "Palette Interpretation: " + QString::number( static_cast< int >( myPaletteInterpretation ) ) );
const GDALColorEntry* myColorEntry = nullptr;
for ( int myIterator = 0; myIterator < myEntryCount; myIterator++ )
{
myColorEntry = GDALGetColorEntry( myGdalColorTable, myIterator );
if ( !myColorEntry )
{
continue;
}
else
{
QString label = labels.value( myIterator );
if ( label.isEmpty() )
{
label = QString::number( myIterator );
}
//Branch on the color interpretation type
if ( myColorInterpretation == GCI_GrayIndex )
{
QgsColorRampShader::ColorRampItem myColorRampItem;
myColorRampItem.value = static_cast< double >( myIterator );
myColorRampItem.label = label;
myColorRampItem.color = QColor::fromRgb( myColorEntry->c1, myColorEntry->c1, myColorEntry->c1, myColorEntry->c4 );
ct.append( myColorRampItem );
}
else if ( myColorInterpretation == GCI_PaletteIndex )
{
QgsColorRampShader::ColorRampItem myColorRampItem;
myColorRampItem.value = static_cast< double >( myIterator );
myColorRampItem.label = label;
//Branch on palette interpretation
if ( myPaletteInterpretation == GPI_RGB )
{
myColorRampItem.color = QColor::fromRgb( myColorEntry->c1, myColorEntry->c2, myColorEntry->c3, myColorEntry->c4 );
}
else if ( myPaletteInterpretation == GPI_CMYK )
{
myColorRampItem.color = QColor::fromCmyk( myColorEntry->c1, myColorEntry->c2, myColorEntry->c3, myColorEntry->c4 );
}
else if ( myPaletteInterpretation == GPI_HLS )
{
myColorRampItem.color = QColor::fromHsv( myColorEntry->c1, myColorEntry->c3, myColorEntry->c2, myColorEntry->c4 );
}
else
{
myColorRampItem.color = QColor::fromRgb( myColorEntry->c1, myColorEntry->c1, myColorEntry->c1, myColorEntry->c4 );
}
ct.append( myColorRampItem );
}
else
{
QgsDebugMsg( "Color interpretation type not supported yet" );
return ct;
}
}
}
}
else
{
QgsDebugMsg( "No color table found for band " + QString::number( theBandNumber ) );
return ct;
}
QgsDebugMsg( "Color table loaded successfully" );
return ct;
}
QGis::DataType QgsGdalProviderBase::dataTypeFromGdal( const GDALDataType theGdalDataType ) const
{
switch ( theGdalDataType )
{
case GDT_Byte:
return QGis::Byte;
case GDT_UInt16:
return QGis::UInt16;
case GDT_Int16:
return QGis::Int16;
case GDT_UInt32:
return QGis::UInt32;
case GDT_Int32:
return QGis::Int32;
case GDT_Float32:
return QGis::Float32;
case GDT_Float64:
return QGis::Float64;
case GDT_CInt16:
return QGis::CInt16;
case GDT_CInt32:
return QGis::CInt32;
case GDT_CFloat32:
return QGis::CFloat32;
case GDT_CFloat64:
return QGis::CFloat64;
case GDT_Unknown:
case GDT_TypeCount:
return QGis::UnknownDataType;
}
return QGis::UnknownDataType;
}
int QgsGdalProviderBase::colorInterpretationFromGdal( const GDALColorInterp gdalColorInterpretation ) const
{
switch ( gdalColorInterpretation )
{
case GCI_GrayIndex:
return QgsRaster::GrayIndex;
case GCI_PaletteIndex:
return QgsRaster::PaletteIndex;
case GCI_RedBand:
return QgsRaster::RedBand;
case GCI_GreenBand:
return QgsRaster::GreenBand;
case GCI_BlueBand:
return QgsRaster::BlueBand;
case GCI_AlphaBand:
return QgsRaster::AlphaBand;
case GCI_HueBand:
return QgsRaster::HueBand;
case GCI_SaturationBand:
return QgsRaster::SaturationBand;
case GCI_LightnessBand:
return QgsRaster::LightnessBand;
case GCI_CyanBand:
return QgsRaster::CyanBand;
case GCI_MagentaBand:
return QgsRaster::MagentaBand;
case GCI_YellowBand:
return QgsRaster::YellowBand;
case GCI_BlackBand:
return QgsRaster::BlackBand;
case GCI_YCbCr_YBand:
return QgsRaster::YCbCr_YBand;
case GCI_YCbCr_CbBand:
return QgsRaster::YCbCr_CbBand;
case GCI_YCbCr_CrBand:
return QgsRaster::YCbCr_CrBand;
case GCI_Undefined:
return QgsRaster::UndefinedColorInterpretation;
}
return QgsRaster::UndefinedColorInterpretation;
}
void QgsGdalProviderBase::registerGdalDrivers()
{
GDALAllRegister();
QSettings mySettings;
QString myJoinedList = mySettings.value( "gdal/skipList", "" ).toString();
if ( !myJoinedList.isEmpty() )
{
QStringList myList = myJoinedList.split( ' ' );
for ( int i = 0; i < myList.size(); ++i )
{
QgsApplication::skipGdalDriver( myList.at( i ) );
}
QgsApplication::applyGdalSkippedDrivers();
}
}
QgsRectangle QgsGdalProviderBase::extent( GDALDatasetH gdalDataset )const
{
double myGeoTransform[6];
bool myHasGeoTransform = GDALGetGeoTransform( gdalDataset, myGeoTransform ) == CE_None;
if ( !myHasGeoTransform )
{
// Initialise the affine transform matrix
myGeoTransform[0] = 0;
myGeoTransform[1] = 1;
myGeoTransform[2] = 0;
myGeoTransform[3] = 0;
myGeoTransform[4] = 0;
myGeoTransform[5] = -1;
}
// Use the affine transform to get geo coordinates for
// the corners of the raster
double myXMax = myGeoTransform[0] +
GDALGetRasterXSize( gdalDataset ) * myGeoTransform[1] +
GDALGetRasterYSize( gdalDataset ) * myGeoTransform[2];
double myYMin = myGeoTransform[3] +
GDALGetRasterXSize( gdalDataset ) * myGeoTransform[4] +
GDALGetRasterYSize( gdalDataset ) * myGeoTransform[5];
QgsRectangle extent( myGeoTransform[0], myYMin, myXMax, myGeoTransform[3] );
return extent;
}
GDALDatasetH QgsGdalProviderBase::gdalOpen( const char *pszFilename, GDALAccess eAccess )
{
// See http://hub.qgis.org/issues/8356 and http://trac.osgeo.org/gdal/ticket/5170
#if GDAL_VERSION_MAJOR == 1 && ( (GDAL_VERSION_MINOR == 9 && GDAL_VERSION_REV <= 2) || (GDAL_VERSION_MINOR == 10 && GDAL_VERSION_REV <= 0) )
char* pszOldVal = CPLStrdup( CPLGetConfigOption( "VSI_CACHE", "FALSE" ) );
CPLSetThreadLocalConfigOption( "VSI_CACHE", "FALSE" );
QgsDebugMsg( "Disabled VSI_CACHE" );
#endif
GDALDatasetH hDS = GDALOpen( pszFilename, eAccess );
#if GDAL_VERSION_MAJOR == 1 && ( (GDAL_VERSION_MINOR == 9 && GDAL_VERSION_REV <= 2) || (GDAL_VERSION_MINOR == 10 && GDAL_VERSION_REV <= 0) )
CPLSetThreadLocalConfigOption( "VSI_CACHE", pszOldVal );
CPLFree( pszOldVal );
QgsDebugMsg( "Reset VSI_CACHE" );
#endif
return hDS;
}
int CPL_STDCALL _gdalProgressFnWithFeedback( double dfComplete, const char *pszMessage, void *pProgressArg )
{
Q_UNUSED( dfComplete );
Q_UNUSED( pszMessage );
QgsRasterBlockFeedback* feedback = static_cast<QgsRasterBlockFeedback*>( pProgressArg );
return !feedback->isCancelled();
}
CPLErr QgsGdalProviderBase::gdalRasterIO( GDALRasterBandH hBand, GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize, void * pData, int nBufXSize, int nBufYSize, GDALDataType eBufType, int nPixelSpace, int nLineSpace, QgsRasterBlockFeedback* feedback )
{
// See http://hub.qgis.org/issues/8356 and http://trac.osgeo.org/gdal/ticket/5170
#if GDAL_VERSION_MAJOR == 1 && ( (GDAL_VERSION_MINOR == 9 && GDAL_VERSION_REV <= 2) || (GDAL_VERSION_MINOR == 10 && GDAL_VERSION_REV <= 0) )
char* pszOldVal = CPLStrdup( CPLGetConfigOption( "VSI_CACHE", "FALSE" ) );
CPLSetThreadLocalConfigOption( "VSI_CACHE", "FALSE" );
QgsDebugMsg( "Disabled VSI_CACHE" );
#endif
#if GDAL_VERSION_MAJOR >= 2
GDALRasterIOExtraArg extra;
INIT_RASTERIO_EXTRA_ARG( extra );
if ( 0 && feedback ) // disabled!
{
// Currently the cancellation is disabled... When RasterIO call is cancelled,
// GDAL returns CE_Failure with error code = 0 (CPLE_None), however one would
// expect to get CPLE_UserInterrupt to clearly identify that the failure was
// caused by the cancellation and not that something dodgy is going on.
// Are both error codes acceptable?
extra.pfnProgress = _gdalProgressFnWithFeedback;
extra.pProgressData = ( void* ) feedback;
}
CPLErr err = GDALRasterIOEx( hBand, eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, eBufType, nPixelSpace, nLineSpace, &extra );
#else
Q_UNUSED( feedback );
CPLErr err = GDALRasterIO( hBand, eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, eBufType, nPixelSpace, nLineSpace );
#endif
#if GDAL_VERSION_MAJOR == 1 && ( (GDAL_VERSION_MINOR == 9 && GDAL_VERSION_REV <= 2) || (GDAL_VERSION_MINOR == 10 && GDAL_VERSION_REV <= 0) )
CPLSetThreadLocalConfigOption( "VSI_CACHE", pszOldVal );
CPLFree( pszOldVal );
QgsDebugMsg( "Reset VSI_CACHE" );
#endif
return err;
}
int QgsGdalProviderBase::gdalGetOverviewCount( GDALRasterBandH hBand )
{
// See http://hub.qgis.org/issues/8356 and http://trac.osgeo.org/gdal/ticket/5170
#if GDAL_VERSION_MAJOR == 1 && ( (GDAL_VERSION_MINOR == 9 && GDAL_VERSION_REV <= 2) || (GDAL_VERSION_MINOR == 10 && GDAL_VERSION_REV <= 0) )
char* pszOldVal = CPLStrdup( CPLGetConfigOption( "VSI_CACHE", "FALSE" ) );
CPLSetThreadLocalConfigOption( "VSI_CACHE", "FALSE" );
QgsDebugMsg( "Disabled VSI_CACHE" );
#endif
int count = GDALGetOverviewCount( hBand );
#if GDAL_VERSION_MAJOR == 1 && ( (GDAL_VERSION_MINOR == 9 && GDAL_VERSION_REV <= 2) || (GDAL_VERSION_MINOR == 10 && GDAL_VERSION_REV <= 0) )
CPLSetThreadLocalConfigOption( "VSI_CACHE", pszOldVal );
CPLFree( pszOldVal );
QgsDebugMsg( "Reset VSI_CACHE" );
#endif
return count;
}