Skip to content
Permalink
Browse files

Prefer WKT2 when exporting CRS for use by GDAL

  • Loading branch information
nyalldawson committed Dec 19, 2019
1 parent ac47631 commit f77af382a03a1f8c1ad55f711ac9bd53c0fcb5d9
@@ -177,7 +177,7 @@ QVariantMap QgsRasterizeAlgorithm::processAlgorithm( const QVariantMap &paramete
throw QgsProcessingException( QObject::tr( "Error creating GDAL output layer" ) );
}

GDALSetProjection( hOutputDataset.get(), context.project()->crs().toWkt().toLatin1().constData() );
GDALSetProjection( hOutputDataset.get(), context.project()->crs().toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ).toLatin1().constData() );
double geoTransform[6];
geoTransform[0] = extent.xMinimum();
geoTransform[1] = mapUnitsPerPixel;
@@ -397,7 +397,7 @@ int QgsAlignRaster::suggestedReferenceLayer() const
// would be a better a choice to more accurately compute areas?
// (Why earth is not flat???)
QgsCoordinateReferenceSystem destCRS( QStringLiteral( "EPSG:4326" ) );
QString destWkt = destCRS.toWkt();
QString destWkt = destCRS.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 );

const auto constMRasters = mRasters;
for ( const Item &raster : constMRasters )
@@ -146,7 +146,7 @@ QgsRasterCalculator::Result QgsRasterCalculator::processCalculation( QgsFeedback
return CreateOutputError;
}

GDALSetProjection( outputDataset.get(), mOutputCrs.toWkt().toLocal8Bit().data() );
GDALSetProjection( outputDataset.get(), mOutputCrs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ).toLocal8Bit().data() );
GDALRasterBandH outputRasterBand = GDALGetRasterBand( outputDataset.get(), 1 );

float outputNodataValue = -FLT_MAX;
@@ -526,7 +526,7 @@ QgsRasterCalculator::Result QgsRasterCalculator::processCalculationGPU( std::uni
return CreateOutputError;
}

GDALSetProjection( outputDataset.get(), mOutputCrs.toWkt().toLocal8Bit().data() );
GDALSetProjection( outputDataset.get(), mOutputCrs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ).toLocal8Bit().data() );

GDALRasterBandH outputRasterBand = GDALGetRasterBand( outputDataset.get(), 1 );
if ( !outputRasterBand )
@@ -77,7 +77,7 @@ QgsDwgImporter::QgsDwgImporter( const QString &database, const QgsCoordinateRefe
{
QgsDebugCall;

QString crswkt( crs.toWkt() );
QString crswkt( crs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) );
mCrsH = OSRNewSpatialReference( crswkt.toLocal8Bit().constData() );
QgsDebugMsg( QStringLiteral( "CRS %1[%2]: %3" ).arg( mCrs ).arg( ( qint64 ) mCrsH, 0, 16 ).arg( crswkt ) );
}
@@ -196,7 +196,7 @@ void QgsAlignRasterDialog::updateParametersFromReferenceLayer()
{
QgsCoordinateReferenceSystem refCRS( refInfo.crs() );
if ( refCRS != mCrsSelector->crs() )
customCRSWkt = mCrsSelector->crs().toWkt();
customCRSWkt = mCrsSelector->crs( ).toWkt( QgsCoordinateReferenceSystem::WKT2_2018 );
}

if ( mChkCustomCellSize->isChecked() )
@@ -313,7 +313,7 @@ void QgsAlignRasterDialog::referenceLayerChanged()

void QgsAlignRasterDialog::destinationCrsChanged()
{
if ( mCrsSelector->crs().toWkt() == mAlign->destinationCrs() )
if ( mCrsSelector->crs().toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) == mAlign->destinationCrs() )
return;

int index = mCboReferenceLayer->currentIndex();
@@ -267,7 +267,7 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer *lyr, QgsMapCanv
layout->addWidget( mMetadataWidget );
metadataFrame->setLayout( layout );

