Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[composer] Remove more use of composition map settings
Use relevant composer map item CRS instead of composition mapsetting's
CRS. For items directly linked to a map (eg scalebar, legend) we
use that map's CRS, for others (eg labels, html items) we use the
composition's reference map CRS.

(Note that the CRS cannot be set for a map, and is still currently
linked to the canvas CRS. Fixing that is a future TODO.)
  • Loading branch information
nyalldawson committed Jan 17, 2017
1 parent b119744 commit e3ad5b1
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 27 deletions.
2 changes: 2 additions & 0 deletions python/core/composer/qgscomposermap.sip
Expand Up @@ -127,6 +127,8 @@ class QgsComposerMap : QgsComposerItem
//! @note not available in python bindings
// QgsRectangle* currentMapExtent();

QgsCoordinateReferenceSystem crs() const;

PreviewMode previewMode() const;
void setPreviewMode( PreviewMode m );

Expand Down
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposermapgridwidget.cpp
Expand Up @@ -1035,7 +1035,7 @@ void QgsComposerMapGridWidget::on_mMapGridCRSButton_clicked()

QgsGenericProjectionSelector crsDialog( this );
QgsCoordinateReferenceSystem crs = mComposerMapGrid->crs();
QString currentAuthId = crs.isValid() ? crs.authid() : mComposerMap->composition()->mapSettings().destinationCrs().authid();
QString currentAuthId = crs.isValid() ? crs.authid() : mComposerMap->crs().authid();
crsDialog.setSelectedAuthId( currentAuthId );

if ( crsDialog.exec() == QDialog::Accepted )
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgsatlascomposition.cpp
Expand Up @@ -439,7 +439,7 @@ void QgsAtlasComposition::computeExtent( QgsComposerMap* map )
// QgsGeometry::boundingBox is expressed in the geometry"s native CRS
// We have to transform the grometry to the destination CRS and ask for the bounding box
// Note: we cannot directly take the transformation of the bounding box, since transformations are not linear
mTransformedFeatureBounds = currentGeometry( map->composition()->mapSettings().destinationCrs() ).boundingBox();
mTransformedFeatureBounds = currentGeometry( map->crs() ).boundingBox();
}

