Skip to content

Commit 4d603b4

Browse files
committedDec 12, 2012
Merge branch 'wms_raster_performance'
2 parents 32f0dfd + d9a3c99 commit 4d603b4

9 files changed

+97
-63
lines changed
 

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ void QgsCoordinateReferenceSystem::validate()
286286
mCustomSrsValidation( this );
287287

288288
if ( !mIsValidFlag )
289-
// set the default
290-
createFromOgcWmsCrs( GEO_EPSG_CRS_AUTHID );
289+
{
290+
*this = QgsCRSCache::instance()->crsByAuthId( GEO_EPSG_CRS_AUTHID );
291+
}
291292
}
292293

293294
bool QgsCoordinateReferenceSystem::createFromSrid( long id )

‎src/core/qgscoordinatetransform.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* *
1616
***************************************************************************/
1717
#include "qgscoordinatetransform.h"
18+
#include "qgscrscache.h"
1819
#include "qgsmessagelog.h"
1920
#include "qgslogger.h"
2021

@@ -148,9 +149,8 @@ void QgsCoordinateTransform::initialise()
148149
if ( !mDestCRS.isValid() )
149150
{
150151
//No destination projection is set so we set the default output projection to
151-
//be the same as input proj. This only happens on the first layer loaded
152-
//whatever that may be...
153-
mDestCRS.createFromOgcWmsCrs( mSourceCRS.authid() );
152+
//be the same as input proj.
153+
mDestCRS = QgsCRSCache::instance()->crsByAuthId( mSourceCRS.authid() );
154154
}
155155

156156
// init the projections (destination and source)

‎src/core/raster/qgspalettedrasterrenderer.cpp

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525

2626
QgsPalettedRasterRenderer::QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber,
2727
QColor* colorArray, int nColors ):
28+
QgsRasterRenderer( input, "paletted" ), mBand( bandNumber ), mNColors( nColors )
29+
{
30+
mColors = new QRgb[nColors];
31+
for ( int i = 0; i < nColors; ++i )
32+
{
33+
mColors[i] = colorArray[i].rgba();
34+
}
35+
delete[] colorArray;
36+
}
37+
38+
QgsPalettedRasterRenderer::QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QRgb* colorArray, int nColors ):
2839
QgsRasterRenderer( input, "paletted" ), mBand( bandNumber ), mColors( colorArray ), mNColors( nColors )
2940
{
3041
}
@@ -36,7 +47,7 @@ QgsPalettedRasterRenderer::~QgsPalettedRasterRenderer()
3647

3748
QgsRasterInterface * QgsPalettedRasterRenderer::clone() const
3849
{
39-
QgsPalettedRasterRenderer * renderer = new QgsPalettedRasterRenderer( 0, mBand, colors(), mNColors );
50+
QgsPalettedRasterRenderer * renderer = new QgsPalettedRasterRenderer( 0, mBand, rgbArray(), mNColors );
4051
renderer->setOpacity( mOpacity );
4152
renderer->setAlphaBand( mAlphaBand );
4253
renderer->setRasterTransparency( mRasterTransparency );
@@ -52,14 +63,14 @@ QgsRasterRenderer* QgsPalettedRasterRenderer::create( const QDomElement& elem, Q
5263

5364
int bandNumber = elem.attribute( "band", "-1" ).toInt();
5465
int nColors = 0;
55-
QColor* colors = 0;
66+
QRgb* colors = 0;
5667

5768
QDomElement paletteElem = elem.firstChildElement( "colorPalette" );
5869
if ( !paletteElem.isNull() )
5970
{
6071
QDomNodeList paletteEntries = paletteElem.elementsByTagName( "paletteEntry" );
6172
nColors = paletteEntries.size();
62-
colors = new QColor[ nColors ];
73+
colors = new QRgb[ nColors ];
6374

6475
int value = 0;
6576
QDomElement entryElem;
@@ -68,7 +79,7 @@ QgsRasterRenderer* QgsPalettedRasterRenderer::create( const QDomElement& elem, Q
6879
entryElem = paletteEntries.at( i ).toElement();
6980
value = entryElem.attribute( "value", "0" ).toInt();
7081
QgsDebugMsg( entryElem.attribute( "color", "#000000" ) );
71-
colors[value] = QColor( entryElem.attribute( "color", "#000000" ) );
82+
colors[value] = QColor( entryElem.attribute( "color", "#000000" ) ).rgba();
7283
}
7384
}
7485
QgsRasterRenderer* r = new QgsPalettedRasterRenderer( input, bandNumber, colors, nColors );
@@ -85,11 +96,25 @@ QColor* QgsPalettedRasterRenderer::colors() const
8596
QColor* colorArray = new QColor[ mNColors ];
8697
for ( int i = 0; i < mNColors; ++i )
8798
{
88-
colorArray[i] = mColors[i];
99+
colorArray[i] = QColor( mColors[i] );
89100
}
90101
return colorArray;
91102
}
92103

104+
QRgb* QgsPalettedRasterRenderer::rgbArray() const
105+
{
106+
if ( mNColors < 1 )
107+
{
108+
return 0;
109+
}
110+
QRgb* rgbValues = new QRgb[mNColors];
111+
for ( int i = 0; i < mNColors; ++i )
112+
{
113+
rgbValues[i] = mColors[i];
114+
}
115+
return rgbValues;
116+
}
117+
93118
QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle const & extent, int width, int height )
94119
{
95120
QgsRasterBlock *outputBlock = new QgsRasterBlock();
@@ -146,7 +171,12 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
146171
return outputBlock;
147172
}
148173

