@@ -145,19 +145,36 @@ QgsRectangle QgsAlignRaster::clipExtent() const
145
145
}
146
146
147
147
148
- void QgsAlignRaster::setParametersFromRaster ( const QString& filename )
148
+ bool QgsAlignRaster::setParametersFromRaster ( const QString& filename, const QString& destWkt )
149
149
{
150
- setParametersFromRaster ( RasterInfo ( filename ) );
150
+ return setParametersFromRaster ( RasterInfo ( filename ), destWkt );
151
151
}
152
152
153
- void QgsAlignRaster::setParametersFromRaster ( const RasterInfo& rasterInfo )
153
+ bool QgsAlignRaster::setParametersFromRaster ( const RasterInfo& rasterInfo, const QString& destWkt )
154
154
{
155
- // use ref. layer to init input
156
- mCrsWkt = rasterInfo.crs ();
157
- mCellSizeX = rasterInfo.cellSize ().width ();
158
- mCellSizeY = rasterInfo.cellSize ().height ();
159
- mGridOffsetX = rasterInfo.gridOffset ().x ();
160
- mGridOffsetY = rasterInfo.gridOffset ().y ();
155
+ if ( destWkt.isEmpty () || destWkt.toAscii () == rasterInfo.crs () )
156
+ {
157
+ // use ref. layer to init input
158
+ mCrsWkt = rasterInfo.crs ();
159
+ mCellSizeX = rasterInfo.cellSize ().width ();
160
+ mCellSizeY = rasterInfo.cellSize ().height ();
161
+ mGridOffsetX = rasterInfo.gridOffset ().x ();
162
+ mGridOffsetY = rasterInfo.gridOffset ().y ();
163
+ }
164
+ else
165
+ {
166
+ QSizeF cs;
167
+ QPointF go;
168
+ if ( !suggestedWarpOutput ( rasterInfo, destWkt.toAscii (), &cs, &go ) )
169
+ return false ;
170
+
171
+ mCrsWkt = destWkt.toAscii ();
172
+ mCellSizeX = cs.width ();
173
+ mCellSizeY = cs.height ();
174
+ mGridOffsetX = go.x ();
175
+ mGridOffsetY = go.y ();
176
+ }
177
+ return true ;
161
178
}
162
179
163
180
@@ -172,52 +189,36 @@ bool QgsAlignRaster::determineTransformAndSize()
172
189
173
190
RasterInfo info ( r.inputFilename );
174
191
175
- // Create a transformer that maps from source pixel/line coordinates
176
- // to destination georeferenced coordinates (not destination
177
- // pixel line). We do that by omitting the destination dataset
178
- // handle (setting it to NULL).
179
- void * hTransformArg = GDALCreateGenImgProjTransformer ( info.mDataset , info.mCrsWkt .constData (), NULL , mCrsWkt .constData (), FALSE , 0 , 1 );
180
- if ( !hTransformArg )
192
+ QSizeF cs;
193
+ QgsRectangle extent;
194
+ if ( !suggestedWarpOutput ( info, mCrsWkt , &cs, 0 , &extent ) )
181
195
{
182
- mErrorMessage = QString ( " GDALCreateGenImgProjTransformer failed.\n\n "
183
- " Source WKT:\n %1\n\n Destination WKT:\n %2" )
196
+ mErrorMessage = QString ( " Failed to get suggested warp output.\n\n "
197
+ " File:\n %1\n\n "
198
+ " Source WKT:\n %2\n\n Destination WKT:\n %3" )
199
+ .arg ( r.inputFilename )
184
200
.arg ( QString::fromAscii ( info.mCrsWkt ) )
185
201
.arg ( QString::fromAscii ( mCrsWkt ) );
186
202
return false ;
187
203
}
188
204
189
- // Get approximate output georeferenced bounds and resolution for file.
190
- double adfDstGeoTransform[6 ];
191
- double extents[4 ];
192
- int nPixels = 0 , nLines = 0 ;
193
- CPLErr eErr;
194
- eErr = GDALSuggestedWarpOutput2 ( info.mDataset ,
195
- GDALGenImgProjTransform, hTransformArg,
196
- adfDstGeoTransform, &nPixels, &nLines, extents, 0 );
197
- GDALDestroyGenImgProjTransformer ( hTransformArg );
198
- r.srcCellSizeInDestCRS = fabs ( adfDstGeoTransform[1 ] * adfDstGeoTransform[5 ] );
199
-
200
- if ( eErr != CE_None )
201
- {
202
- mErrorMessage = QString ( " GDALSuggestedWarpOutput2 failed.\n\n " + r.inputFilename );
203
- return false ;
204
- }
205
+ r.srcCellSizeInDestCRS = cs.width () * cs.height ();
205
206
206
207
if ( finalExtent[0 ] == 0 && finalExtent[1 ] == 0 && finalExtent[2 ] == 0 && finalExtent[3 ] == 0 )
207
208
{
208
209
// initialize with the first layer
209
- finalExtent[0 ] = extents[ 0 ] ;
210
- finalExtent[1 ] = extents[ 1 ] ;
211
- finalExtent[2 ] = extents[ 2 ] ;
212
- finalExtent[3 ] = extents[ 3 ] ;
210
+ finalExtent[0 ] = extent. xMinimum () ;
211
+ finalExtent[1 ] = extent. yMinimum () ;
212
+ finalExtent[2 ] = extent. xMaximum () ;
213
+ finalExtent[3 ] = extent. yMaximum () ;
213
214
}
214
215
else
215
216
{
216
217
// use intersection of rects
217
- if ( extents[ 0 ] > finalExtent[0 ] ) finalExtent[0 ] = extents[ 0 ] ;
218
- if ( extents[ 1 ] > finalExtent[1 ] ) finalExtent[1 ] = extents[ 1 ] ;
219
- if ( extents[ 2 ] < finalExtent[2 ] ) finalExtent[2 ] = extents[ 2 ] ;
220
- if ( extents[ 3 ] < finalExtent[3 ] ) finalExtent[3 ] = extents[ 3 ] ;
218
+ if ( extent. xMinimum () > finalExtent[0 ] ) finalExtent[0 ] = extent. xMinimum () ;
219
+ if ( extent. yMinimum () > finalExtent[1 ] ) finalExtent[1 ] = extent. yMinimum () ;
220
+ if ( extent. xMaximum () < finalExtent[2 ] ) finalExtent[2 ] = extent. xMaximum () ;
221
+ if ( extent. yMaximum () < finalExtent[3 ] ) finalExtent[3 ] = extent. yMaximum () ;
221
222
}
222
223
}
223
224
@@ -393,6 +394,41 @@ bool QgsAlignRaster::createAndWarp( const Item& raster )
393
394
return true ;
394
395
}
395
396
397
+ bool QgsAlignRaster::suggestedWarpOutput ( const QgsAlignRaster::RasterInfo& info, const QByteArray& destWkt, QSizeF* cellSize, QPointF* gridOffset, QgsRectangle* rect )
398
+ {
399
+ // Create a transformer that maps from source pixel/line coordinates
400
+ // to destination georeferenced coordinates (not destination
401
+ // pixel line). We do that by omitting the destination dataset
402
+ // handle (setting it to NULL).
403
+ void * hTransformArg = GDALCreateGenImgProjTransformer ( info.mDataset , info.mCrsWkt .constData (), NULL , destWkt.constData (), FALSE , 0 , 1 );
404
+ if ( !hTransformArg )
405
+ return false ;
406
+
407
+ // Get approximate output georeferenced bounds and resolution for file.
408
+ double adfDstGeoTransform[6 ];
409
+ double extents[4 ];
410
+ int nPixels = 0 , nLines = 0 ;
411
+ CPLErr eErr;
412
+ eErr = GDALSuggestedWarpOutput2 ( info.mDataset ,
413
+ GDALGenImgProjTransform, hTransformArg,
414
+ adfDstGeoTransform, &nPixels, &nLines, extents, 0 );
415
+ GDALDestroyGenImgProjTransformer ( hTransformArg );
416
+
417
+ if ( eErr != CE_None )
418
+ return false ;
419
+
420
+ QSizeF cs ( qAbs ( adfDstGeoTransform[1 ] ), qAbs ( adfDstGeoTransform[5 ] ) );
421
+
422
+ if ( rect )
423
+ *rect = QgsRectangle ( extents[0 ], extents[1 ], extents[2 ], extents[3 ] );
424
+ if ( cellSize )
425
+ *cellSize = cs;
426
+ if ( gridOffset )
427
+ *gridOffset = QPointF ( fmod_with_tolerance ( adfDstGeoTransform[0 ], cs.width () ),
428
+ fmod_with_tolerance ( adfDstGeoTransform[3 ], cs.height () ) );
429
+ return true ;
430
+ }
431
+
396
432
397
433
// ----------
398
434
0 commit comments