QgsDebugMsg( "Setting crs to " + mRasterLayer->crs().toWkt() );
QgsDebugMsg( "Setting crs to " + mRasterLayer->crs().toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) );
QgsDebugMsg( "Setting crs to " + mRasterLayer->crs().userFriendlyIdentifier() );
mCrsSelector->setCrs( mRasterLayer->crs() );

@@ -345,7 +345,7 @@ QgsExpressionContextScope *QgsExpressionContextUtils::mapSettingsScope( const Qg
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_acronym" ), mapSettings.destinationCrs().projectionAcronym(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_ellipsoid" ), mapSettings.destinationCrs().ellipsoidAcronym(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_proj4" ), mapSettings.destinationCrs().toProj(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapSettings.destinationCrs().toWkt(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapSettings.destinationCrs().toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ), true ) );

// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )
@@ -1592,7 +1592,7 @@ bool QgsLayoutExporter::georeferenceOutputPrivate( const QString &file, QgsLayou
}

if ( t )
GDALSetProjection( outputDS.get(), map->crs().toWkt().toLocal8Bit().constData() );
GDALSetProjection( outputDS.get(), map->crs().toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ).toLocal8Bit().constData() );
}
CPLSetConfigOption( "GDAL_PDF_DPI", nullptr );

@@ -1495,7 +1495,7 @@ QgsExpressionContext QgsLayoutItemMap::createExpressionContext() const
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_acronym" ), mapCrs.projectionAcronym(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_ellipsoid" ), mapCrs.ellipsoidAcronym(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_proj4" ), mapCrs.toProj(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapCrs.toWkt(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapCrs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ), true ) );

QVariantList layersIds;
QVariantList layers;
@@ -59,7 +59,7 @@ bool QgsGdalLayerItem::setCrs( const QgsCoordinateReferenceSystem &crs )
if ( !hDS )
return false;

QString wkt = crs.toWkt();
QString wkt = crs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 );
if ( GDALSetProjection( hDS.get(), wkt.toLocal8Bit().data() ) != CE_None )
{
QgsDebugMsg( QStringLiteral( "Could not set CRS" ) );
@@ -2947,7 +2947,7 @@ QgsGdalProvider *QgsGdalProviderMetadata::createRasterDataProvider(
}

GDALSetGeoTransform( dataset.get(), geoTransform );
GDALSetProjection( dataset.get(), crs.toWkt().toLocal8Bit().data() );
GDALSetProjection( dataset.get(), crs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ).toLocal8Bit().data() );

QgsDataProvider::ProviderOptions providerOptions;
return new QgsGdalProvider( uri, providerOptions, true, dataset.release() );
@@ -253,7 +253,7 @@ QString QgsMemoryProvider::dataSourceUri( bool expandAuthConfig ) const
}
else
{
crsDef = QStringLiteral( "wkt:%1" ).arg( mCrs.toWkt() );
crsDef = QStringLiteral( "wkt:%1" ).arg( mCrs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) );
}
uri.addQueryItem( QStringLiteral( "crs" ), crsDef );
}
@@ -3504,7 +3504,7 @@ bool QgsOgrProviderUtils::createEmptyDataSource( const QString &uri,
mySpatialRefSys.validate();
}

QString myWkt = mySpatialRefSys.toWkt();
QString myWkt = mySpatialRefSys.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 );

