Skip to content

Commit c96acd1

Browse files
author
rblazek
committed
align extent according to src resolution/origin
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15533 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent d0501c1 commit c96acd1

File tree

3 files changed

+62
-24
lines changed

3 files changed

+62
-24
lines changed

src/core/qgsrasterdataprovider.cpp

+19-19
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void QgsRasterDataProvider::readBlock( int bandNo, QgsRectangle const & viewExt
4747
mMaxSrcYRes = extent().height() / ySize();
4848
}
4949

50-
QgsRasterProjector myProjector( theSrcCRS, theDestCRS, viewExtent, height, width, mMaxSrcXRes, mMaxSrcYRes );
50+
QgsRasterProjector myProjector( theSrcCRS, theDestCRS, viewExtent, height, width, mMaxSrcXRes, mMaxSrcYRes, mExtent );
5151

5252
QgsDebugMsg( QString( "create projector time (ms): %1" ).arg( time.elapsed() ) );
5353

@@ -182,10 +182,10 @@ QString QgsRasterDataProvider::lastErrorFormat()
182182

183183
QByteArray QgsRasterDataProvider::noValueBytes( int theBandNo )
184184
{
185-
int type = dataType(theBandNo);
186-
int size = dataTypeSize(theBandNo) / 8;
185+
int type = dataType( theBandNo );
186+
int size = dataTypeSize( theBandNo ) / 8;
187187
QByteArray ba;
188-
ba.resize(size);
188+
ba.resize( size );
189189
char * data = ba.data();
190190
double noval = mNoDataValue[theBandNo-1];
191191
unsigned char uc;
@@ -195,35 +195,35 @@ QByteArray QgsRasterDataProvider::noValueBytes( int theBandNo )
195195
int i;
196196
float f;
197197
double d;
198-
switch (type)
198+
switch ( type )
199199
{
200200
case QgsRasterDataProvider::Byte:
201-
uc = (unsigned char)noval;
202-
memcpy ( data, &uc, size);
201+
uc = ( unsigned char )noval;
202+
memcpy( data, &uc, size );
203203
break;
204204
case QgsRasterDataProvider::UInt16:
205-
us = (unsigned short)noval;
206-
memcpy ( data, &us, size);
205+
us = ( unsigned short )noval;
206+
memcpy( data, &us, size );
207207
break;
208208
case QgsRasterDataProvider::Int16:
209-
s = (short)noval;
210-
memcpy ( data, &s, size);
209+
s = ( short )noval;
210+
memcpy( data, &s, size );
211211
break;
212212
case QgsRasterDataProvider::UInt32:
213-
ui = (unsigned int)noval;
214-
memcpy ( data, &ui, size);
213+
ui = ( unsigned int )noval;
214+
memcpy( data, &ui, size );
215215
break;
216216
case QgsRasterDataProvider::Int32:
217-
i = (int)noval;
218-
memcpy ( data, &i, size);
217+
i = ( int )noval;
218+
memcpy( data, &i, size );
219219
break;
220220
case QgsRasterDataProvider::Float32:
221-
f = (float)noval;
222-
memcpy ( data, &f, size);
221+
f = ( float )noval;
222+
memcpy( data, &f, size );
223223
break;
224224
case QgsRasterDataProvider::Float64:
225-
d = (double)noval;
226-
memcpy ( data, &d, size);
225+
d = ( double )noval;
226+
memcpy( data, &d, size );
227227
break;
228228
default:
229229
QgsLogger::warning( "GDAL data type is not supported" );

src/core/qgsrasterprojector.cpp

+38-4
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ QgsRasterProjector::QgsRasterProjector(
2727
QgsCoordinateReferenceSystem theDestCRS,
2828
QgsRectangle theDestExtent,
2929
int theDestRows, int theDestCols,
30-
double theMaxSrcXRes, double theMaxSrcYRes )
30+
double theMaxSrcXRes, double theMaxSrcYRes,
31+
QgsRectangle theExtent )
3132
: mSrcCRS( theSrcCRS )
3233
, mDestCRS( theDestCRS )
3334
, mDestExtent( theDestExtent )
3435
, mDestRows( theDestRows ), mDestCols( theDestCols )
3536
, mMaxSrcXRes( theMaxSrcXRes ), mMaxSrcYRes( theMaxSrcYRes )
37+
, mExtent( theExtent )
3638
{
3739
QgsDebugMsg( "Entered" );
3840
QgsDebugMsg( "theDestExtent = " + theDestExtent.toString() );
@@ -135,7 +137,37 @@ void QgsRasterProjector::calcSrcExtent()
135137
myPoint = mCPMatrix[mCPRows-1][i];
136138
mSrcExtent.combineExtentWith( myPoint.x(), myPoint.y() );
137139
}
138-
// Expand a bit to avoid possible approx coords falling out because of representation error
140+
// Expand a bit to avoid possible approx coords falling out because of representation error?
141+
142+
// If mMaxSrcXRes, mMaxSrcYRes are defined (fixed src resolution)
143+
// align extent to src resolution to avoid jumping reprojected pixels
144+
// because of shifting resampled grid
145+
// Important especially if we are over mMaxSrcXRes, mMaxSrcYRes limits
146+
147+
QgsDebugMsg( "mSrcExtent = " + mSrcExtent.toString() );
148+
149+
if ( mMaxSrcXRes > 0 )
150+
{
151+
// with floor/ceil it should work correctly also for mSrcExtent.xMinimum() < mExtent.xMinimum()
152+
double col = floor(( mSrcExtent.xMinimum() - mExtent.xMinimum() ) / mMaxSrcXRes );
153+
double x = mExtent.xMinimum() + col * mMaxSrcXRes;
154+
mSrcExtent.setXMinimum( x );
155+
156+
col = ceil(( mSrcExtent.xMaximum() - mExtent.xMinimum() ) / mMaxSrcXRes );
157+
x = mExtent.xMinimum() + col * mMaxSrcXRes;
158+
mSrcExtent.setXMaximum( x );
159+
}
160+
if ( mMaxSrcYRes > 0 )
161+
{
162+
double row = floor(( mExtent.yMaximum() - mSrcExtent.yMaximum() ) / mMaxSrcYRes );
163+
double y = mExtent.yMaximum() - row * mMaxSrcYRes;
164+
mSrcExtent.setYMaximum( y );
165+
166+
row = ceil(( mExtent.yMaximum() - mSrcExtent.yMinimum() ) / mMaxSrcYRes );
167+
y = mExtent.yMaximum() - row * mMaxSrcYRes;
168+
mSrcExtent.setYMinimum( y );
169+
}
170+
139171

140172
QgsDebugMsg( "mSrcExtent = " + mSrcExtent.toString() );
141173
}
@@ -197,8 +229,10 @@ void QgsRasterProjector::calcSrcRowsCols()
197229
double myMinYSize = mMaxSrcYRes > myMinSize ? mMaxSrcYRes : myMinSize;
198230
QgsDebugMsg( QString( "myMinXSize = %1 myMinYSize = %2" ).arg( myMinXSize ).arg( myMinYSize ) );
199231
QgsDebugMsg( QString( "mSrcExtent.width = %1 mSrcExtent.height = %2" ).arg( mSrcExtent.width() ).arg( mSrcExtent.height() ) );
200-
mSrcRows = ( int ) ceil( mSrcExtent.height() / myMinYSize );
201-
mSrcCols = ( int ) ceil( mSrcExtent.width() / myMinXSize );
232+
233+
// we have to round to keep alignment set in calcSrcExtent
234+
mSrcRows = ( int ) qRound( mSrcExtent.height() / myMinYSize );
235+
mSrcCols = ( int ) qRound( mSrcExtent.width() / myMinXSize );
202236

203237
QgsDebugMsg( QString( "mSrcRows = %1 mSrcCols = %2" ).arg( mSrcRows ).arg( mSrcCols ) );
204238
}

src/core/qgsrasterprojector.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class QgsRasterProjector
5151
QgsCoordinateReferenceSystem theDestCRS,
5252
QgsRectangle theDestExtent,
5353
int theDestRows, int theDestCols,
54-
double theMaxSrcXRes, double theMaxSrcYRes
54+
double theMaxSrcXRes, double theMaxSrcYRes,
55+
QgsRectangle theExtent
5556
);
5657

5758
/** \brief The destructor */
@@ -136,6 +137,9 @@ class QgsRasterProjector
136137
/** Source extent */
137138
QgsRectangle mSrcExtent;
138139

140+
/** Source raster extent */
141+
QgsRectangle mExtent;
142+
139143
/** Number of destination rows */
140144
int mDestRows;
141145

0 commit comments

Comments
 (0)