Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Deprecate most methods which construct CRSes from internal CRS ids
These should NOT be used, and auth:code or WKT definitions used instead.

Unfortunately some use of these methods are very heavily entangled around
other code, so we can't deprecate all of them until 4.0

(cherry picked from commit cbc1ee5)
  • Loading branch information
nyalldawson committed Dec 20, 2019
1 parent c72565e commit fc06fce
Show file tree
Hide file tree
Showing 24 changed files with 128 additions and 144 deletions.
26 changes: 19 additions & 7 deletions python/core/auto_generated/qgscoordinatereferencesystem.sip.in
Expand Up @@ -202,7 +202,7 @@ If no prefix is specified, WKT definition is assumed.
%End


explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId );
explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) /Deprecated/;
%Docstring
Constructor a CRS object using a PostGIS SRID, an EPSG code or an internal QGIS CRS ID.

Expand All @@ -214,6 +214,11 @@ Constructor a CRS object using a PostGIS SRID, an EPSG code or an internal QGIS

:param id: The ID valid for the chosen CRS ID type
:param type: One of the types described in CrsType

.. deprecated::
We encourage you to use EPSG codes or WKT to describe CRSes in your code
wherever possible. Internal QGIS CRS IDs are not guaranteed to be permanent / involatile,
and Proj strings are a lossy format.
%End

QgsCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &srs );
Expand Down Expand Up @@ -317,17 +322,16 @@ Creates a CRS from a specified QGIS SRS ID.



bool createFromId( long id, CrsType type = PostgisCrsId );
bool createFromId( long id, CrsType type = PostgisCrsId ) /Deprecated/;
%Docstring
Sets this CRS by lookup of the given ID in the CRS database.

:return: ``True`` on success else ``False``

.. note::

.. deprecated::
We encourage you to use EPSG code or WKT to describe CRSes in your code
wherever possible. Internal QGIS CRS IDs are not guaranteed to be permanent / involatile,
and Proj strings are a lossy format.
wherever possible. Internal QGIS CRS IDs are not guaranteed to be permanent / involatile,
and Proj strings are a lossy format.
%End


Expand Down Expand Up @@ -401,6 +405,11 @@ user's local CRS database from home directory is used.
this method uses an internal cache. Call invalidateCache() to clear the cache.

.. seealso:: :py:func:`fromSrsId`

.. warning::

This method is highly discouraged, and CRS objects should instead be constructed
using auth:id codes or WKT strings
%End

bool createFromProj4( const QString &projString ) /Deprecated/;
Expand Down Expand Up @@ -517,7 +526,7 @@ For more details on supported formats see OGRSpatialReference.SetFromUserInput()
/ // TODO QGIS3: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
%End

static void setupESRIWktFix();
static void setupESRIWktFix() /Deprecated/;
%Docstring
Make sure that ESRI WKT import is done properly.
This is required for proper shapefile CRS import when using gdal>= 1.9.
Expand All @@ -532,6 +541,9 @@ This is required for proper shapefile CRS import when using gdal>= 1.9.
This function sets CPL config option GDAL_FIX_ESRI_WKT to a proper value,
unless it has been set by the user through the commandline or an environment variable.
For more details refer to OGRSpatialReference.morphFromESRI() .

.. deprecated::
Not used on builds based on Proj version 6 or later
%End

bool isValid() const;
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmimportphotos.cpp
Expand Up @@ -270,7 +270,7 @@ QVariantMap QgsImportPhotosAlgorithm::processAlgorithm( const QVariantMap &param
outFields.append( QgsField( QStringLiteral( "timestamp" ), QVariant::DateTime ) );
QString outputDest;
std::unique_ptr< QgsFeatureSink > outputSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, outputDest, outFields,
QgsWkbTypes::PointZ, QgsCoordinateReferenceSystem( 4326 ) ) );
QgsWkbTypes::PointZ, QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ) );

QgsFields invalidFields;
invalidFields.append( QgsField( QStringLiteral( "photo" ), QVariant::String ) );
Expand Down
10 changes: 1 addition & 9 deletions src/core/providers/memory/qgsmemoryprovider.cpp
Expand Up @@ -253,15 +253,7 @@ QString QgsMemoryProvider::dataSourceUri( bool expandAuthConfig ) const
}
else
{
int srid = mCrs.postgisSrid();
if ( srid )
{
crsDef = QStringLiteral( "postgis:%1" ).arg( srid );
}
else
{
crsDef = QStringLiteral( "wkt:%1" ).arg( mCrs.toWkt() );
}
crsDef = QStringLiteral( "wkt:%1" ).arg( mCrs.toWkt() );
}
uri.addQueryItem( QStringLiteral( "crs" ), crsDef );
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -3690,7 +3690,11 @@ QgsCoordinateReferenceSystem QgsOgrProvider::crs() const
}