if ( !myWkt.isNull() && myWkt.length() != 0 )
{
@@ -1195,8 +1195,8 @@ QString QgsCoordinateReferenceSystem::userFriendlyIdentifier( bool shortString )
return description();
else if ( shortString )
return QObject::tr( "Unknown CRS" );
else if ( !toWkt().isEmpty() )
return QObject::tr( "Unknown CRS: %1" ).arg( toWkt().left( 50 ) + QString( QChar( 0x2026 ) ) );
else if ( !toWkt( WKT2_2018 ).isEmpty() )
return QObject::tr( "Unknown CRS: %1" ).arg( toWkt( WKT2_2018 ).left( 50 ) + QString( QChar( 0x2026 ) ) );
else if ( !toProj().isEmpty() )
return QObject::tr( "Unknown CRS: %1" ).arg( toProj().left( 50 ) + QString( QChar( 0x2026 ) ) );
else
@@ -1826,7 +1826,7 @@ bool QgsCoordinateReferenceSystem::operator!=( const QgsCoordinateReferenceSyste

QString QgsCoordinateReferenceSystem::toWkt( WktVariant variant, bool multiline, int indentationWidth ) const
{
if ( d->mWkt.isEmpty() )
if ( d->mWkt.isEmpty() || variant != WKT1_GDAL )
{
#if PROJ_VERSION_MAJOR>=6
if ( d->mPj )
@@ -1857,7 +1857,11 @@ QString QgsCoordinateReferenceSystem::toWkt( WktVariant variant, bool multiline,
const QByteArray multiLineOption = QStringLiteral( "MULTILINE=%1" ).arg( multiline ? QStringLiteral( "YES" ) : QStringLiteral( "NO" ) ).toLocal8Bit();
const QByteArray indentatationWidthOption = QStringLiteral( "INDENTATION_WIDTH=%1" ).arg( multiline ? QString::number( indentationWidth ) : QStringLiteral( "0" ) ).toLocal8Bit();
const char *const options[] = {multiLineOption.constData(), indentatationWidthOption.constData(), nullptr};
d->mWkt = QString( proj_as_wkt( QgsProjContext::get(), d->mPj.get(), type, options ) );
const QString res = QString( proj_as_wkt( QgsProjContext::get(), d->mPj.get(), type, options ) );
if ( variant == WKT1_GDAL )
d->mWkt = res;

return res;
}
#else
Q_UNUSED( variant )
@@ -1869,9 +1873,9 @@ QString QgsCoordinateReferenceSystem::toWkt( WktVariant variant, bool multiline,
d->mWkt = wkt;
CPLFree( wkt );
}
return d->mWkt;
#endif
}
return d->mWkt;
}

bool QgsCoordinateReferenceSystem::readXml( const QDomNode &node )
@@ -1984,7 +1988,7 @@ bool QgsCoordinateReferenceSystem::writeXml( QDomNode &node, QDomDocument &doc )
QDomElement srsElement = doc.createElement( QStringLiteral( "spatialrefsys" ) );

QDomElement wktElement = doc.createElement( QStringLiteral( "wkt" ) );
wktElement.appendChild( doc.createTextNode( toWkt() ) );
wktElement.appendChild( doc.createTextNode( toWkt( WKT2_2018 ) ) );
srsElement.appendChild( wktElement );

QDomElement proj4Element = doc.createElement( QStringLiteral( "proj4" ) );
@@ -2124,7 +2128,7 @@ void QgsCoordinateReferenceSystem::debugPrint()
QgsDebugMsg( "* Valid : " + ( d->mIsValid ? QString( "true" ) : QString( "false" ) ) );
QgsDebugMsg( "* SrsId : " + QString::number( d->mSrsId ) );
QgsDebugMsg( "* Proj4 : " + toProj() );
QgsDebugMsg( "* WKT : " + toWkt() );
QgsDebugMsg( "* WKT : " + toWkt( WKT2_2018 ) );
QgsDebugMsg( "* Desc. : " + d->mDescription );
if ( mapUnits() == QgsUnitTypes::DistanceMeters )
{
@@ -2169,11 +2173,7 @@ long QgsCoordinateReferenceSystem::saveAsUserCrs( const QString &name, Format na
{
proj4String = toProj();
}
QString wktString = d->mWkt;
if ( wktString.isEmpty() )
{
wktString = toWkt();
}
QString wktString = toWkt( WKT2_2018 );

// ellipsoid acroynym column is incorrectly marked as not null in many crs database instances,
// hack around this by using an empty string instead
@@ -3441,7 +3441,7 @@ void QgsCoordinateReferenceSystem::pushRecentCoordinateReferenceSystem( const Qg
{
authids << c.authid();
proj << c.toProj();
wkt << c.toWkt();
wkt << c.toWkt( WKT2_2018 );
}

QgsSettings settings;
@@ -835,9 +835,9 @@ bool QgsCoordinateTransform::setFromCache( const QgsCoordinateReferenceSystem &s
return false;

const QString sourceKey = src.authid().isEmpty() ?
src.toWkt() : src.authid();
src.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) : src.authid();
const QString destKey = dest.authid().isEmpty() ?
dest.toWkt() : dest.authid();
dest.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) : dest.authid();

if ( sourceKey.isEmpty() || destKey.isEmpty() )
return false;
@@ -921,9 +921,9 @@ void QgsCoordinateTransform::addToCache()
return;

const QString sourceKey = d->mSourceCRS.authid().isEmpty() ?
d->mSourceCRS.toWkt() : d->mSourceCRS.authid();
d->mSourceCRS.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) : d->mSourceCRS.authid();
const QString destKey = d->mDestCRS.authid().isEmpty() ?
d->mDestCRS.toWkt() : d->mDestCRS.authid();
d->mDestCRS.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) : d->mDestCRS.authid();

if ( sourceKey.isEmpty() || destKey.isEmpty() )
return;
@@ -61,7 +61,7 @@ gdal::dataset_unique_ptr QgsGdalUtils::createMultiBandMemoryDataset( GDALDataTyp
geoTransform[4] = 0;
geoTransform[5] = -cellSizeY;

GDALSetProjection( hSrcDS.get(), crs.toWkt().toLatin1().constData() );
GDALSetProjection( hSrcDS.get(), crs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ).toLatin1().constData() );
GDALSetGeoTransform( hSrcDS.get(), geoTransform );
return hSrcDS;
}
@@ -92,7 +92,7 @@ gdal::dataset_unique_ptr QgsGdalUtils::createSingleBandTiffDataset( const QStrin
}

// Write out the projection definition.
GDALSetProjection( hDstDS.get(), crs.toWkt().toLatin1().constData() );
GDALSetProjection( hDstDS.get(), crs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ).toLatin1().constData() );
GDALSetGeoTransform( hDstDS.get(), geoTransform );
return hDstDS;
}
@@ -320,7 +320,7 @@ bool QgsMapRendererTask::run()
f -= 0.5 * e;
double geoTransform[6] = { c, a, b, f, d, e };
GDALSetGeoTransform( outputDS.get(), geoTransform );
GDALSetProjection( outputDS.get(), mMapSettings.destinationCrs().toWkt().toLocal8Bit().constData() );
GDALSetProjection( outputDS.get(), mMapSettings.destinationCrs().toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ).toLocal8Bit().constData() );
}

