예제 #1
0
QString QgsCoordinateUtils::formatCoordinateForProject( const QgsPoint& point, const QgsCoordinateReferenceSystem& destCrs, int precision )
{
  QString format = QgsProject::instance()->readEntry( "PositionPrecision", "/DegreeFormat", "MU" );

  QgsPoint geo = point;
  if ( format == "DM" || format == "DMS" || format == "D" )
  {
    // degrees
    if ( destCrs.isValid() && !destCrs.geographicFlag() )
    {
      // need to transform to geographic coordinates
      QgsCoordinateTransform ct( destCrs, QgsCoordinateReferenceSystem( GEOSRID ) );
      try
      {
        geo = ct.transform( point );
      }
      catch ( QgsCsException& )
      {
        return QString();
      }
    }

    if ( format == "DM" )
      return geo.toDegreesMinutes( precision, true, true );
    else if ( format == "DMS" )
      return geo.toDegreesMinutesSeconds( precision, true, true );
    else
      return geo.toString( precision );
  }
  else
  {
    // coordinates in map units
    return point.toString( precision );
  }
}
QString QgsCoordinateFormat::getDisplayString( const QgsPoint& p, const QgsCoordinateReferenceSystem& sSrs , Format format, const QString &epsg )
{
  QgsPoint pTrans = QgsCoordinateTransformCache::instance()->transform( sSrs.authid(), epsg )->transform( p );
  switch ( format )
  {
    case Default:
    {
      const QgsCoordinateReferenceSystem& crs = QgsCRSCache::instance()->crsByAuthId( epsg );
      int prec = crs.mapUnits() == QGis::Degrees ? 4 : 0;
      return QString( "%1, %2" ).arg( pTrans.x(), 0, 'f', prec ).arg( pTrans.y(), 0, 'f', prec );
    }
    case DegMinSec:
    {
      return pTrans.toDegreesMinutesSeconds( 1 );
    }
    case DegMin:
    {
      return pTrans.toDegreesMinutes( 3 );
    }
    case DecDeg:
    {
      return QString( "%1%2,%3%4" ).arg( pTrans.x(), 0, 'f', 5 ).arg( QChar( 176 ) )
             .arg( pTrans.y(), 0, 'f', 5 ).arg( QChar( 176 ) );
    }
    case UTM:
    {
      QgsLatLonToUTM::UTMCoo coo = QgsLatLonToUTM::LL2UTM( pTrans );
      return QString( "%1, %2 (zone %3%4)" ).arg( coo.easting ).arg( coo.northing ).arg( coo.zoneNumber ).arg( coo.zoneLetter );
    }
    case MGRS:
    {
      QgsLatLonToUTM::UTMCoo utm = QgsLatLonToUTM::LL2UTM( pTrans );
      QgsLatLonToUTM::MGRSCoo mgrs = QgsLatLonToUTM::UTM2MGRS( utm );
      if ( mgrs.letter100kID.isEmpty() )
        return QString();

      return QString( "%1%2%3 %4 %5" ).arg( mgrs.zoneNumber ).arg( mgrs.zoneLetter ).arg( mgrs.letter100kID ).arg( mgrs.easting, 5, 10, QChar( '0' ) ).arg( mgrs.northing, 5, 10, QChar( '0' ) );
    }
  }
  return "";
}
void QgsStatusBarCoordinatesWidget::showMouseCoordinates( const QgsPoint & p )
{
  if ( !mMapCanvas || mToggleExtentsViewButton->isChecked() )
  {
    return;
  }

  if ( mMapCanvas->mapUnits() == QGis::Degrees )
  {
    if ( !mMapCanvas->mapSettings().destinationCrs().isValid() )
      return;

    QgsPoint geo = p;
    if ( !mMapCanvas->mapSettings().destinationCrs().geographicFlag() )
    {
      QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), QgsCoordinateReferenceSystem( GEOSRID ) );
      geo = ct.transform( p );
    }
    QString format = QgsProject::instance()->readEntry( "PositionPrecision", "/DegreeFormat", "D" );

    if ( format == "DM" )
      mLineEdit->setText( geo.toDegreesMinutes( mMousePrecisionDecimalPlaces ) );
    else if ( format == "DMS" )
      mLineEdit->setText( geo.toDegreesMinutesSeconds( mMousePrecisionDecimalPlaces ) );
    else
      mLineEdit->setText( geo.toString( mMousePrecisionDecimalPlaces ) );
  }
  else
  {
    mLineEdit->setText( p.toString( mMousePrecisionDecimalPlaces ) );
  }

  if ( mLineEdit->width() > mLineEdit->minimumWidth() )
  {
    mLineEdit->setMinimumWidth( mLineEdit->width() );
  }
}