149-
for ( size_t i = 0; i < ( size_t )width*height; i++ )
174+
//use direct data access instead of QgsRasterBlock::setValue
175+
//because of performance
176+
unsigned int* outputData = ( unsigned int* )( outputBlock->data() );
177+
178+
size_t rasterSize = ( size_t )width * height;
179+
for ( size_t i = 0; i < rasterSize; ++i )
150180
{
151181
int val = ( int ) inputBlock->value( i );
152182
if ( inputBlock->isNoDataValue( val ) )
@@ -156,14 +186,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
156186
}
157187
if ( !hasTransparency )
158188
{
159-
if ( val < 0 || val > mNColors )
160-
{
161-
outputBlock->setColor( i, myDefaultColor );
162-
}
163-
else
164-
{
165-
outputBlock->setColor( i, mColors[ val ].rgba() );
166-
}
189+
outputData[i] = mColors[ val ];
167190
}
168191
else
169192
{
@@ -176,9 +199,8 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
176199
{
177200
currentOpacity *= alphaBlock->value( i ) / 255.0;
178201
}
179-
QColor& currentColor = mColors[val];
180-
181-
outputBlock->setColor( i, qRgba( currentOpacity * currentColor.red(), currentOpacity * currentColor.green(), currentOpacity * currentColor.blue(), currentOpacity * 255 ) );
202+
QColor currentColor = QColor( mColors[val] );
203+
outputData[i] = qRgba( currentOpacity * currentColor.red(), currentOpacity * currentColor.green(), currentOpacity * currentColor.blue(), currentOpacity * 255 );
182204
}
183205
}
184206

@@ -207,7 +229,7 @@ void QgsPalettedRasterRenderer::writeXML( QDomDocument& doc, QDomElement& parent
207229
{
208230
QDomElement colorElem = doc.createElement( "paletteEntry" );
209231
colorElem.setAttribute( "value", i );
210-
colorElem.setAttribute( "color", mColors[i].name() );
232+
colorElem.setAttribute( "color", QColor( mColors[i] ).name() );
211233
colorPaletteElem.appendChild( colorElem );
212234
}
213235
rasterRendererElem.appendChild( colorPaletteElem );
@@ -219,7 +241,7 @@ void QgsPalettedRasterRenderer::legendSymbologyItems( QList< QPair< QString, QCo
219241
{
220242
for ( int i = 0; i < mNColors; ++i )
221243
{
222-
symbolItems.push_back( qMakePair( QString::number( i ), mColors[i] ) );
244+
symbolItems.push_back( qMakePair( QString::number( i ), QColor( mColors[i] ) ) );
223245
}
224246
}
225247

‎src/core/raster/qgspalettedrasterrenderer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
3131
public:
3232
/**Renderer owns color array*/
3333
QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QColor* colorArray, int nColors );
34+
QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QRgb* colorArray, int nColors );
3435
~QgsPalettedRasterRenderer();
3536
QgsRasterInterface * clone() const;
3637
static QgsRasterRenderer* create( const QDomElement& elem, QgsRasterInterface* input );
@@ -43,6 +44,8 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
4344
int nColors() const { return mNColors; }
4445
/**Returns copy of color array (caller takes ownership)*/
4546
QColor* colors() const;
47+
/**Returns copy of rgb array (caller takes ownership)*/
48+
QRgb* rgbArray() const;
4649