if ( mExportMetadata )
@@ -396,7 +396,7 @@ bool QgsMapRendererTask::run()
f -= 0.5 * e;
double geoTransform[] = { c, a, b, f, d, e };
GDALSetGeoTransform( outputDS.get(), geoTransform );
GDALSetProjection( outputDS.get(), mMapSettings.destinationCrs().toWkt().toLocal8Bit().constData() );
GDALSetProjection( outputDS.get(), mMapSettings.destinationCrs().toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ).toLocal8Bit().constData() );
}
}

@@ -687,7 +687,7 @@ QgsVectorLayer *QgsOfflineEditing::copyVectorLayer( QgsVectorLayer *layer, sqlit
}

OGRSFDriverH hDriver = nullptr;
OGRSpatialReferenceH hSRS = OSRNewSpatialReference( layer->crs().toWkt().toLocal8Bit().data() );
OGRSpatialReferenceH hSRS = OSRNewSpatialReference( layer->crs().toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ).toLocal8Bit().data() );
gdal::ogr_datasource_unique_ptr hDS( OGROpen( offlineDbPath.toUtf8().constData(), true, &hDriver ) );
OGRLayerH hLayer = OGR_DS_CreateLayer( hDS.get(), tableName.toUtf8().constData(), hSRS, static_cast<OGRwkbGeometryType>( layer->wkbType() ), options );
CSLDestroy( options );
@@ -395,7 +395,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
// consider spatial reference system of the layer
if ( srs.isValid() )
{
QString srsWkt = srs.toWkt();
QString srsWkt = srs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 );
QgsDebugMsg( "WKT to save as is " + srsWkt );
mOgrRef = OSRNewSpatialReference( srsWkt.toLocal8Bit().constData() );
#if GDAL_VERSION_MAJOR >= 3
@@ -643,7 +643,7 @@ void QgsRasterLayer::setDataProvider( QString const &provider, const QgsDataProv
// Setup source CRS
setCrs( QgsCoordinateReferenceSystem( mDataProvider->crs() ) );

QgsDebugMsgLevel( "using wkt:\n" + crs().toWkt(), 4 );
QgsDebugMsgLevel( "using wkt:\n" + crs().toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ), 4 );

