@@ -101,7 +101,8 @@ int CPL_STDCALL progressCallback( double dfComplete,
101
101
102
102
103
103
QgsGdalProvider::QgsGdalProvider ( QString const & uri )
104
- : QgsRasterDataProvider( uri ), mValid( true )
104
+ : QgsRasterDataProvider( uri )
105
+ , mValid( true )
105
106
{
106
107
QgsDebugMsg ( " QgsGdalProvider: constructing with uri '" + uri + " '." );
107
108
@@ -137,8 +138,8 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
137
138
138
139
// Check if we need a warped VRT for this file.
139
140
bool hasGeoTransform = GDALGetGeoTransform ( mGdalBaseDataset , mGeoTransform ) == CE_None;
140
- if ( ( hasGeoTransform
141
- && ( mGeoTransform [1 ] < 0.0
141
+ if (( hasGeoTransform
142
+ && ( mGeoTransform [1 ] < 0.0
142
143
|| mGeoTransform [2 ] != 0.0
143
144
|| mGeoTransform [4 ] != 0.0
144
145
|| mGeoTransform [5 ] > 0.0 ) )
@@ -149,7 +150,7 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
149
150
mGdalDataset =
150
151
GDALAutoCreateWarpedVRT ( mGdalBaseDataset , NULL , NULL ,
151
152
GRA_NearestNeighbour, 0.2 , NULL );
152
-
153
+
153
154
if ( mGdalDataset == NULL )
154
155
{
155
156
QgsLogger::warning ( " Warped VRT Creation failed." );
@@ -158,16 +159,16 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
158
159
}
159
160
else
160
161
{
161
- GDALGetGeoTransform (mGdalDataset , mGeoTransform );
162
+ GDALGetGeoTransform ( mGdalDataset , mGeoTransform );
162
163
}
163
164
}
164
165
else
165
166
{
166
167
mGdalDataset = mGdalBaseDataset ;
167
168
GDALReferenceDataset ( mGdalDataset );
168
169
}
169
-
170
- if (!hasGeoTransform)
170
+
171
+ if ( !hasGeoTransform )
171
172
{
172
173
// Initialise the affine transform matrix
173
174
mGeoTransform [0 ] = 0 ;
@@ -200,22 +201,11 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
200
201
// QgsCoordinateTransform for this layer
201
202
// NOTE: we must do this before metadata is called
202
203
203
- QString myWktString;
204
- myWktString = QString ( GDALGetProjectionRef ( mGdalDataset ) );
205
- mCrs .createFromWkt ( myWktString );
206
- if ( !mCrs .isValid () )
204
+ if ( !crsFromWkt ( GDALGetProjectionRef ( mGdalDataset ) ) &&
205
+ !crsFromWkt ( GDALGetGCPProjection ( mGdalDataset ) ) )
207
206
{
208
- // try to get the gcp srs from the raster layer if available
209
- myWktString = QString ( GDALGetGCPProjection ( mGdalDataset ) );
210
- // What is the purpose of this piece of code?
211
- // Sideeffects from validate()?
212
- // myCRS.createFromWkt(myWktString);
213
- // if (!myCRS.isValid())
214
- // {
215
- // // use force and make CRS valid!
216
- // myCRS.validate();
217
- // }
218
-
207
+ QgsDebugMsg ( " No valid CRS identified" );
208
+ mCrs .validate ();
219
209
}
220
210
221
211
// set up the coordinat transform - in the case of raster this is mainly used to convert
@@ -354,6 +344,43 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
354
344
QgsDebugMsg ( " end" );
355
345
}
356
346
347
+ bool QgsGdalProvider::crsFromWkt ( const char *wkt )
348
+ {
349
+ void *hCRS = OSRNewSpatialReference ( NULL );
350
+
351
+ if ( OSRImportFromWkt ( hCRS, (char **) &wkt ) == OGRERR_NONE )
352
+ {
353
+ if ( OSRAutoIdentifyEPSG ( hCRS ) == OGRERR_NONE )
354
+ {
355
+ QString authid = QString ( " %1:%2" )
356
+ .arg ( OSRGetAuthorityName ( hCRS, NULL ) )
357
+ .arg ( OSRGetAuthorityCode ( hCRS, NULL ) );
358
+ QgsDebugMsg ( " authid recognized as " + authid );
359
+ mCrs .createFromOgcWmsCrs ( authid );
360
+ }
361
+ else
362
+ {
363
+ // get the proj4 text
364
+ char *pszProj4;
365
+ OSRExportToProj4 ( hCRS, &pszProj4 );
366
+ QgsDebugMsg ( pszProj4 );
367
+ OGRFree ( pszProj4 );
368
+
369
+ char *pszWkt = NULL ;
370
+ OSRExportToWkt ( hCRS, &pszWkt );
371
+ QString myWktString = QString ( pszWkt );
372
+ OGRFree ( pszWkt );
373
+
374
+ // create CRS from Wkt
375
+ mCrs .createFromWkt ( myWktString );
376
+ }
377
+ }
378
+
379
+ OSRRelease ( hCRS );
380
+
381
+ return mCrs .isValid ();
382
+ }
383
+
357
384
QgsGdalProvider::~QgsGdalProvider ()
358
385
{
359
386
QgsDebugMsg ( " QgsGdalProvider: deconstructing." );
0 commit comments