Skip to content

Commit a0073a3

Browse files
committed
Better handling of pre 3.0 project CRS upgrades
Project CRS is inconsistently used for 2.x projects, so always take the main canvas CRS as the project CRS when opening a 2.x project (or invalid CRS if OTF disabled)
1 parent 4b53ac6 commit a0073a3

File tree

2 files changed

+111
-10
lines changed

2 files changed

+111
-10
lines changed

src/core/qgsproject.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ void QgsProject::setCrs( const QgsCoordinateReferenceSystem &crs )
428428
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectCRSProj4String" ), crs.toProj4() );
429429
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectCRSID" ), static_cast< int >( crs.srsid() ) );
430430
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectCrs" ), crs.authid() );
431+
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectionsEnabled" ), crs.isValid() ? 1 : 0 );
431432
setDirty( true );
432433
emit crsChanged();
433434
}

src/core/qgsprojectfiletransform.cpp

+110-10
Original file line numberDiff line numberDiff line change
@@ -617,28 +617,128 @@ void QgsProjectFileTransform::transform2200to2300()
617617

618618
void QgsProjectFileTransform::transform2990()
619619
{
620-
QDomNodeList srsNodes = mDom.elementsByTagName( QStringLiteral( "SpatialRefSys" ) );
620+
// transform OTF off to "no projection" for project
621+
QDomElement propsElem = mDom.firstChildElement( QStringLiteral( "qgis" ) ).toElement().firstChildElement( QStringLiteral( "properties" ) );
622+
QDomNodeList srsNodes = propsElem.elementsByTagName( QStringLiteral( "SpatialRefSys" ) );
623+
QDomElement srsElem;
624+
QDomElement projElem;
621625
if ( srsNodes.count() > 0 )
622626
{
623-
QDomElement srsElem = srsNodes.at( 0 ).toElement();
624-
QDomNodeList projNodes = srsElem.elementsByTagName( "ProjectionsEnabled" );
627+
srsElem = srsNodes.at( 0 ).toElement();
628+
QDomNodeList projNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectionsEnabled" ) );
625629
if ( projNodes.count() == 0 )
626630
{
627-
QDomElement projElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
628-
projElem.setAttribute( "type", "int" );
629-
projElem.setNodeValue( "0" );
631+
projElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
632+
projElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
633+
QDomText projText = mDom.createTextNode( QStringLiteral( "0" ) );
634+
projElem.appendChild( projText );
630635
srsElem.appendChild( projElem );
631636
}
632637
}
633638
else
634639
{
635-
QDomElement srsElem = mDom.createElement( QStringLiteral( "SpatialRefSys" ) );
636-
mDom.appendChild( srsElem );
637-
QDomElement projElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
640+
srsElem = mDom.createElement( QStringLiteral( "SpatialRefSys" ) );
641+
projElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
638642
projElem.setAttribute( "type", "int" );
639-
projElem.setNodeValue( "0" );
643+
QDomText projText = mDom.createTextNode( QStringLiteral( "0" ) );
644+
projElem.appendChild( projText );
640645
srsElem.appendChild( projElem );
646+
propsElem.appendChild( srsElem );
641647
}
648+
// transform map canvas CRS to project CRS - this is because project CRS was inconsistently used
649+
// prior to 3.0. In >= 3.0 main canvas CRS is forced to match project CRS, so we need to make
650+
// sure we can read the project CRS correctly
651+
QDomNodeList canvasNodes = mDom.elementsByTagName( QStringLiteral( "mapcanvas" ) );
652+
if ( canvasNodes.count() > 0 )
653+
{
654+
QDomElement canvasElem = canvasNodes.at( 0 ).toElement();
655+
QDomNodeList projectionsNodes = canvasElem.elementsByTagName( QStringLiteral( "projections" ) );
656+
bool hasOtf = false;
657+
if ( projectionsNodes.count() > 0 )
658+
{
659+
QDomElement projectionsElem = projectionsNodes.at( 0 ).toElement();
660+
hasOtf = projectionsElem.text().toInt();
661+
}
662+
663+
QDomNodeList canvasSrsNodes = canvasElem.elementsByTagName( QStringLiteral( "spatialrefsys" ) );
664+
if ( canvasSrsNodes.count() > 0 )
665+
{
666+
QDomElement canvasSrsElem = canvasSrsNodes.at( 0 ).toElement();
667+
QString proj;
668+
QString authid;
669+
QString srsid;
670+
671+
QDomNodeList proj4Nodes = canvasSrsElem.elementsByTagName( QStringLiteral( "proj4" ) );
672+
if ( proj4Nodes.count() > 0 )
673+
{
674+
QDomElement proj4Node = proj4Nodes.at( 0 ).toElement();
675+
proj = proj4Node.text();
676+
}
677+
QDomNodeList authidNodes = canvasSrsElem.elementsByTagName( QStringLiteral( "authid" ) );
678+
if ( authidNodes.count() > 0 )
679+
{
680+
QDomElement authidNode = authidNodes.at( 0 ).toElement();
681+
authid = authidNode.text();
682+
}
683+
QDomNodeList srsidNodes = canvasSrsElem.elementsByTagName( QStringLiteral( "srsid" ) );
684+
if ( srsidNodes.count() > 0 )
685+
{
686+
QDomElement srsidNode = srsidNodes.at( 0 ).toElement();
687+
srsid = srsidNode.text();
688+
}
689+
690+
// clear existing project CRS nodes
691+
QDomNodeList oldProjectProj4Nodes = srsElem.elementsByTagName( QStringLiteral( "ProjectCRSProj4String" ) );
692+
for ( int i = oldProjectProj4Nodes.count(); i >= 0; --i )
693+
{
694+
srsElem.removeChild( oldProjectProj4Nodes.at( i ) );
695+
}
696+
QDomNodeList oldProjectCrsNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectCrs" ) );
697+
for ( int i = oldProjectCrsNodes.count(); i >= 0; --i )
698+
{
699+
srsElem.removeChild( oldProjectCrsNodes.at( i ) );
700+
}
701+
QDomNodeList oldProjectCrsIdNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectCRSID" ) );
702+
for ( int i = oldProjectCrsIdNodes.count(); i >= 0; --i )
703+
{
704+
srsElem.removeChild( oldProjectCrsIdNodes.at( i ) );
705+
}
706+
QDomNodeList projectionsEnabledNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectionsEnabled" ) );
707+
for ( int i = projectionsEnabledNodes.count(); i >= 0; --i )
708+
{
709+
srsElem.removeChild( projectionsEnabledNodes.at( i ) );
710+
}
711+
712+
QDomElement proj4Elem = mDom.createElement( QStringLiteral( "ProjectCRSProj4String" ) );
713+
proj4Elem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QString" ) );
714+
QDomText proj4Text = mDom.createTextNode( proj );
715+
proj4Elem.appendChild( proj4Text );
716+
QDomElement projectCrsElem = mDom.createElement( QStringLiteral( "ProjectCrs" ) );
717+
projectCrsElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QString" ) );
718+
QDomText projectCrsText = mDom.createTextNode( authid );
719+
projectCrsElem.appendChild( projectCrsText );
720+
QDomElement projectCrsIdElem = mDom.createElement( QStringLiteral( "ProjectCRSID" ) );
721+
projectCrsIdElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
722+
QDomText srsidText = mDom.createTextNode( srsid );
723+
projectCrsIdElem.appendChild( srsidText );
724+
QDomElement projectionsEnabledElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
725+
projectionsEnabledElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
726+
QDomText projectionsEnabledText = mDom.createTextNode( hasOtf ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
727+
projectionsEnabledElem.appendChild( projectionsEnabledText );
728+
srsElem.appendChild( proj4Elem );
729+
srsElem.appendChild( projectCrsElem );
730+
srsElem.appendChild( projectCrsIdElem );
731+
srsElem.appendChild( projectionsEnabledElem );
732+
733+
QDomNodeList srsNodes = propsElem.elementsByTagName( QStringLiteral( "SpatialRefSys" ) );
734+
for ( int i = srsNodes.count(); i >= 0; --i )
735+
{
736+
propsElem.removeChild( srsNodes.at( i ) );
737+
}
738+
propsElem.appendChild( srsElem );
739+
}
740+
}
741+
642742

643743
QDomNodeList mapLayers = mDom.elementsByTagName( QStringLiteral( "maplayer" ) );
644744

0 commit comments

Comments
 (0)