Skip to content
Permalink
Browse files
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)
  • Loading branch information
nyalldawson committed Mar 15, 2017
1 parent 4b53ac6 commit a0073a381a76aa27923529bbccf8c3cafd43ee0e
Showing with 111 additions and 10 deletions.
  1. +1 −0 src/core/qgsproject.cpp
  2. +110 −10 src/core/qgsprojectfiletransform.cpp
@@ -428,6 +428,7 @@ void QgsProject::setCrs( const QgsCoordinateReferenceSystem &crs )
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectCRSProj4String" ), crs.toProj4() );
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectCRSID" ), static_cast< int >( crs.srsid() ) );
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectCrs" ), crs.authid() );
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectionsEnabled" ), crs.isValid() ? 1 : 0 );
setDirty( true );
emit crsChanged();
}
@@ -617,28 +617,128 @@ void QgsProjectFileTransform::transform2200to2300()

void QgsProjectFileTransform::transform2990()
{
QDomNodeList srsNodes = mDom.elementsByTagName( QStringLiteral( "SpatialRefSys" ) );
// transform OTF off to "no projection" for project
QDomElement propsElem = mDom.firstChildElement( QStringLiteral( "qgis" ) ).toElement().firstChildElement( QStringLiteral( "properties" ) );
QDomNodeList srsNodes = propsElem.elementsByTagName( QStringLiteral( "SpatialRefSys" ) );
QDomElement srsElem;
QDomElement projElem;
if ( srsNodes.count() > 0 )
{
QDomElement srsElem = srsNodes.at( 0 ).toElement();
QDomNodeList projNodes = srsElem.elementsByTagName( "ProjectionsEnabled" );
srsElem = srsNodes.at( 0 ).toElement();
QDomNodeList projNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectionsEnabled" ) );
if ( projNodes.count() == 0 )
{
QDomElement projElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
projElem.setAttribute( "type", "int" );
projElem.setNodeValue( "0" );
projElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
projElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
QDomText projText = mDom.createTextNode( QStringLiteral( "0" ) );
projElem.appendChild( projText );
srsElem.appendChild( projElem );
}
}
else
{
QDomElement srsElem = mDom.createElement( QStringLiteral( "SpatialRefSys" ) );
mDom.appendChild( srsElem );
QDomElement projElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
srsElem = mDom.createElement( QStringLiteral( "SpatialRefSys" ) );
projElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
projElem.setAttribute( "type", "int" );
projElem.setNodeValue( "0" );
QDomText projText = mDom.createTextNode( QStringLiteral( "0" ) );
projElem.appendChild( projText );
srsElem.appendChild( projElem );
propsElem.appendChild( srsElem );
}
// transform map canvas CRS to project CRS - this is because project CRS was inconsistently used
// prior to 3.0. In >= 3.0 main canvas CRS is forced to match project CRS, so we need to make
// sure we can read the project CRS correctly
QDomNodeList canvasNodes = mDom.elementsByTagName( QStringLiteral( "mapcanvas" ) );
if ( canvasNodes.count() > 0 )
{
QDomElement canvasElem = canvasNodes.at( 0 ).toElement();
QDomNodeList projectionsNodes = canvasElem.elementsByTagName( QStringLiteral( "projections" ) );
bool hasOtf = false;
if ( projectionsNodes.count() > 0 )
{
QDomElement projectionsElem = projectionsNodes.at( 0 ).toElement();
hasOtf = projectionsElem.text().toInt();
}

QDomNodeList canvasSrsNodes = canvasElem.elementsByTagName( QStringLiteral( "spatialrefsys" ) );
if ( canvasSrsNodes.count() > 0 )
{
QDomElement canvasSrsElem = canvasSrsNodes.at( 0 ).toElement();
QString proj;
QString authid;
QString srsid;

QDomNodeList proj4Nodes = canvasSrsElem.elementsByTagName( QStringLiteral( "proj4" ) );
if ( proj4Nodes.count() > 0 )
{
QDomElement proj4Node = proj4Nodes.at( 0 ).toElement();
proj = proj4Node.text();
}
QDomNodeList authidNodes = canvasSrsElem.elementsByTagName( QStringLiteral( "authid" ) );
if ( authidNodes.count() > 0 )
{
QDomElement authidNode = authidNodes.at( 0 ).toElement();
authid = authidNode.text();
}
QDomNodeList srsidNodes = canvasSrsElem.elementsByTagName( QStringLiteral( "srsid" ) );
if ( srsidNodes.count() > 0 )
{
QDomElement srsidNode = srsidNodes.at( 0 ).toElement();
srsid = srsidNode.text();
}

// clear existing project CRS nodes
QDomNodeList oldProjectProj4Nodes = srsElem.elementsByTagName( QStringLiteral( "ProjectCRSProj4String" ) );
for ( int i = oldProjectProj4Nodes.count(); i >= 0; --i )
{
srsElem.removeChild( oldProjectProj4Nodes.at( i ) );
}
QDomNodeList oldProjectCrsNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectCrs" ) );
for ( int i = oldProjectCrsNodes.count(); i >= 0; --i )
{
srsElem.removeChild( oldProjectCrsNodes.at( i ) );
}
QDomNodeList oldProjectCrsIdNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectCRSID" ) );
for ( int i = oldProjectCrsIdNodes.count(); i >= 0; --i )
{
srsElem.removeChild( oldProjectCrsIdNodes.at( i ) );
}
QDomNodeList projectionsEnabledNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectionsEnabled" ) );
for ( int i = projectionsEnabledNodes.count(); i >= 0; --i )
{
srsElem.removeChild( projectionsEnabledNodes.at( i ) );
}

QDomElement proj4Elem = mDom.createElement( QStringLiteral( "ProjectCRSProj4String" ) );
proj4Elem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QString" ) );
QDomText proj4Text = mDom.createTextNode( proj );
proj4Elem.appendChild( proj4Text );
QDomElement projectCrsElem = mDom.createElement( QStringLiteral( "ProjectCrs" ) );
projectCrsElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QString" ) );
QDomText projectCrsText = mDom.createTextNode( authid );
projectCrsElem.appendChild( projectCrsText );
QDomElement projectCrsIdElem = mDom.createElement( QStringLiteral( "ProjectCRSID" ) );
projectCrsIdElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
QDomText srsidText = mDom.createTextNode( srsid );
projectCrsIdElem.appendChild( srsidText );
QDomElement projectionsEnabledElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
projectionsEnabledElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
QDomText projectionsEnabledText = mDom.createTextNode( hasOtf ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
projectionsEnabledElem.appendChild( projectionsEnabledText );
srsElem.appendChild( proj4Elem );
srsElem.appendChild( projectCrsElem );
srsElem.appendChild( projectCrsIdElem );
srsElem.appendChild( projectionsEnabledElem );

QDomNodeList srsNodes = propsElem.elementsByTagName( QStringLiteral( "SpatialRefSys" ) );
for ( int i = srsNodes.count(); i >= 0; --i )
{
propsElem.removeChild( srsNodes.at( i ) );
}
propsElem.appendChild( srsElem );
}
}


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

0 comments on commit a0073a3

Please sign in to comment.