diff --git a/python/core/qgscoordinatereferencesystem.sip b/python/core/qgscoordinatereferencesystem.sip index 69a86afa770..6696d6d99ae 100644 --- a/python/core/qgscoordinatereferencesystem.sip +++ b/python/core/qgscoordinatereferencesystem.sip @@ -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 ----------------------- /** diff --git a/src/core/qgscoordinatereferencesystem.cpp b/src/core/qgscoordinatereferencesystem.cpp index a5ecb34ff33..808b7596519 100644 --- a/src/core/qgscoordinatereferencesystem.cpp +++ b/src/core/qgscoordinatereferencesystem.cpp @@ -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() { } diff --git a/src/core/qgscoordinatereferencesystem.h b/src/core/qgscoordinatereferencesystem.h index 3a84e82f067..a6d46d10e17 100644 --- a/src/core/qgscoordinatereferencesystem.h +++ b/src/core/qgscoordinatereferencesystem.h @@ -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 ----------------------- /** @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); diff --git a/tests/src/core/testqgscoordinatereferencesystem.cpp b/tests/src/core/testqgscoordinatereferencesystem.cpp index 90a7bd98ee6..7c2ab3cdd7c 100644 --- a/tests/src/core/testqgscoordinatereferencesystem.cpp +++ b/tests/src/core/testqgscoordinatereferencesystem.cpp @@ -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(); @@ -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() { @@ -175,6 +189,7 @@ void TestQgsCoordinateReferenceSystem::createFromSrid() myCrs.createFromSrid( GEOSRID ); debugPrint( myCrs ); QVERIFY( myCrs.isValid() ); + QCOMPARE( myCrs.srsid(), GEOCRS_ID ); } void TestQgsCoordinateReferenceSystem::createFromWkt() { @@ -182,6 +197,16 @@ void TestQgsCoordinateReferenceSystem::createFromWkt() 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 ) @@ -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() { @@ -434,7 +483,7 @@ void TestQgsCoordinateReferenceSystem::isGeographic() QgsCoordinateReferenceSystem nonGeographic; nonGeographic.createFromId( 3857, QgsCoordinateReferenceSystem::EpsgCrsId ); - QVERIFY( !geographic.nonGeographic() ); + QVERIFY( !nonGeographic.isGeographic() ); } void TestQgsCoordinateReferenceSystem::mapUnits() {