Skip to content

Commit 486eb70

Browse files
author
jef
committed
gdal provider: change handling of crs
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15506 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 5b4fe60 commit 486eb70

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

src/providers/gdal/qgsgdalprovider.cpp

+49-22
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ int CPL_STDCALL progressCallback( double dfComplete,
101101

102102

103103
QgsGdalProvider::QgsGdalProvider( QString const & uri )
104-
: QgsRasterDataProvider( uri ), mValid( true )
104+
: QgsRasterDataProvider( uri )
105+
, mValid( true )
105106
{
106107
QgsDebugMsg( "QgsGdalProvider: constructing with uri '" + uri + "'." );
107108

@@ -137,8 +138,8 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
137138

138139
// Check if we need a warped VRT for this file.
139140
bool hasGeoTransform = GDALGetGeoTransform( mGdalBaseDataset, mGeoTransform ) == CE_None;
140-
if ( ( hasGeoTransform
141-
&& ( mGeoTransform[1] < 0.0
141+
if (( hasGeoTransform
142+
&& ( mGeoTransform[1] < 0.0
142143
|| mGeoTransform[2] != 0.0
143144
|| mGeoTransform[4] != 0.0
144145
|| mGeoTransform[5] > 0.0 ) )
@@ -149,7 +150,7 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
149150
mGdalDataset =
150151
GDALAutoCreateWarpedVRT( mGdalBaseDataset, NULL, NULL,
151152
GRA_NearestNeighbour, 0.2, NULL );
152-
153+
153154
if ( mGdalDataset == NULL )
154155
{
155156
QgsLogger::warning( "Warped VRT Creation failed." );
@@ -158,16 +159,16 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
158159
}
159160
else
160161
{
161-
GDALGetGeoTransform(mGdalDataset, mGeoTransform);
162+
GDALGetGeoTransform( mGdalDataset, mGeoTransform );
162163
}
163164
}
164165
else
165166
{
166167
mGdalDataset = mGdalBaseDataset;
167168
GDALReferenceDataset( mGdalDataset );
168169
}
169-
170-
if (!hasGeoTransform)
170+
171+
if ( !hasGeoTransform )
171172
{
172173
// Initialise the affine transform matrix
173174
mGeoTransform[0] = 0;
@@ -200,22 +201,11 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
200201
// QgsCoordinateTransform for this layer
201202
// NOTE: we must do this before metadata is called
202203

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 ) ) )
207206
{
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();
219209
}
220210

221211
//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 )
354344
QgsDebugMsg( "end" );
355345
}
356346

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+
357384
QgsGdalProvider::~QgsGdalProvider()
358385
{
359386
QgsDebugMsg( "QgsGdalProvider: deconstructing." );

src/providers/gdal/qgsgdalprovider.h

+2
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ class QgsGdalProvider : public QgsRasterDataProvider
249249

250250

251251
private:
252+
// initialize CRS from wkt
253+
bool crsFromWkt( const char *wkt );
252254

253255
/**
254256
* Flag indicating if the layer data source is a valid WMS layer

0 commit comments

Comments
 (0)