Skip to content

Commit

Permalink
Add some static methods for creating QgsCoordinateReferenceSystem
Browse files Browse the repository at this point in the history
This avoids the need for

  QgsCoordinateReferenceSystem crs;
  crs.createFromSrsId(...)

and instead can be replaced with

  QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem::fromSrsId(...)
  • Loading branch information
nyalldawson committed Jul 25, 2016
1 parent ee62bde commit bb220a0
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 3 deletions.
41 changes: 41 additions & 0 deletions python/core/qgscoordinatereferencesystem.sip
Expand Up @@ -190,6 +190,47 @@ class QgsCoordinateReferenceSystem
// TODO QGIS 3: remove theType and always use EPSG code
QgsCoordinateReferenceSystem( const long theId, CrsType theType = PostgisCrsId );

// static creators

/** Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
* @param ogcCrs OGR compliant CRS definition, eg "EPSG:4326"
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 3.0
* @see createFromOgcWmsCrs()
*/
static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString& ogcCrs );

/** Creates a CRS from a given EPSG ID.
* @param epsg epsg CRS ID
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 3.0
*/
static QgsCoordinateReferenceSystem fromEpsgId( long epsg );

/** Creates a CRS from a proj4 style formatted string.
* @param proj4 proj4 format string
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 3.0
* @see createFromProj4()
*/
static QgsCoordinateReferenceSystem fromProj4( const QString& proj4 );

/** Creates a CRS from a WKT spatial ref sys definition string.
* @param wkt WKT for the desired spatial reference system.
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 3.0
* @see createFromWkt()
*/
static QgsCoordinateReferenceSystem fromWkt( const QString& wkt );

/** Creates a CRS from a specified QGIS SRS ID.
* @param srsId internal QGIS SRS ID
* @returns matching CRS, or an invalid CRS if ID could not be found
* @note added in QGIS 3.0
* @see createFromSrsId()
*/
static QgsCoordinateReferenceSystem fromSrsId( long srsId );

// Misc helper functions -----------------------

/**
Expand Down
33 changes: 33 additions & 0 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -78,6 +78,39 @@ QgsCoordinateReferenceSystem& QgsCoordinateReferenceSystem::operator=( const Qgs
return *this;
}

QgsCoordinateReferenceSystem QgsCoordinateReferenceSystem::fromOgcWmsCrs( const QString& ogcCrs )
{
QgsCoordinateReferenceSystem crs;
crs.createFromOgcWmsCrs( ogcCrs );
return crs;
}

QgsCoordinateReferenceSystem QgsCoordinateReferenceSystem::fromEpsgId( long epsg )
{
return fromOgcWmsCrs( "EPSG:" + QString::number( epsg ) );
}

QgsCoordinateReferenceSystem QgsCoordinateReferenceSystem::fromProj4( const QString& proj4 )
{
QgsCoordinateReferenceSystem crs;
crs.createFromProj4( proj4 );
return crs;
}

QgsCoordinateReferenceSystem QgsCoordinateReferenceSystem::fromWkt( const QString& wkt )
{
QgsCoordinateReferenceSystem crs;
crs.createFromWkt( wkt );
return crs;
}

QgsCoordinateReferenceSystem QgsCoordinateReferenceSystem::fromSrsId( long srsId )
{
QgsCoordinateReferenceSystem crs;
crs.createFromSrsId( srsId );
return crs;
}

QgsCoordinateReferenceSystem::~QgsCoordinateReferenceSystem()
{
}
Expand Down
45 changes: 45 additions & 0 deletions src/core/qgscoordinatereferencesystem.h
Expand Up @@ -237,6 +237,47 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
//! Assignment operator
QgsCoordinateReferenceSystem& operator=( const QgsCoordinateReferenceSystem& srs );

// static creators

/** Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
* @param ogcCrs OGR compliant CRS definition, eg "EPSG:4326"
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 3.0
* @see createFromOgcWmsCrs()
*/
static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString& ogcCrs );

/** Creates a CRS from a given EPSG ID.
* @param epsg epsg CRS ID
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 3.0
*/
static QgsCoordinateReferenceSystem fromEpsgId( long epsg );

/** Creates a CRS from a proj4 style formatted string.
* @param proj4 proj4 format string
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 3.0
* @see createFromProj4()
*/
static QgsCoordinateReferenceSystem fromProj4( const QString& proj4 );

/** Creates a CRS from a WKT spatial ref sys definition string.
* @param wkt WKT for the desired spatial reference system.
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 3.0
* @see createFromWkt()
*/
static QgsCoordinateReferenceSystem fromWkt( const QString& wkt );

/** Creates a CRS from a specified QGIS SRS ID.
* @param srsId internal QGIS SRS ID
* @returns matching CRS, or an invalid CRS if ID could not be found
* @note added in QGIS 3.0
* @see createFromSrsId()
*/
static QgsCoordinateReferenceSystem fromSrsId( long srsId );

// Misc helper functions -----------------------