void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
Expand Down Expand Up @@ -481,7 +481,7 @@ void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
if ( map->atlasScalingMode() == QgsComposerMap::Fixed || map->atlasScalingMode() == QgsComposerMap::Predefined || isPointLayer )
{
QgsScaleCalculator calc;
calc.setMapUnits( composition()->mapSettings().mapUnits() );
calc.setMapUnits( map->crs().mapUnits() );
calc.setDpi( 25.4 );
double originalScale = calc.calculate( mOrigExtent, map->rect().width() );
double geomCenterX = ( xa1 + xa2 ) / 2.0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerattributetablev2.cpp
Expand Up @@ -422,7 +422,7 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co
if ( layer )
{
//transform back to layer CRS
QgsCoordinateTransform coordTransform( layer->crs(), mComposition->mapSettings().destinationCrs() );
QgsCoordinateTransform coordTransform( layer->crs(), mComposerMap->crs() );
try
{
selectionRect = coordTransform.transformBoundingBox( selectionRect, QgsCoordinateTransform::ReverseTransform );
Expand Down
5 changes: 4 additions & 1 deletion src/core/composer/qgscomposerhtml.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsdistancearea.h"
#include "qgsjsonutils.h"
#include "qgsmapsettings.h"
#include "qgscomposermap.h"

#include "qgswebpage.h"
#include "qgswebframe.h"
Expand Down Expand Up @@ -542,7 +543,9 @@ void QgsComposerHtml::setExpressionContext( const QgsFeature &feature, QgsVector
else if ( mComposition )
{
//set to composition's mapsettings' crs
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
QgsComposerMap* referenceMap = mComposition->referenceMap();
if ( referenceMap )
mDistanceArea->setSourceCrs( referenceMap->crs().srsid() );
}
if ( mComposition )
{
Expand Down
7 changes: 5 additions & 2 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsfontutils.h"
#include "qgsexpressioncontext.h"
#include "qgsmapsettings.h"
#include "qgscomposermap.h"

#include "qgswebview.h"
#include "qgswebframe.h"
Expand Down Expand Up @@ -261,8 +262,10 @@ void QgsComposerLabel::refreshExpressionContext()
}
else
{
//set to composition's mapsettings' crs
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
//set to composition's reference map's crs
QgsComposerMap* referenceMap = mComposition->referenceMap();
if ( referenceMap )
mDistanceArea->setSourceCrs( referenceMap->crs().srsid() );
}
mDistanceArea->setEllipsoidalMode( true );
mDistanceArea->setEllipsoid( mComposition->project()->ellipsoid() );
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerlegend.cpp
Expand Up @@ -704,7 +704,7 @@ void QgsComposerLegend::doUpdateFilterByMap()
QgsGeometry filterPolygon;
if ( mInAtlas )
{
filterPolygon = composition()->atlasComposition().currentGeometry( composition()->mapSettings().destinationCrs() );
filterPolygon = composition()->atlasComposition().currentGeometry( mComposerMap->crs() );
}
mLegendModel->setLegendFilter( &ms, /* useExtent */ mInAtlas || mLegendFilterByMap, filterPolygon, /* useExpressions */ true );
}
Expand Down
35 changes: 24 additions & 11 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -208,7 +208,7 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, QSizeF s
jobMapSettings.setExtent( extent );
jobMapSettings.setOutputSize( size.toSize() );
jobMapSettings.setOutputDpi( dpi );
jobMapSettings.setMapUnits( ms.mapUnits() );
jobMapSettings.setMapUnits( crs().mapUnits() );
jobMapSettings.setBackgroundColor( Qt::transparent );
jobMapSettings.setOutputImageFormat( ms.outputImageFormat() );
jobMapSettings.setRotation( mEvaluatedMapRotation );
Expand All @@ -233,7 +233,7 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, QSizeF s
}
jobMapSettings.setLayers( layers );
jobMapSettings.setLayerStyleOverrides( layerStyleOverridesToRender( expressionContext ) );
jobMapSettings.setDestinationCrs( ms.destinationCrs() );
jobMapSettings.setDestinationCrs( crs() );
jobMapSettings.setCrsTransformEnabled( ms.hasCrsTransformEnabled() );
jobMapSettings.setFlags( ms.flags() );
jobMapSettings.setFlag( QgsMapSettings::DrawSelection, false );
Expand Down Expand Up @@ -625,7 +625,7 @@ QMap<QString, QString> QgsComposerMap::layerStyleOverridesToRender( const QgsExp
double QgsComposerMap::scale() const
{
QgsScaleCalculator calculator;
calculator.setMapUnits( mComposition->mapSettings().mapUnits() );
calculator.setMapUnits( crs().mapUnits() );
calculator.setDpi( 25.4 ); //QGraphicsView units are mm
return calculator.calculate( *currentMapExtent(), rect().width() );
}
Expand Down Expand Up @@ -717,7 +717,7 @@ void QgsComposerMap::zoomContent( const double factor, const QPointF point, cons
//and also apply to the map's original extent (see #9602)
//we can't use the scaleRatio calculated earlier, as the scale can vary depending on extent for geographic coordinate systems
QgsScaleCalculator calculator;
calculator.setMapUnits( mComposition->mapSettings().mapUnits() );
calculator.setMapUnits( crs().mapUnits() );
calculator.setDpi( 25.4 ); //QGraphicsView units are mm
double scaleRatio = scale() / calculator.calculate( mExtent, rect().width() );
mExtent.scale( scaleRatio );
Expand Down Expand Up @@ -871,6 +871,18 @@ QgsRectangle* QgsComposerMap::currentMapExtent()
}
}

QgsCoordinateReferenceSystem QgsComposerMap::crs() const
{
if ( mComposition )
{
return mComposition->mapSettings().destinationCrs();
}
else
{
return QgsCoordinateReferenceSystem();
}
}

const QgsRectangle* QgsComposerMap::currentMapExtent() const
{
//const version
Expand Down Expand Up @@ -905,7 +917,7 @@ void QgsComposerMap::setNewScale( double scaleDenominator, bool forceUpdate )
//and also apply to the map's original extent (see #9602)
//we can't use the scaleRatio calculated earlier, as the scale can vary depending on extent for geographic coordinate systems
QgsScaleCalculator calculator;
calculator.setMapUnits( mComposition->mapSettings().mapUnits() );
calculator.setMapUnits( crs().mapUnits() );
calculator.setDpi( 25.4 ); //QGraphicsView units are mm
scaleRatio = scaleDenominator / calculator.calculate( mExtent, rect().width() );
mExtent.scale( scaleRatio );
Expand Down Expand Up @@ -1826,9 +1838,10 @@ QgsExpressionContext QgsComposerMap::createExpressionContext() const

if ( mComposition )
{
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mComposition->mapSettings().destinationCrs().authid(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mComposition->mapSettings().destinationCrs().toProj4(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mComposition->mapSettings().mapUnits() ), true ) );
QgsCoordinateReferenceSystem mapCrs = crs();
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mapCrs.authid(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapCrs.toProj4(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapCrs.mapUnits() ), true ) );
}

context.appendScope( scope );
Expand Down Expand Up @@ -1979,12 +1992,12 @@ QPointF QgsComposerMap::composerMapPosForItem( const QgsAnnotation* annotation )

mapX = annotation->mapPosition().x();
mapY = annotation->mapPosition().y();
QgsCoordinateReferenceSystem crs = annotation->mapPositionCrs();
QgsCoordinateReferenceSystem annotationCrs = annotation->mapPositionCrs();

if ( crs != mComposition->mapSettings().destinationCrs() )
if ( annotationCrs != crs() )
{
//need to reproject
QgsCoordinateTransform t( crs, mComposition->mapSettings().destinationCrs() );
QgsCoordinateTransform t( annotationCrs, crs() );
double z = 0.0;
t.transformInPlace( mapX, mapY, z );
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/composer/qgscomposermap.h
Expand Up @@ -21,6 +21,7 @@
#include "qgis_core.h"
#include "qgscomposeritem.h"
#include "qgsrectangle.h"
#include "qgscoordinatereferencesystem.h"
#include <QFont>
#include <QGraphicsRectItem>

Expand Down Expand Up @@ -166,6 +167,12 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
//! @note not available in python bindings
QgsRectangle* currentMapExtent();

/**
* Returns the map's coordinate reference system.
* @note added in QGIS 3.0
*/
QgsCoordinateReferenceSystem crs() const;

PreviewMode previewMode() const {return mPreviewMode;}
void setPreviewMode( PreviewMode m );

Expand Down
10 changes: 5 additions & 5 deletions src/core/composer/qgscomposermapgrid.cpp
Expand Up @@ -651,7 +651,7 @@ void QgsComposerMapGrid::draw( QPainter* p )
QList< QPair< double, QLineF > > horizontalLines;

//is grid in a different crs than map?
if ( mGridUnit == MapUnit && mCRS.isValid() && mCRS != ms.destinationCrs() )
if ( mGridUnit == MapUnit && mCRS.isValid() && mCRS != mComposerMap->crs() )
{
drawGridCrsTransform( context, dotsPerMM, horizontalLines, verticalLines );
}
Expand Down Expand Up @@ -1416,7 +1416,7 @@ QString QgsComposerMapGrid::gridAnnotationString( double value, QgsComposerMapGr
}
else if ( mComposerMap && mComposerMap->composition() )
{
geographic = mComposerMap->composition()->mapSettings().destinationCrs().isGeographic();
geographic = mComposerMap->crs().isGeographic();
}

if ( geographic && coord == QgsComposerMapGrid::Longitude &&
Expand Down Expand Up @@ -2072,7 +2072,7 @@ void QgsComposerMapGrid::calculateMaxExtension( double& top, double& right, doub
//collect grid lines
QList< QPair< double, QLineF > > verticalLines;
QList< QPair< double, QLineF > > horizontalLines;
if ( mGridUnit == MapUnit && mCRS.isValid() && mCRS != ms.destinationCrs() )
if ( mGridUnit == MapUnit && mCRS.isValid() && mCRS != mComposerMap->crs() )
{
drawGridCrsTransform( context, 0, horizontalLines, verticalLines, false );
}
Expand Down Expand Up @@ -2340,7 +2340,7 @@ int QgsComposerMapGrid::crsGridParams( QgsRectangle& crsRect, QgsCoordinateTrans

try
{
QgsCoordinateTransform tr( mComposerMap->composition()->mapSettings().destinationCrs(), mCRS );
QgsCoordinateTransform tr( mComposerMap->crs(), mCRS );
QPolygonF mapPolygon = mComposerMap->transformedMapPolygon();
QRectF mbr = mapPolygon.boundingRect();
QgsRectangle mapBoundingRect( mbr.left(), mbr.bottom(), mbr.right(), mbr.top() );
Expand Down Expand Up @@ -2372,7 +2372,7 @@ int QgsComposerMapGrid::crsGridParams( QgsRectangle& crsRect, QgsCoordinateTrans
}

inverseTransform.setSourceCrs( mCRS );
inverseTransform.setDestinationCrs( mComposerMap->composition()->mapSettings().destinationCrs() );
inverseTransform.setDestinationCrs( mComposerMap->crs() );
}
catch ( QgsCsException & cse )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerpicture.cpp
Expand Up @@ -426,7 +426,7 @@ void QgsComposerPicture::updateMapRotation()
case TrueNorth:
{
QgsPoint center = mRotationMap->currentMapExtent()->center();
QgsCoordinateReferenceSystem crs = mComposition->mapSettings().destinationCrs();
QgsCoordinateReferenceSystem crs = mRotationMap->crs();

try
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerscalebar.cpp
Expand Up @@ -303,7 +303,7 @@ double QgsComposerScaleBar::mapWidth() const
{
QgsDistanceArea da;
da.setEllipsoidalMode( true );
da.setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
da.setSourceCrs( mComposerMap->crs().srsid() );
da.setEllipsoid( mComposition->project()->ellipsoid() );

QgsUnitTypes::DistanceUnit units = QgsUnitTypes::DistanceMeters;
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposition.cpp
Expand Up @@ -2850,7 +2850,7 @@ void QgsComposition::georeferenceOutput( const QString& file, QgsComposerMap* ma
//TODO - metadata can be set here, e.g.:
GDALSetMetadataItem( outputDS, "AUTHOR", "me", nullptr );
#endif
GDALSetProjection( outputDS, mMapSettings.destinationCrs().toWkt().toLocal8Bit().constData() );
GDALSetProjection( outputDS, map->crs().toWkt().toLocal8Bit().constData() );
GDALClose( outputDS );
}
CPLSetConfigOption( "GDAL_PDF_DPI", nullptr );
Expand Down

0 comments on commit e3ad5b1

Please sign in to comment.