// add towgs84 parameter
#if PROJ_VERSION_MAJOR<6
Q_NOWARN_DEPRECATED_PUSH
QgsCoordinateReferenceSystem::setupESRIWktFix();
Q_NOWARN_DEPRECATED_POP
#endif

if ( OGRSpatialReferenceH spatialRefSys = mOgrLayer->GetSpatialRef() )
{
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -119,7 +119,9 @@ QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem( const QString &defin
QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem( const long id, CrsType type )
{
d = new QgsCoordinateReferenceSystemPrivate();
Q_NOWARN_DEPRECATED_PUSH
createFromId( id, type );
Q_NOWARN_DEPRECATED_POP
}

QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &srs ) //NOLINT
Expand Down Expand Up @@ -298,7 +300,9 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString &definition )
else
{
const long id = match.captured( 2 ).toLong();
Q_NOWARN_DEPRECATED_PUSH
result = createFromId( id, InternalCrsId );
Q_NOWARN_DEPRECATED_POP
}
}
else
Expand Down Expand Up @@ -336,11 +340,15 @@ bool QgsCoordinateReferenceSystem::createFromUserInput( const QString &definitio
QString userWkt;
OGRSpatialReferenceH crs = OSRNewSpatialReference( nullptr );

#if PROJ_VERSION_MAJOR<6
// make sure towgs84 parameter is loaded if using an ESRI definition and gdal >= 1.9
if ( definition.startsWith( QLatin1String( "ESRI::" ) ) )
{
Q_NOWARN_DEPRECATED_PUSH
setupESRIWktFix();
Q_NOWARN_DEPRECATED_POP
}
#endif

if ( OSRSetFromUserInput( crs, definition.toLocal8Bit().constData() ) == OGRERR_NONE )
{
Expand Down
14 changes: 10 additions & 4 deletions src/core/qgscoordinatereferencesystem.h
Expand Up @@ -251,8 +251,11 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* and proj strings are a lossy format.
* \param id The ID valid for the chosen CRS ID type
* \param type One of the types described in CrsType
* \deprecated We encourage you to use EPSG codes or WKT to describe CRSes in your code
* wherever possible. Internal QGIS CRS IDs are not guaranteed to be permanent / involatile,
* and Proj strings are a lossy format.
*/
explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId );
Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;

//! Copy constructor
QgsCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &srs );
Expand Down Expand Up @@ -338,11 +341,11 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
/**
* Sets this CRS by lookup of the given ID in the CRS database.
* \returns TRUE on success else FALSE
* \note We encourage you to use EPSG code or WKT to describe CRSes in your code
* \deprecated We encourage you to use EPSG code or WKT to describe CRSes in your code
* wherever possible. Internal QGIS CRS IDs are not guaranteed to be permanent / involatile,
* and Proj strings are a lossy format.
*/
bool createFromId( long id, CrsType type = PostgisCrsId );
Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;

// TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())

Expand Down Expand Up @@ -394,6 +397,8 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* \returns TRUE on success else FALSE
* \note this method uses an internal cache. Call invalidateCache() to clear the cache.
* \see fromSrsId()
* \warning This method is highly discouraged, and CRS objects should instead be constructed
* using auth:id codes or WKT strings
*/
bool createFromSrsId( long srsId );

Expand Down Expand Up @@ -492,8 +497,9 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* \note This function sets CPL config option GDAL_FIX_ESRI_WKT to a proper value,
* unless it has been set by the user through the commandline or an environment variable.
* For more details refer to OGRSpatialReference::morphFromESRI() .
* \deprecated Not used on builds based on Proj version 6 or later
*/
static void setupESRIWktFix();
Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;