/**
Expand All @@ -256,6 +297,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* and refer to QGIS internal CRS IDs.
* @note this method is expensive. Consider using QgsCRSCache::crsByOgcWmsCrs() instead.
* @return True on success else false
* @see fromOgcWmsCrs()
*/
// TODO QGIS 3: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
bool createFromOgcWmsCrs( const QString& theCrs );
Expand All @@ -277,6 +319,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* @note this method is expensive. Consider using QgsCRSCache::crsByWkt() instead.
* @param theWkt The WKT for the desired spatial reference system.
* @return True on success else false
* @see fromWkt()
*/
bool createFromWkt( const QString &theWkt );

Expand All @@ -287,6 +330,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* @note this method is expensive. Consider using QgsCRSCache::crsBySrsId() instead.
* @param theSrsId The internal QGIS CRS ID for the desired spatial reference system.
* @return True on success else false
* @see fromSrsId()
*/
bool createFromSrsId( const long theSrsId );

Expand All @@ -310,6 +354,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* @note this method is expensive. Consider using QgsCRSCache::crsByProj4() instead.
* @param theProjString A proj4 format string
* @return True on success else false
* @see fromProj4()
*/
bool createFromProj4( const QString &theProjString );

Expand Down
55 changes: 52 additions & 3 deletions tests/src/core/testqgscoordinatereferencesystem.cpp
Expand Up @@ -38,12 +38,16 @@ class TestQgsCoordinateReferenceSystem: public QObject
void copyCtor();
void assignmentCtor();
void createFromId();
void fromEpsgId();
void createFromOgcWmsCrs();
void createFromSrid();
void createFromWkt();
void fromWkt();
void createFromESRIWkt();
void createFromSrsId();
void createFromSrId();
void fromSrsId();
void createFromProj4();
void fromProj4();
void isValid();
void validate();
void equality();
Expand Down Expand Up @@ -161,6 +165,16 @@ void TestQgsCoordinateReferenceSystem::createFromId()
QgsCoordinateReferenceSystem::EpsgCrsId );
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
}

void TestQgsCoordinateReferenceSystem::fromEpsgId()
{
QgsCoordinateReferenceSystem myCrs = QgsCoordinateReferenceSystem::fromEpsgId( GEO_EPSG_CRS_ID );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
myCrs = QgsCoordinateReferenceSystem::fromEpsgId( -999 );
QVERIFY( !myCrs.isValid() );
}
void TestQgsCoordinateReferenceSystem::createFromOgcWmsCrs()
{
Expand All @@ -175,13 +189,24 @@ void TestQgsCoordinateReferenceSystem::createFromSrid()
myCrs.createFromSrid( GEOSRID );
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
}
void TestQgsCoordinateReferenceSystem::createFromWkt()
{
QgsCoordinateReferenceSystem myCrs;
myCrs.createFromWkt( GEOWKT );
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
}

void TestQgsCoordinateReferenceSystem::fromWkt()
{
QgsCoordinateReferenceSystem myCrs = QgsCoordinateReferenceSystem::fromWkt( GEOWKT );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
myCrs = QgsCoordinateReferenceSystem::fromWkt( "not wkt" );
QVERIFY( !myCrs.isValid() );
}

QString TestQgsCoordinateReferenceSystem::testESRIWkt( int i, QgsCoordinateReferenceSystem &myCrs )
Expand Down Expand Up @@ -297,17 +322,41 @@ void TestQgsCoordinateReferenceSystem::createFromESRIWkt()

// QVERIFY( bOK );
}
void TestQgsCoordinateReferenceSystem::createFromSrsId()
void TestQgsCoordinateReferenceSystem::createFromSrId()
{
QgsCoordinateReferenceSystem myCrs;
QVERIFY( myCrs.createFromSrid( GEOSRID ) );
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
}

void TestQgsCoordinateReferenceSystem::fromSrsId()
{
QgsCoordinateReferenceSystem myCrs = QgsCoordinateReferenceSystem::fromSrsId( GEOCRS_ID );
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
myCrs = QgsCoordinateReferenceSystem::fromSrsId( -9999 );
QVERIFY( !myCrs.isValid() );
}
void TestQgsCoordinateReferenceSystem::createFromProj4()
{
QgsCoordinateReferenceSystem myCrs;
QVERIFY( myCrs.createFromProj4( GEOPROJ4 ) );
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
}

void TestQgsCoordinateReferenceSystem::fromProj4()
{
QgsCoordinateReferenceSystem myCrs = QgsCoordinateReferenceSystem::fromProj4( GEOPROJ4 );
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
myCrs = QgsCoordinateReferenceSystem::fromProj4( "" );
QVERIFY( !myCrs.isValid() );
}
void TestQgsCoordinateReferenceSystem::isValid()
{
Expand Down Expand Up @@ -434,7 +483,7 @@ void TestQgsCoordinateReferenceSystem::isGeographic()

QgsCoordinateReferenceSystem nonGeographic;
nonGeographic.createFromId( 3857, QgsCoordinateReferenceSystem::EpsgCrsId );
QVERIFY( !geographic.nonGeographic() );
QVERIFY( !nonGeographic.isGeographic() );
}
void TestQgsCoordinateReferenceSystem::mapUnits()
{
Expand Down

0 comments on commit bb220a0

Please sign in to comment.