//defaults - Needs to be set after the Contrast list has been build
//Try to read the default contrast enhancement from the config file
@@ -388,7 +388,7 @@ bool QgsNewGeoPackageLayerDialog::apply()
QgsCoordinateReferenceSystem srs = mCrsSelector->crs();
if ( wkbType != wkbNone && srs.isValid() )
{
QString srsWkt = srs.toWkt();
QString srsWkt = srs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 );
hSRS = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
}

@@ -68,7 +68,7 @@ QgsDb2Provider::QgsDb2Provider( const QString &uri, const ProviderOptions &optio
mDatabase = getDatabase( uri, errMsg );
mConnInfo = anUri.connectionInfo();
QgsCoordinateReferenceSystem layerCrs = crs();
QgsDebugMsg( "CRS: " + layerCrs.toWkt() );
QgsDebugMsg( "CRS: " + layerCrs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) );

if ( !errMsg.isEmpty() )
{
@@ -1303,7 +1303,7 @@ QgsVectorLayerExporter::ExportError QgsDb2Provider::createEmptyLayer( const QStr
// srs->posgisSrid() seems to return the authority id which is
// most often the EPSG id. Hopefully DB2 has defined an SRS using this
// value as the srid / srs_id. If not, we are out of luck.
QgsDebugMsg( "srs: " + srs.toWkt() );
QgsDebugMsg( "srs: " + srs.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) );
long srid = srs.postgisSrid();
QgsDebugMsg( QStringLiteral( "srid: %1" ).arg( srid ) );
if ( srid >= 0 )
@@ -218,7 +218,7 @@ class TestAlignRaster : public QObject
QgsAlignRaster::List rasters;
rasters << QgsAlignRaster::Item( SRC_FILE, tmpFile );
align.setRasters( rasters );
align.setParametersFromRaster( SRC_FILE, destCRS.toWkt() );
align.setParametersFromRaster( SRC_FILE, destCRS.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) );
bool res = align.run();
QVERIFY( res );

@@ -247,7 +247,7 @@ class TestAlignRaster : public QObject
QgsAlignRaster::List rasters;
rasters << QgsAlignRaster::Item( SRC_FILE, tmpFile );
align.setRasters( rasters );
align.setParametersFromRaster( SRC_FILE, destCRS.toWkt() );
align.setParametersFromRaster( SRC_FILE, destCRS.toWkt( QgsCoordinateReferenceSystem::WKT2_2018 ) );
bool res = align.run();
QVERIFY( !res );
}

0 comments on commit f77af38

Please sign in to comment.
You can’t perform that action at this time.