Add some static methods for creating QgsCoordinateReferenceSystem

This avoids the need for

  QgsCoordinateReferenceSystem crs;
  crs.createFromSrsId(...)

and instead can be replaced with

  QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem::fromSrsId(...)
This commit is contained in:
Nyall Dawson 2016-07-23 17:33:12 +10:00
parent ee62bdeeb4
commit bb220a0b43
4 changed files with 171 additions and 3 deletions

View File

@ -190,6 +190,47 @@ class QgsCoordinateReferenceSystem
// TODO QGIS 3: remove theType and always use EPSG code // TODO QGIS 3: remove theType and always use EPSG code
QgsCoordinateReferenceSystem( const long theId, CrsType theType = PostgisCrsId ); 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 ----------------------- // Misc helper functions -----------------------
/** /**

View File

@ -78,6 +78,39 @@ QgsCoordinateReferenceSystem& QgsCoordinateReferenceSystem::operator=( const Qgs
return *this; 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() QgsCoordinateReferenceSystem::~QgsCoordinateReferenceSystem()
{ {
} }

View File

@ -237,6 +237,47 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
//! Assignment operator //! Assignment operator
QgsCoordinateReferenceSystem& operator=( const QgsCoordinateReferenceSystem& srs ); 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 ----------------------- // Misc helper functions -----------------------
/** /**
@ -256,6 +297,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* and refer to QGIS internal CRS IDs. * and refer to QGIS internal CRS IDs.
* @note this method is expensive. Consider using QgsCRSCache::crsByOgcWmsCrs() instead. * @note this method is expensive. Consider using QgsCRSCache::crsByOgcWmsCrs() instead.
* @return True on success else false * @return True on success else false
* @see fromOgcWmsCrs()
*/ */
// TODO QGIS 3: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid()) // TODO QGIS 3: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
bool createFromOgcWmsCrs( const QString& theCrs ); bool createFromOgcWmsCrs( const QString& theCrs );
@ -277,6 +319,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* @note this method is expensive. Consider using QgsCRSCache::crsByWkt() instead. * @note this method is expensive. Consider using QgsCRSCache::crsByWkt() instead.
* @param theWkt The WKT for the desired spatial reference system. * @param theWkt The WKT for the desired spatial reference system.
* @return True on success else false * @return True on success else false
* @see fromWkt()
*/ */
bool createFromWkt( const QString &theWkt ); bool createFromWkt( const QString &theWkt );
@ -287,6 +330,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* @note this method is expensive. Consider using QgsCRSCache::crsBySrsId() instead. * @note this method is expensive. Consider using QgsCRSCache::crsBySrsId() instead.
* @param theSrsId The internal QGIS CRS ID for the desired spatial reference system. * @param theSrsId The internal QGIS CRS ID for the desired spatial reference system.
* @return True on success else false * @return True on success else false
* @see fromSrsId()
*/ */
bool createFromSrsId( const long theSrsId ); bool createFromSrsId( const long theSrsId );
@ -310,6 +354,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* @note this method is expensive. Consider using QgsCRSCache::crsByProj4() instead. * @note this method is expensive. Consider using QgsCRSCache::crsByProj4() instead.
* @param theProjString A proj4 format string * @param theProjString A proj4 format string
* @return True on success else false * @return True on success else false
* @see fromProj4()
*/ */
bool createFromProj4( const QString &theProjString ); bool createFromProj4( const QString &theProjString );

View File

@ -38,12 +38,16 @@ class TestQgsCoordinateReferenceSystem: public QObject
void copyCtor(); void copyCtor();
void assignmentCtor(); void assignmentCtor();
void createFromId(); void createFromId();
void fromEpsgId();
void createFromOgcWmsCrs(); void createFromOgcWmsCrs();
void createFromSrid(); void createFromSrid();
void createFromWkt(); void createFromWkt();
void fromWkt();
void createFromESRIWkt(); void createFromESRIWkt();
void createFromSrsId(); void createFromSrId();
void fromSrsId();
void createFromProj4(); void createFromProj4();
void fromProj4();
void isValid(); void isValid();
void validate(); void validate();
void equality(); void equality();
@ -161,6 +165,16 @@ void TestQgsCoordinateReferenceSystem::createFromId()
QgsCoordinateReferenceSystem::EpsgCrsId ); QgsCoordinateReferenceSystem::EpsgCrsId );
debugPrint( myCrs ); debugPrint( myCrs );
QVERIFY( myCrs.isValid() ); 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() void TestQgsCoordinateReferenceSystem::createFromOgcWmsCrs()
{ {
@ -175,6 +189,7 @@ void TestQgsCoordinateReferenceSystem::createFromSrid()
myCrs.createFromSrid( GEOSRID ); myCrs.createFromSrid( GEOSRID );
debugPrint( myCrs ); debugPrint( myCrs );
QVERIFY( myCrs.isValid() ); QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
} }
void TestQgsCoordinateReferenceSystem::createFromWkt() void TestQgsCoordinateReferenceSystem::createFromWkt()
{ {
@ -182,6 +197,16 @@ void TestQgsCoordinateReferenceSystem::createFromWkt()
myCrs.createFromWkt( GEOWKT ); myCrs.createFromWkt( GEOWKT );
debugPrint( myCrs ); debugPrint( myCrs );
QVERIFY( myCrs.isValid() ); 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 ) QString TestQgsCoordinateReferenceSystem::testESRIWkt( int i, QgsCoordinateReferenceSystem &myCrs )
@ -297,17 +322,41 @@ void TestQgsCoordinateReferenceSystem::createFromESRIWkt()
// QVERIFY( bOK ); // QVERIFY( bOK );
} }
void TestQgsCoordinateReferenceSystem::createFromSrsId() void TestQgsCoordinateReferenceSystem::createFromSrId()
{ {
QgsCoordinateReferenceSystem myCrs; QgsCoordinateReferenceSystem myCrs;
QVERIFY( myCrs.createFromSrid( GEOSRID ) ); QVERIFY( myCrs.createFromSrid( GEOSRID ) );
debugPrint( myCrs ); 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() void TestQgsCoordinateReferenceSystem::createFromProj4()
{ {
QgsCoordinateReferenceSystem myCrs; QgsCoordinateReferenceSystem myCrs;
QVERIFY( myCrs.createFromProj4( GEOPROJ4 ) ); QVERIFY( myCrs.createFromProj4( GEOPROJ4 ) );
debugPrint( myCrs ); 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() void TestQgsCoordinateReferenceSystem::isValid()
{ {
@ -434,7 +483,7 @@ void TestQgsCoordinateReferenceSystem::isGeographic()
QgsCoordinateReferenceSystem nonGeographic; QgsCoordinateReferenceSystem nonGeographic;
nonGeographic.createFromId( 3857, QgsCoordinateReferenceSystem::EpsgCrsId ); nonGeographic.createFromId( 3857, QgsCoordinateReferenceSystem::EpsgCrsId );
QVERIFY( !geographic.nonGeographic() ); QVERIFY( !nonGeographic.isGeographic() );
} }
void TestQgsCoordinateReferenceSystem::mapUnits() void TestQgsCoordinateReferenceSystem::mapUnits()
{ {