//! Returns whether this CRS is correctly initialized and usable
bool isValid() const;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgscoordinateutils.cpp
Expand Up @@ -81,7 +81,7 @@ QString QgsCoordinateUtils::formatCoordinateForProject( QgsProject *project, con
if ( destCrs.isValid() && !destCrs.isGeographic() )
{
// need to transform to geographic coordinates
QgsCoordinateTransform ct( destCrs, QgsCoordinateReferenceSystem( GEOSRID ), project );
QgsCoordinateTransform ct( destCrs, QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), project );
try
{
geo = ct.transform( point );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsdistancearea.cpp
Expand Up @@ -48,7 +48,7 @@ QgsDistanceArea::QgsDistanceArea()
mSemiMinor = -1.0;
mInvFlattening = -1.0;
QgsCoordinateTransformContext context; // this is ok - by default we have a source/dest of WGS84, so no reprojection takes place
setSourceCrs( QgsCoordinateReferenceSystem::fromSrsId( GEOCRS_ID ), context ); // WGS 84
setSourceCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), context ); // WGS 84
setEllipsoid( GEO_NONE );
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsjsonutils.cpp
Expand Up @@ -40,7 +40,7 @@ QgsJsonExporter::QgsJsonExporter( QgsVectorLayer *vectorLayer, int precision )
mCrs = vectorLayer->crs();
mTransform.setSourceCrs( mCrs );
}
mTransform.setDestinationCrs( QgsCoordinateReferenceSystem( 4326, QgsCoordinateReferenceSystem::EpsgCrsId ) );
mTransform.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
}

void QgsJsonExporter::setVectorLayer( QgsVectorLayer *vectorLayer )
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/coordinate_capture/coordinatecapture.cpp
Expand Up @@ -74,7 +74,7 @@ CoordinateCapture::CoordinateCapture( QgisInterface *qgisInterface )
*/
void CoordinateCapture::initGui()
{
mCrs.createFromSrsId( GEOCRS_ID ); // initialize the CRS object
mCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ); // initialize the CRS object

connect( mQGisIface->mapCanvas(), &QgsMapCanvas::destinationCrsChanged, this, &CoordinateCapture::setSourceCrs );
connect( mQGisIface, &QgisInterface::currentThemeChanged, this, &CoordinateCapture::setCurrentTheme );
Expand Down
4 changes: 2 additions & 2 deletions src/server/services/wfs/qgswfsgetfeature.cpp
Expand Up @@ -122,7 +122,7 @@ namespace QgsWfs
QgsCoordinateReferenceSystem requestCrs;
int requestPrecision = 6;
if ( !onlyOneLayer )
requestCrs = QgsCoordinateReferenceSystem( 4326, QgsCoordinateReferenceSystem::EpsgCrsId );
requestCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );

QList<getFeatureQuery>::iterator qIt = aRequest.queries.begin();
for ( ; qIt != aRequest.queries.end(); ++qIt )
Expand Down Expand Up @@ -1091,7 +1091,7 @@ namespace QgsWfs
QgsGeometry exportGeom = QgsGeometry::fromRect( *rect );
QgsCoordinateTransform transform;
transform.setSourceCrs( crs );
transform.setDestinationCrs( QgsCoordinateReferenceSystem( 4326, QgsCoordinateReferenceSystem::EpsgCrsId ) );
transform.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
try
{
if ( exportGeom.transform( transform ) == 0 )
Expand Down
12 changes: 4 additions & 8 deletions tests/src/analysis/testqgsrastercalculator.cpp
Expand Up @@ -447,8 +447,7 @@ void TestQgsRasterCalculator::calcWithLayers()
QVector<QgsRasterCalculatorEntry> entries;
entries << entry1 << entry2;

QgsCoordinateReferenceSystem crs;
crs.createFromId( 32633, QgsCoordinateReferenceSystem::EpsgCrsId );
QgsCoordinateReferenceSystem crs( QStringLiteral( "EPSG:323633" ) );
QgsRectangle extent( 783235, 3348110, 783350, 3347960 );

QTemporaryFile tmpFile;
Expand Down Expand Up @@ -515,8 +514,7 @@ void TestQgsRasterCalculator::calcWithReprojectedLayers()
QVector<QgsRasterCalculatorEntry> entries;
entries << entry1 << entry2;

QgsCoordinateReferenceSystem crs;
crs.createFromId( 32633, QgsCoordinateReferenceSystem::EpsgCrsId );
QgsCoordinateReferenceSystem crs( QStringLiteral( "EPSG:323633" ) );
QgsRectangle extent( 783235, 3348110, 783350, 3347960 );

QTemporaryFile tmpFile;
Expand Down Expand Up @@ -637,8 +635,7 @@ void TestQgsRasterCalculator::errors( )
QVector<QgsRasterCalculatorEntry> entries;
entries << entry1;

QgsCoordinateReferenceSystem crs;
crs.createFromId( 32633, QgsCoordinateReferenceSystem::EpsgCrsId );
QgsCoordinateReferenceSystem crs( QStringLiteral( "EPSG:323633" ) );
QgsRectangle extent( 783235, 3348110, 783350, 3347960 );

QTemporaryFile tmpFile;
Expand Down Expand Up @@ -763,8 +760,7 @@ void TestQgsRasterCalculator::calcFormulasWithReprojectedLayers()
QVector<QgsRasterCalculatorEntry> entries;
entries << entry1 << entry2;

QgsCoordinateReferenceSystem crs;
crs.createFromId( 32633, QgsCoordinateReferenceSystem::EpsgCrsId );
QgsCoordinateReferenceSystem crs( QStringLiteral( "EPSG:323633" ) );
QgsRectangle extent( 783235, 3348110, 783350, 3347960 );


Expand Down
3 changes: 1 addition & 2 deletions tests/src/app/testqgisappclipboard.cpp
Expand Up @@ -399,8 +399,7 @@ void TestQgisAppClipboard::clipboardLogic()
feats.addFeature( feat );
feats.addFeature( feat2 );
feats.setFields( fields );
QgsCoordinateReferenceSystem crs;
crs.createFromSrsId( 3452 );
QgsCoordinateReferenceSystem crs( QStringLiteral( "EPSG:4326" ) );
feats.setCrs( crs );
mQgisApp->clipboard()->replaceWithCopyOf( feats );

Expand Down
2 changes: 1 addition & 1 deletion tests/src/app/testqgsattributetable.cpp
Expand Up @@ -331,7 +331,7 @@ void TestQgsAttributeTable::testRegression15974()
QString path = QDir::tempPath() + "/testshp15974.shp";
std::unique_ptr< QgsVectorLayer> tempLayer( new QgsVectorLayer( QStringLiteral( "polygon?crs=epsg:4326&field=id:integer" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
QVERIFY( tempLayer->isValid() );
QgsVectorFileWriter::writeAsVectorFormat( tempLayer.get(), path, QStringLiteral( "system" ), QgsCoordinateReferenceSystem( 4326 ), QStringLiteral( "ESRI Shapefile" ) );
QgsVectorFileWriter::writeAsVectorFormat( tempLayer.get(), path, QStringLiteral( "system" ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), QStringLiteral( "ESRI Shapefile" ) );
std::unique_ptr< QgsVectorLayer> shpLayer( new QgsVectorLayer( path, QStringLiteral( "test" ), QStringLiteral( "ogr" ) ) );
QgsFeature f1( shpLayer->dataProvider()->fields(), 1 );
QgsGeometry geom;
Expand Down
7 changes: 3 additions & 4 deletions tests/src/core/testqgscoordinatereferencesystem.cpp
Expand Up @@ -200,7 +200,7 @@ void TestQgsCoordinateReferenceSystem::copyCtor()
QCOMPARE( myCrs2.authid(), QStringLiteral( "EPSG:4326" ) );

//test implicit sharing detachment - modify original
myCrs.createFromId( 3111, QgsCoordinateReferenceSystem::EpsgCrsId );
myCrs.createFromString( QStringLiteral( "EPSG:3111" ) ) );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.authid(), QStringLiteral( "EPSG:3111" ) );
QVERIFY( myCrs2.isValid() );
Expand All @@ -217,7 +217,7 @@ void TestQgsCoordinateReferenceSystem::assignmentCtor()
QCOMPARE( myCrs2.authid(), QStringLiteral( "EPSG:4326" ) );

//test implicit sharing detachment - modify original
myCrs.createFromId( 3111, QgsCoordinateReferenceSystem::EpsgCrsId );
myCrs.createFromString( QStringLiteral( "EPSG:3111" ) );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.authid(), QStringLiteral( "EPSG:3111" ) );
QVERIFY( myCrs2.isValid() );
Expand Down Expand Up @@ -1092,8 +1092,7 @@ void TestQgsCoordinateReferenceSystem::isGeographic()
QgsCoordinateReferenceSystem geographic( QStringLiteral( "EPSG:4326" ) );
QVERIFY( geographic.isGeographic() );

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

0 comments on commit fc06fce

Please sign in to comment.