bool QgsCoordinateTransformPrivate::initialize()
{
  invalidate();
  if ( !mSourceCRS.isValid() )
  {
    // Pass through with no projection since we have no idea what the layer
    // coordinates are and projecting them may not be appropriate
    QgsDebugMsgLevel( QStringLiteral( "Source CRS is invalid!" ), 4 );
    return false;
  }

  if ( !mDestCRS.isValid() )
  {
    //No destination projection is set so we set the default output projection to
    //be the same as input proj.
    mDestCRS = mSourceCRS;
    QgsDebugMsgLevel( QStringLiteral( "Destination CRS is invalid!" ), 4 );
    return false;
  }

  mIsValid = true;

  int sourceDatumTransform = mSourceDatumTransform;
  int destDatumTransform = mDestinationDatumTransform;
  bool useDefaultDatumTransform = ( sourceDatumTransform == - 1 && destDatumTransform == -1 );

  // init the projections (destination and source)
  freeProj();

  mSourceProjString = mSourceCRS.toProj4();
  if ( !useDefaultDatumTransform )
  {
    mSourceProjString = stripDatumTransform( mSourceProjString );
  }
  if ( sourceDatumTransform != -1 )
  {
    mSourceProjString += ( ' ' + QgsDatumTransform::datumTransformToProj( sourceDatumTransform ) );
  }

  mDestProjString = mDestCRS.toProj4();
  if ( !useDefaultDatumTransform )
  {
    mDestProjString = stripDatumTransform( mDestProjString );
  }
  if ( destDatumTransform != -1 )
  {
    mDestProjString += ( ' ' +  QgsDatumTransform::datumTransformToProj( destDatumTransform ) );
  }

  if ( !useDefaultDatumTransform )
  {
    addNullGridShifts( mSourceProjString, mDestProjString, sourceDatumTransform, destDatumTransform );
  }

  // create proj projections for current thread
  QPair<projPJ, projPJ> res = threadLocalProjData();

#ifdef COORDINATE_TRANSFORM_VERBOSE
  QgsDebugMsg( "From proj : " + mSourceCRS.toProj4() );
  QgsDebugMsg( "To proj   : " + mDestCRS.toProj4() );
#endif

  if ( !res.first || !res.second )
  {
    mIsValid = false;
  }

#ifdef COORDINATE_TRANSFORM_VERBOSE
  if ( mIsValid )
  {
    QgsDebugMsg( QStringLiteral( "------------------------------------------------------------" ) );
    QgsDebugMsg( QStringLiteral( "The OGR Coordinate transformation for this layer was set to" ) );
    QgsLogger::debug<QgsCoordinateReferenceSystem>( "Input", mSourceCRS, __FILE__, __FUNCTION__, __LINE__ );
    QgsLogger::debug<QgsCoordinateReferenceSystem>( "Output", mDestCRS, __FILE__, __FUNCTION__, __LINE__ );
    QgsDebugMsg( QStringLiteral( "------------------------------------------------------------" ) );
  }
  else
  {
    QgsDebugMsg( QStringLiteral( "------------------------------------------------------------" ) );
    QgsDebugMsg( QStringLiteral( "The OGR Coordinate transformation FAILED TO INITIALIZE!" ) );
    QgsDebugMsg( QStringLiteral( "------------------------------------------------------------" ) );
  }
#else
  if ( !mIsValid )
  {
    QgsDebugMsg( QStringLiteral( "Coordinate transformation failed to initialize!" ) );
  }
#endif

  //XXX todo overload == operator for QgsCoordinateReferenceSystem
  //at the moment srs.parameters contains the whole proj def...soon it won't...
  //if (mSourceCRS->toProj4() == mDestCRS->toProj4())
  if ( mSourceCRS == mDestCRS )
  {
    // If the source and destination projection are the same, set the short
    // circuit flag (no transform takes place)
    mShortCircuit = true;
    QgsDebugMsgLevel( QStringLiteral( "Source/Dest CRS equal, shortcircuit is set." ), 3 );
  }
  else
  {
    // Transform must take place
    mShortCircuit = false;
    QgsDebugMsgLevel( QStringLiteral( "Source/Dest CRS not equal, shortcircuit is not set." ), 3 );
  }
  return mIsValid;
}
示例#2
0
// XXX This whole function is full of multiple return statements!!!
// And probably shouldn't be a void
void QgsCoordinateTransform::initialise()
{
    // XXX Warning - multiple return paths in this block!!
    if ( !mSourceCRS.isValid() )
    {
        //mSourceCRS = defaultWkt;
        // Pass through with no projection since we have no idea what the layer
        // coordinates are and projecting them may not be appropriate
        mShortCircuit = true;
        QgsDebugMsg( "SourceCRS seemed invalid!" );
        return;
    }

    if ( !mDestCRS.isValid() )
    {
        //No destination projection is set so we set the default output projection to
        //be the same as input proj.
        mDestCRS = QgsCRSCache::instance()->crsByAuthId( mSourceCRS.authid() );
    }

    bool useDefaultDatumTransform = ( mSourceDatumTransform == - 1 && mDestinationDatumTransform == -1 );

    // init the projections (destination and source)

    pj_free( mSourceProjection );
    QString sourceProjString = mSourceCRS.toProj4();
    if ( !useDefaultDatumTransform )
    {
        sourceProjString = stripDatumTransform( sourceProjString );
    }
    if ( mSourceDatumTransform != -1 )
    {
        sourceProjString += ( " " + datumTransformString( mSourceDatumTransform ) );
    }

    pj_free( mDestinationProjection );
    QString destProjString = mDestCRS.toProj4();
    if ( !useDefaultDatumTransform )
    {
        destProjString = stripDatumTransform( destProjString );
    }
    if ( mDestinationDatumTransform != -1 )
    {
        destProjString += ( " " +  datumTransformString( mDestinationDatumTransform ) );
    }

    if ( !useDefaultDatumTransform )
    {
        addNullGridShifts( sourceProjString, destProjString );
    }

    mSourceProjection = pj_init_plus( sourceProjString.toUtf8() );
    mDestinationProjection = pj_init_plus( destProjString.toUtf8() );

#ifdef COORDINATE_TRANSFORM_VERBOSE
    QgsDebugMsg( "From proj : " + mSourceCRS.toProj4() );
    QgsDebugMsg( "To proj   : " + mDestCRS.toProj4() );
#endif

    mInitialisedFlag = true;
    if ( !mDestinationProjection )
    {
        mInitialisedFlag = false;
    }
    if ( !mSourceProjection )
    {
        mInitialisedFlag = false;
    }
#ifdef COORDINATE_TRANSFORM_VERBOSE
    if ( mInitialisedFlag )
    {
        QgsDebugMsg( "------------------------------------------------------------" );
        QgsDebugMsg( "The OGR Coordinate transformation for this layer was set to" );
        QgsLogger::debug<QgsCoordinateReferenceSystem>( "Input", mSourceCRS, __FILE__, __FUNCTION__, __LINE__ );
        QgsLogger::debug<QgsCoordinateReferenceSystem>( "Output", mDestCRS, __FILE__, __FUNCTION__, __LINE__ );
        QgsDebugMsg( "------------------------------------------------------------" );
    }
    else
    {
        QgsDebugMsg( "------------------------------------------------------------" );
        QgsDebugMsg( "The OGR Coordinate transformation FAILED TO INITIALISE!" );
        QgsDebugMsg( "------------------------------------------------------------" );
    }
#else
    if ( !mInitialisedFlag )
    {
        QgsDebugMsg( "Coordinate transformation failed to initialize!" );
    }
#endif

    //XXX todo overload == operator for QgsCoordinateReferenceSystem
    //at the moment srs.parameters contains the whole proj def...soon it wont...
    //if (mSourceCRS->toProj4() == mDestCRS->toProj4())
    if ( mSourceCRS == mDestCRS )
    {
        // If the source and destination projection are the same, set the short
        // circuit flag (no transform takes place)
        mShortCircuit = true;
        QgsDebugMsgLevel( "Source/Dest CRS equal, shortcircuit is set.", 3 );
    }
    else
    {
        // Transform must take place
        mShortCircuit = false;
        QgsDebugMsgLevel( "Source/Dest CRS UNequal, shortcircuit is NOt set.", 3 );
    }

}