Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
gdal provider: change handling of crs
git-svn-id: http://svn.osgeo.org/qgis/trunk@15506 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Mar 15, 2011
1 parent a3cfe6f commit 88d4389
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
71 changes: 49 additions & 22 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -101,7 +101,8 @@ int CPL_STDCALL progressCallback( double dfComplete,


QgsGdalProvider::QgsGdalProvider( QString const & uri )
: QgsRasterDataProvider( uri ), mValid( true )
: QgsRasterDataProvider( uri )
, mValid( true )
{
QgsDebugMsg( "QgsGdalProvider: constructing with uri '" + uri + "'." );

Expand Down Expand Up @@ -137,8 +138,8 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )

// Check if we need a warped VRT for this file.
bool hasGeoTransform = GDALGetGeoTransform( mGdalBaseDataset, mGeoTransform ) == CE_None;
if ( ( hasGeoTransform
&& ( mGeoTransform[1] < 0.0
if (( hasGeoTransform
&& ( mGeoTransform[1] < 0.0
|| mGeoTransform[2] != 0.0
|| mGeoTransform[4] != 0.0
|| mGeoTransform[5] > 0.0 ) )
Expand All @@ -149,7 +150,7 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
mGdalDataset =
GDALAutoCreateWarpedVRT( mGdalBaseDataset, NULL, NULL,
GRA_NearestNeighbour, 0.2, NULL );

if ( mGdalDataset == NULL )
{
QgsLogger::warning( "Warped VRT Creation failed." );
Expand All @@ -158,16 +159,16 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
}
else
{
GDALGetGeoTransform(mGdalDataset, mGeoTransform);
GDALGetGeoTransform( mGdalDataset, mGeoTransform );
}
}
else
{
mGdalDataset = mGdalBaseDataset;
GDALReferenceDataset( mGdalDataset );
}
if (!hasGeoTransform)

if ( !hasGeoTransform )
{
// Initialise the affine transform matrix
mGeoTransform[0] = 0;
Expand Down Expand Up @@ -200,22 +201,11 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
// QgsCoordinateTransform for this layer
// NOTE: we must do this before metadata is called

QString myWktString;
myWktString = QString( GDALGetProjectionRef( mGdalDataset ) );
mCrs.createFromWkt( myWktString );
if ( !mCrs.isValid() )
if ( !crsFromWkt( GDALGetProjectionRef( mGdalDataset ) ) &&
!crsFromWkt( GDALGetGCPProjection( mGdalDataset ) ) )
{
//try to get the gcp srs from the raster layer if available
myWktString = QString( GDALGetGCPProjection( mGdalDataset ) );
// What is the purpose of this piece of code?
// Sideeffects from validate()?
// myCRS.createFromWkt(myWktString);
// if (!myCRS.isValid())
// {
// // use force and make CRS valid!
// myCRS.validate();
// }

QgsDebugMsg( "No valid CRS identified" );
mCrs.validate();
}

//set up the coordinat transform - in the case of raster this is mainly used to convert
Expand Down Expand Up @@ -354,6 +344,43 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
QgsDebugMsg( "end" );
}

bool QgsGdalProvider::crsFromWkt( const char *wkt )
{
void *hCRS = OSRNewSpatialReference( NULL );

if ( OSRImportFromWkt( hCRS, (char **) &wkt ) == OGRERR_NONE )
{
if ( OSRAutoIdentifyEPSG( hCRS ) == OGRERR_NONE )
{
QString authid = QString( "%1:%2" )
.arg( OSRGetAuthorityName( hCRS, NULL ) )
.arg( OSRGetAuthorityCode( hCRS, NULL ) );
QgsDebugMsg( "authid recognized as " + authid );
mCrs.createFromOgcWmsCrs( authid );
}
else
{
// get the proj4 text
char *pszProj4;
OSRExportToProj4( hCRS, &pszProj4 );
QgsDebugMsg( pszProj4 );
OGRFree( pszProj4 );

char *pszWkt = NULL;
OSRExportToWkt( hCRS, &pszWkt );
QString myWktString = QString( pszWkt );
OGRFree( pszWkt );

// create CRS from Wkt
mCrs.createFromWkt( myWktString );
}
}

OSRRelease( hCRS );

return mCrs.isValid();
}

QgsGdalProvider::~QgsGdalProvider()
{
QgsDebugMsg( "QgsGdalProvider: deconstructing." );
Expand Down
2 changes: 2 additions & 0 deletions src/providers/gdal/qgsgdalprovider.h
Expand Up @@ -249,6 +249,8 @@ class QgsGdalProvider : public QgsRasterDataProvider


private:
// initialize CRS from wkt
bool crsFromWkt( const char *wkt );

/**
* Flag indicating if the layer data source is a valid WMS layer
Expand Down

0 comments on commit 88d4389

Please sign in to comment.