/
qgsrasterprojector.cpp
451 lines (382 loc) · 14.6 KB
/
qgsrasterprojector.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/***************************************************************************
qgsrasterprojector.cpp - Raster projector
--------------------------------------
Date : Jan 16, 2011
Copyright : (C) 2005 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. *
* *
***************************************************************************/
/* $Id: qgsrasterprojector.cpp 15005 2011-01-08 16:35:21Z rblazek $ */
#include <cassert>
#include "qgslogger.h"
#include "qgsrasterprojector.h"
#include "qgscoordinatetransform.h"
QgsRasterProjector::QgsRasterProjector(
QgsCoordinateReferenceSystem theSrcCRS,
QgsCoordinateReferenceSystem theDestCRS,
QgsRectangle theDestExtent,
int theDestRows, int theDestCols,
double theMaxSrcXRes, double theMaxSrcYRes,
QgsRectangle theExtent )
: mSrcCRS( theSrcCRS )
, mDestCRS( theDestCRS )
, mDestExtent( theDestExtent )
, mExtent( theExtent )
, mDestRows( theDestRows ), mDestCols( theDestCols )
, mMaxSrcXRes( theMaxSrcXRes ), mMaxSrcYRes( theMaxSrcYRes )
, mCoordinateTransform( theDestCRS, theSrcCRS )
{
QgsDebugMsg( "Entered" );
QgsDebugMsg( "theDestExtent = " + theDestExtent.toString() );
mDestXRes = mDestExtent.width() / ( mDestCols );
mDestYRes = mDestExtent.height() / ( mDestRows );
// Calculate tolerance
// TODO: Think it over better
// Note: we are checking on matrix each even point, that means taht the real error
// in that moment is approximately half size
double myDestRes = mDestXRes < mDestYRes ? mDestXRes : mDestYRes;
mSqrTolerance = myDestRes * myDestRes;
// Initialize the matrix by corners and middle points
mCPCols = mCPRows = 3;
for ( int i = 0; i < mCPRows; i++ )
{
QList<QgsPoint> myRow;
myRow.append( QgsPoint() );
myRow.append( QgsPoint() );
myRow.append( QgsPoint() );
mCPMatrix.insert( i, myRow );
}
for ( int i = 0; i < mCPRows; i++ )
{
calcRow( i );
}
while ( true )
{
bool myColsOK = checkCols();
if ( !myColsOK )
{
insertRows();
}
bool myRowsOK = checkRows();
if ( !myRowsOK )
{
insertCols();
}
if ( myColsOK && myRowsOK )
{
QgsDebugMsg( "CP matrix within tolerance" );
break;
}
// What is the maximum reasonable size of transformatio matrix?
// TODO: consider better when to break - ratio
if ( mCPRows * mCPCols > 0.25 * mDestRows * mDestCols )
{
QgsDebugMsg( "Too large CP matrix" );
break;
}
}
QgsDebugMsg( QString( "CPMatrix size: mCPRows = %1 mCPCols = %2" ).arg( mCPRows ).arg( mCPCols ) );
mDestRowsPerMatrixRow = ( float )mDestRows / ( mCPRows - 1 );
mDestColsPerMatrixCol = ( float )mDestCols / ( mCPCols - 1 );
//QgsDebugMsg( "CPMatrix:\n" + cpToString() );
// Calculate source dimensions
calcSrcExtent();
calcSrcRowsCols();
mSrcXRes = mSrcExtent.height() / mSrcRows;
mSrcYRes = mSrcExtent.width() / mSrcCols;
// init helper points
pHelperTop = new QgsPoint[mDestCols];
pHelperBottom = new QgsPoint[mDestCols];
calcHelper( 0, pHelperTop );
calcHelper( 1, pHelperBottom );
mHelperTopRow = 0;
}
QgsRasterProjector::~QgsRasterProjector()
{
//delete mCoordinateTransform;
}
void QgsRasterProjector::calcSrcExtent()
{
/* Run around the mCPMatrix and find source extent */
QgsPoint myPoint = mCPMatrix[0][0];
mSrcExtent = QgsRectangle( myPoint.x(), myPoint.y(), myPoint.x(), myPoint.y() );
for ( int i = 0; i < mCPRows; i++ )
{
myPoint = mCPMatrix[i][0];
mSrcExtent.combineExtentWith( myPoint.x(), myPoint.y() );
myPoint = mCPMatrix[i][mCPCols-1];
mSrcExtent.combineExtentWith( myPoint.x(), myPoint.y() );
}
for ( int i = 1; i < mCPCols - 1; i++ )
{
myPoint = mCPMatrix[0][i];
mSrcExtent.combineExtentWith( myPoint.x(), myPoint.y() );
myPoint = mCPMatrix[mCPRows-1][i];
mSrcExtent.combineExtentWith( myPoint.x(), myPoint.y() );
}
// Expand a bit to avoid possible approx coords falling out because of representation error?
// If mMaxSrcXRes, mMaxSrcYRes are defined (fixed src resolution)
// align extent to src resolution to avoid jumping reprojected pixels
// because of shifting resampled grid
// Important especially if we are over mMaxSrcXRes, mMaxSrcYRes limits
QgsDebugMsg( "mSrcExtent = " + mSrcExtent.toString() );
if ( mMaxSrcXRes > 0 )
{
// with floor/ceil it should work correctly also for mSrcExtent.xMinimum() < mExtent.xMinimum()
double col = floor(( mSrcExtent.xMinimum() - mExtent.xMinimum() ) / mMaxSrcXRes );
double x = mExtent.xMinimum() + col * mMaxSrcXRes;
mSrcExtent.setXMinimum( x );
col = ceil(( mSrcExtent.xMaximum() - mExtent.xMinimum() ) / mMaxSrcXRes );
x = mExtent.xMinimum() + col * mMaxSrcXRes;
mSrcExtent.setXMaximum( x );
}
if ( mMaxSrcYRes > 0 )
{
double row = floor(( mExtent.yMaximum() - mSrcExtent.yMaximum() ) / mMaxSrcYRes );
double y = mExtent.yMaximum() - row * mMaxSrcYRes;
mSrcExtent.setYMaximum( y );
row = ceil(( mExtent.yMaximum() - mSrcExtent.yMinimum() ) / mMaxSrcYRes );
y = mExtent.yMaximum() - row * mMaxSrcYRes;
mSrcExtent.setYMinimum( y );
}
QgsDebugMsg( "mSrcExtent = " + mSrcExtent.toString() );
}
QString QgsRasterProjector::cpToString()
{
QString myString;
for ( int i = 0; i < mCPRows; i++ )
{
if ( i > 0 ) myString += "\n";
for ( int j = 0; j < mCPCols; j++ )
{
if ( j > 0 ) myString += " ";
QgsPoint myPoint = mCPMatrix[i][j];
myString += myPoint.toString();
}
}
return myString;
}
void QgsRasterProjector::calcSrcRowsCols()
{
// Wee need to calculate minimum cell size in the source
// TODO: Think it over better, what is the right source resolution?
// Taking distances between cell centers projected to source along source
// axis would result in very high resolution
// TODO: different resolution for rows and cols ?
// For now, we take cell sizes projected to source but not to source axes
double myDestColsPerMatrixCell = mDestCols / mCPCols;
double myDestRowsPerMatrixCell = mDestRows / mCPRows;
QgsDebugMsg( QString( "myDestColsPerMatrixCell = %1 myDestRowsPerMatrixCell = %2" ).arg( myDestColsPerMatrixCell ).arg( myDestRowsPerMatrixCell ) );
double myMinSize = DBL_MAX;
for ( int i = 0; i < mCPRows - 1; i++ )
{
for ( int j = 0; j < mCPCols - 1; j++ )
{
QgsPoint myPointA = mCPMatrix[i][j];
QgsPoint myPointB = mCPMatrix[i][j+1];
QgsPoint myPointC = mCPMatrix[i+1][j];
double mySize = sqrt( myPointA.sqrDist( myPointB ) ) / myDestColsPerMatrixCell;
if ( mySize < myMinSize ) { myMinSize = mySize; }
mySize = sqrt( myPointA.sqrDist( myPointC ) ) / myDestRowsPerMatrixCell;
if ( mySize < myMinSize ) { myMinSize = mySize; }
}
}
// Make it a bit higher resolution
// TODO: find the best coefficient, attention, increasing resolution for WMS
// is changing WMS content
myMinSize *= 0.75;
QgsDebugMsg( QString( "mMaxSrcXRes = %1 mMaxSrcYRes = %2" ).arg( mMaxSrcXRes ).arg( mMaxSrcYRes ) );
// mMaxSrcXRes, mMaxSrcYRes may be 0 - no limit (WMS)
double myMinXSize = mMaxSrcXRes > myMinSize ? mMaxSrcXRes : myMinSize;
double myMinYSize = mMaxSrcYRes > myMinSize ? mMaxSrcYRes : myMinSize;
QgsDebugMsg( QString( "myMinXSize = %1 myMinYSize = %2" ).arg( myMinXSize ).arg( myMinYSize ) );
QgsDebugMsg( QString( "mSrcExtent.width = %1 mSrcExtent.height = %2" ).arg( mSrcExtent.width() ).arg( mSrcExtent.height() ) );
// we have to round to keep alignment set in calcSrcExtent
mSrcRows = ( int ) qRound( mSrcExtent.height() / myMinYSize );
mSrcCols = ( int ) qRound( mSrcExtent.width() / myMinXSize );
QgsDebugMsg( QString( "mSrcRows = %1 mSrcCols = %2" ).arg( mSrcRows ).arg( mSrcCols ) );
}
inline void QgsRasterProjector::destPointOnCPMatrix( int theRow, int theCol, double *theX, double *theY )
{
*theX = mDestExtent.xMinimum() + theCol * mDestExtent.width() / ( mCPCols - 1 );
*theY = mDestExtent.yMaximum() - theRow * mDestExtent.height() / ( mCPRows - 1 );
}
inline int QgsRasterProjector::matrixRow( int theDestRow )
{
return ( int )( floor(( theDestRow + 0.5 ) / mDestRowsPerMatrixRow ) );
}
inline int QgsRasterProjector::matrixCol( int theDestCol )
{
return ( int )( floor(( theDestCol + 0.5 ) / mDestColsPerMatrixCol ) );
}
QgsPoint QgsRasterProjector::srcPoint( int theDestRow, int theCol )
{
return QgsPoint();
}
void QgsRasterProjector::calcHelper( int theMatrixRow, QgsPoint *thePoints )
{
// TODO?: should we also precalc dest cell center coordinates for x and y?
for ( int myDestCol = 0; myDestCol < mDestCols; myDestCol++ )
{
double myDestX = mDestExtent.xMinimum() + ( myDestCol + 0.5 ) * mDestXRes;
int myMatrixCol = matrixCol( myDestCol );
double myDestXMin, myDestYMin, myDestXMax, myDestYMax;
destPointOnCPMatrix( theMatrixRow, myMatrixCol, &myDestXMin, &myDestYMin );
destPointOnCPMatrix( theMatrixRow, myMatrixCol + 1, &myDestXMax, &myDestYMax );
double xfrac = ( myDestX - myDestXMin ) / ( myDestXMax - myDestXMin );
QgsPoint &mySrcPoint0 = mCPMatrix[theMatrixRow][myMatrixCol];
QgsPoint &mySrcPoint1 = mCPMatrix[theMatrixRow][myMatrixCol+1];
double s = mySrcPoint0.x() + ( mySrcPoint1.x() - mySrcPoint0.x() ) * xfrac;
double t = mySrcPoint0.y() + ( mySrcPoint1.y() - mySrcPoint0.y() ) * xfrac;
thePoints[myDestCol].setX( s );
thePoints[myDestCol].setY( t );
}
}
void QgsRasterProjector::nextHelper()
{
QgsPoint *tmp;
tmp = pHelperTop;
pHelperTop = pHelperBottom;
pHelperBottom = tmp;
calcHelper( mHelperTopRow + 2, pHelperBottom );
mHelperTopRow++;
}
void QgsRasterProjector::srcRowCol( int theDestRow, int theDestCol, int *theSrcRow, int *theSrcCol )
{
int myMatrixRow = matrixRow( theDestRow );
int myMatrixCol = matrixCol( theDestCol );
if ( myMatrixRow > mHelperTopRow )
{
// TODO: make it more robust (for random, not sequential reading)
nextHelper();
}
double myDestY = mDestExtent.yMaximum() - ( theDestRow + 0.5 ) * mDestYRes;
// See the schema in javax.media.jai.WarpGrid doc (but up side down)
// TODO: use some kind of cache of values which can be reused
double myDestXMin, myDestYMin, myDestXMax, myDestYMax;
destPointOnCPMatrix( myMatrixRow + 1, myMatrixCol, &myDestXMin, &myDestYMin );
destPointOnCPMatrix( myMatrixRow, myMatrixCol + 1, &myDestXMax, &myDestYMax );
double yfrac = ( myDestY - myDestYMin ) / ( myDestYMax - myDestYMin );
QgsPoint &myTop = pHelperTop[theDestCol];
QgsPoint &myBot = pHelperBottom[theDestCol];
// Warning: this is very SLOW compared to the following code!:
//double mySrcX = myBot.x() + (myTop.x() - myBot.x()) * yfrac;
//double mySrcY = myBot.y() + (myTop.y() - myBot.y()) * yfrac;
double tx = myTop.x();
double ty = myTop.y();
double bx = myBot.x();
double by = myBot.y();
double mySrcX = bx + ( tx - bx ) * yfrac;
double mySrcY = by + ( ty - by ) * yfrac;
// TODO: check again cell selection (coor is in the middle)
*theSrcRow = ( int ) floor(( mSrcExtent.yMaximum() - mySrcY ) / mSrcXRes );
*theSrcCol = ( int ) floor(( mySrcX - mSrcExtent.xMinimum() ) / mSrcYRes );
assert( *theSrcRow < mSrcRows );
assert( *theSrcCol < mSrcCols );
}
void QgsRasterProjector::insertRows()
{
for ( int r = 0; r < mCPRows - 1; r++ )
{
QList<QgsPoint> myRow;
for ( int c = 0; c < mCPCols; c++ )
{
myRow.append( QgsPoint() );
}
QgsDebugMsg( QString( "insert new row at %1" ).arg( 1 + r*2 ) );
mCPMatrix.insert( 1 + r*2, myRow );
}
mCPRows += mCPRows - 1;
for ( int r = 1; r < mCPRows - 1; r += 2 )
{
calcRow( r );
}
}
void QgsRasterProjector::insertCols()
{
for ( int r = 0; r < mCPRows; r++ )
{
QList<QgsPoint> myRow;
for ( int c = 0; c < mCPCols - 1; c++ )
{
mCPMatrix[r].insert( 1 + c*2, QgsPoint() );
}
}
mCPCols += mCPCols - 1;
for ( int c = 1; c < mCPCols - 1; c += 2 )
{
calcCol( c );
}
}
void QgsRasterProjector::calcCP( int theRow, int theCol )
{
double myDestX, myDestY;
destPointOnCPMatrix( theRow, theCol, &myDestX, &myDestY );
QgsPoint myDestPoint( myDestX, myDestY );
mCPMatrix[theRow][theCol] = mCoordinateTransform.transform( myDestPoint );
}
bool QgsRasterProjector::calcRow( int theRow )
{
QgsDebugMsg( QString( "theRow = %1" ).arg( theRow ) );
for ( int i = 0; i < mCPCols; i++ )
{
calcCP( theRow, i );
}
return true;
}
bool QgsRasterProjector::calcCol( int theCol )
{
QgsDebugMsgLevel( QString( "theCol = %1" ).arg( theCol ), 3 );
for ( int i = 0; i < mCPRows; i++ )
{
calcCP( i, theCol );
}
return true;
}
bool QgsRasterProjector::checkCols()
{
for ( int c = 0; c < mCPCols; c++ )
{
for ( int r = 1; r < mCPRows - 1; r += 2 )
{
double myDestX, myDestY;
destPointOnCPMatrix( r, c, &myDestX, &myDestY );
QgsPoint myDestPoint( myDestX, myDestY );
QgsPoint mySrcPoint1 = mCPMatrix[r-1][c];
QgsPoint mySrcPoint2 = mCPMatrix[r][c];
QgsPoint mySrcPoint3 = mCPMatrix[r+1][c];
QgsPoint mySrcApprox(( mySrcPoint1.x() + mySrcPoint3.x() ) / 2, ( mySrcPoint1.y() + mySrcPoint3.y() ) / 2 );
QgsPoint myDestApprox = mCoordinateTransform.transform( mySrcApprox, QgsCoordinateTransform::ReverseTransform );
double mySqrDist = myDestApprox.sqrDist( myDestPoint );
if ( mySqrDist > mSqrTolerance ) { return false; }
}
}
return true;
}
bool QgsRasterProjector::checkRows()
{
for ( int r = 0; r < mCPRows; r++ )
{
for ( int c = 1; c < mCPCols - 1; c += 2 )
{
double myDestX, myDestY;
destPointOnCPMatrix( r, c, &myDestX, &myDestY );
QgsPoint myDestPoint( myDestX, myDestY );
QgsPoint mySrcPoint1 = mCPMatrix[r][c-1];
QgsPoint mySrcPoint2 = mCPMatrix[r][c];
QgsPoint mySrcPoint3 = mCPMatrix[r][c+1];
QgsPoint mySrcApprox(( mySrcPoint1.x() + mySrcPoint3.x() ) / 2, ( mySrcPoint1.y() + mySrcPoint3.y() ) / 2 );
QgsPoint myDestApprox = mCoordinateTransform.transform( mySrcApprox, QgsCoordinateTransform::ReverseTransform );
double mySqrDist = myDestApprox.sqrDist( myDestPoint );
if ( mySqrDist > mSqrTolerance ) { return false; }
}
}
return true;
}