4750
void writeXML( QDomDocument& doc, QDomElement& parentElem ) const;
4851

@@ -53,7 +56,7 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
5356
private:
5457
int mBand;
5558
/**Color array*/
56-
QColor* mColors;
59+
QRgb* mColors;
5760
/**Number of colors*/
5861
int mNColors;
5962
};

‎src/core/raster/qgsrasterblock.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -239,28 +239,6 @@ bool QgsRasterBlock::isNoDataValue( double value, double noDataValue )
239239
return false;
240240
}
241241

242-
bool QgsRasterBlock::isNoDataValue( double value ) const
243-
{
244-
// More precise would be qIsNaN(value) && qIsNaN(noDataValue(bandNo)), but probably
245-
// not important and slower
246-
if ( qIsNaN( value ) ||
247-
doubleNear( value, mNoDataValue ) )
248-
{
249-
return true;
250-
}
251-
return false;
252-
}
253-
254-
double QgsRasterBlock::value( size_t index ) const
255-
{
256-
if ( index >= ( size_t )mWidth*mHeight )
257-
{
258-
QgsDebugMsg( QString( "Index %1 out of range (%2 x %3)" ).arg( index ).arg( mWidth ).arg( mHeight ) );
259-
return mNoDataValue;
260-
}
261-
return readValue( mData, mDataType, index );
262-
}
263-
264242
double QgsRasterBlock::value( int row, int column ) const
265243
{
266244
return value(( size_t )row*mWidth + column );

‎src/core/raster/qgsrasterblock.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,28 @@ inline bool QgsRasterBlock::valueInRange( double value, const QList<QgsRasterBlo
430430
return false;
431431
}
432432

433+
inline double QgsRasterBlock::value( size_t index ) const
434+
{
435+
/*if ( index >= ( size_t )mWidth*mHeight )
436+
{
437+
QgsDebugMsg( QString( "Index %1 out of range (%2 x %3)" ).arg( index ).arg( mWidth ).arg( mHeight ) );
438+
return mNoDataValue;
439+
}*/
440+
return readValue( mData, mDataType, index );
441+
}
442+
443+
inline bool QgsRasterBlock::isNoDataValue( double value ) const
444+
{
445+
// More precise would be qIsNaN(value) && qIsNaN(noDataValue(bandNo)), but probably
446+
// not important and slower
447+
if ( qIsNaN( value ) ||
448+
doubleNear( value, mNoDataValue ) )
449+
{
450+
return true;
451+
}
452+
return false;
453+
}
454+
433455
#endif
434456

435457

‎src/mapserver/qgis_map_serv.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ map service syntax for SOAP/HTTP POST
2929
#include "qgswfsserver.h"
3030
#include "qgsmaprenderer.h"
3131
#include "qgsmapserviceexception.h"
32+
#include "qgspallabeling.h"
3233
#include "qgsprojectparser.h"
3334
#include "qgssldparser.h"
3435
#include <QDomDocument>
@@ -198,6 +199,7 @@ int main( int argc, char * argv[] )
198199

199200
//creating QgsMapRenderer is expensive (access to srs.db), so we do it here before the fcgi loop
200201
QgsMapRenderer* theMapRenderer = new QgsMapRenderer();
202+
theMapRenderer->setLabelingEngine( new QgsPalLabeling() );
201203

202204
while ( fcgi_accept() >= 0 )
203205
{

‎src/mapserver/qgswmsserver.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "qgsmaplayerregistry.h"
2525
#include "qgsmaprenderer.h"
2626
#include "qgsmaptopixel.h"
27-
#include "qgspallabeling.h"
2827
#include "qgsproject.h"
2928
#include "qgsrasterlayer.h"
3029
#include "qgsscalecalculator.h"
@@ -941,7 +940,6 @@ QImage* QgsWMSServer::initializeRendering( QStringList& layersList, QStringList&
941940
delete theImage;
942941
return 0;
943942
}
944-
mMapRenderer->setLabelingEngine( new QgsPalLabeling() );
945943

946944
//find out the current scale denominater and set it to the SLD parser
947945
QgsScaleCalculator scaleCalc(( theImage->logicalDpiX() + theImage->logicalDpiY() ) / 2 , mMapRenderer->destinationCrs().mapUnits() );

‎src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,17 @@ void QgsGdalProvider::readBlock( int theBandNo, QgsRectangle const & theExtent,
395395

396396
int dataSize = dataTypeSize( theBandNo );
397397

398-
// fill with null values
399-
QByteArray ba = QgsRasterBlock::valueBytes( dataType( theBandNo ), noDataValue( theBandNo ) );
400-
char *nodata = ba.data();
401-
char *block = ( char * ) theBlock;
402-
for ( int i = 0; i < thePixelWidth * thePixelHeight; i++ )
398+
if ( !mExtent.contains( theExtent ) )
403399
{
404-
memcpy( block, nodata, dataSize );
405-
block += dataSize;
400+
// fill with null values
401+
QByteArray ba = QgsRasterBlock::valueBytes( dataType( theBandNo ), noDataValue( theBandNo ) );
402+
char *nodata = ba.data();
403+
char *block = ( char * ) theBlock;
404+
for ( int i = 0; i < thePixelWidth * thePixelHeight; i++ )
405+
{
406+
memcpy( block, nodata, dataSize );
407+
block += dataSize;
408+
}
406409
}
407410

408411
QgsRectangle myRasterExtent = theExtent.intersect( &mExtent );
@@ -561,23 +564,28 @@ void QgsGdalProvider::readBlock( int theBandNo, QgsRectangle const & theExtent,
561564
double tmpXRes = srcWidth * srcXRes / tmpWidth;
562565
double tmpYRes = srcHeight * srcYRes / tmpHeight; // negative
563566

567+
double y = myRasterExtent.yMaximum() - 0.5 * yRes;
564568
for ( int row = 0; row < height; row++ )
565569
{
566-
double y = myRasterExtent.yMaximum() - ( row + 0.5 ) * yRes;
567570
int tmpRow = static_cast<int>( floor( -1. * ( tmpYMax - y ) / tmpYRes ) );
568571

569572
char *srcRowBlock = tmpBlock + dataSize * tmpRow * tmpWidth;
570573
char *dstRowBlock = ( char * )theBlock + dataSize * ( top + row ) * thePixelWidth;
571-
for ( int col = 0; col < width; col++ )
574+
575+
double x = myRasterExtent.xMinimum() + 0.5 * xRes; // cell center
576+
char* dst = dstRowBlock + dataSize * left;
577+
char* src = 0;
578+
int tmpCol = 0;
579+
for ( int col = 0; col < width; ++col )
572580
{
573-
// cell center
574-
double x = myRasterExtent.xMinimum() + ( col + 0.5 ) * xRes;
575581
// floor() is quite slow! Use just cast to int.
576-
int tmpCol = static_cast<int>(( x - tmpXMin ) / tmpXRes ) ;
577-
char *src = srcRowBlock + dataSize * tmpCol;
578-
char *dst = dstRowBlock + dataSize * ( left + col );
582+
tmpCol = static_cast<int>(( x - tmpXMin ) / tmpXRes ) ;
583+
src = srcRowBlock + dataSize * tmpCol;
579584
memcpy( dst, src, dataSize );
585+
dst += dataSize;
586+
x += xRes;
580587
}
588+
y -= yRes;
581589
}
582590

583591
QgsFree( tmpBlock );

0 commit comments

Comments
 (0)
Please sign in to comment.