Skip to content

Commit e3ad5b1

Browse files
committedJan 17, 2017
[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.)
1 parent b119744 commit e3ad5b1

13 files changed

+55
-27
lines changed
 

‎python/core/composer/qgscomposermap.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class QgsComposerMap : QgsComposerItem
127127
//! @note not available in python bindings
128128
// QgsRectangle* currentMapExtent();
129129

130+
QgsCoordinateReferenceSystem crs() const;
131+
130132
PreviewMode previewMode() const;
131133
void setPreviewMode( PreviewMode m );
132134

‎src/app/composer/qgscomposermapgridwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ void QgsComposerMapGridWidget::on_mMapGridCRSButton_clicked()
10351035

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

10411041
if ( crsDialog.exec() == QDialog::Accepted )

‎src/core/composer/qgsatlascomposition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ void QgsAtlasComposition::computeExtent( QgsComposerMap* map )
439439
// QgsGeometry::boundingBox is expressed in the geometry"s native CRS
440440
// We have to transform the grometry to the destination CRS and ask for the bounding box
441441
// Note: we cannot directly take the transformation of the bounding box, since transformations are not linear
442-
mTransformedFeatureBounds = currentGeometry( map->composition()->mapSettings().destinationCrs() ).boundingBox();
442+
mTransformedFeatureBounds = currentGeometry( map->crs() ).boundingBox();
443443
}
444444

445445
void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
@@ -481,7 +481,7 @@ void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
481481
if ( map->atlasScalingMode() == QgsComposerMap::Fixed || map->atlasScalingMode() == QgsComposerMap::Predefined || isPointLayer )
482482
{
483483
QgsScaleCalculator calc;
484-
calc.setMapUnits( composition()->mapSettings().mapUnits() );
484+
calc.setMapUnits( map->crs().mapUnits() );
485485
calc.setDpi( 25.4 );
486486
double originalScale = calc.calculate( mOrigExtent, map->rect().width() );
487487
double geomCenterX = ( xa1 + xa2 ) / 2.0;

‎src/core/composer/qgscomposerattributetablev2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co
422422
if ( layer )
423423
{
424424
//transform back to layer CRS
425-
QgsCoordinateTransform coordTransform( layer->crs(), mComposition->mapSettings().destinationCrs() );
425+
QgsCoordinateTransform coordTransform( layer->crs(), mComposerMap->crs() );
426426
try
427427
{
428428
selectionRect = coordTransform.transformBoundingBox( selectionRect, QgsCoordinateTransform::ReverseTransform );

‎src/core/composer/qgscomposerhtml.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "qgsdistancearea.h"
2828
#include "qgsjsonutils.h"
2929
#include "qgsmapsettings.h"
30+
#include "qgscomposermap.h"
3031

3132
#include "qgswebpage.h"
3233
#include "qgswebframe.h"
@@ -542,7 +543,9 @@ void QgsComposerHtml::setExpressionContext( const QgsFeature &feature, QgsVector
542543
else if ( mComposition )
543544
{
544545
//set to composition's mapsettings' crs
545-
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
546+
QgsComposerMap* referenceMap = mComposition->referenceMap();
547+
if ( referenceMap )
548+
mDistanceArea->setSourceCrs( referenceMap->crs().srsid() );
546549
}
547550
if ( mComposition )
548551
{

‎src/core/composer/qgscomposerlabel.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "qgsfontutils.h"
2828
#include "qgsexpressioncontext.h"
2929
#include "qgsmapsettings.h"
30+
#include "qgscomposermap.h"
3031

3132
#include "qgswebview.h"
3233
#include "qgswebframe.h"
@@ -261,8 +262,10 @@ void QgsComposerLabel::refreshExpressionContext()
261262
}
262263
else
263264
{
264-
//set to composition's mapsettings' crs
265-
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
265+
//set to composition's reference map's crs
266+
QgsComposerMap* referenceMap = mComposition->referenceMap();
267+
if ( referenceMap )
268+
mDistanceArea->setSourceCrs( referenceMap->crs().srsid() );
266269
}
267270
mDistanceArea->setEllipsoidalMode( true );
268271
mDistanceArea->setEllipsoid( mComposition->project()->ellipsoid() );

‎src/core/composer/qgscomposerlegend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ void QgsComposerLegend::doUpdateFilterByMap()
704704
QgsGeometry filterPolygon;
705705
if ( mInAtlas )
706706
{
707-
filterPolygon = composition()->atlasComposition().currentGeometry( composition()->mapSettings().destinationCrs() );
707+
filterPolygon = composition()->atlasComposition().currentGeometry( mComposerMap->crs() );
708708
}
709709
mLegendModel->setLegendFilter( &ms, /* useExtent */ mInAtlas || mLegendFilterByMap, filterPolygon, /* useExpressions */ true );
710710
}

‎src/core/composer/qgscomposermap.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, QSizeF s
208208
jobMapSettings.setExtent( extent );
209209
jobMapSettings.setOutputSize( size.toSize() );
210210
jobMapSettings.setOutputDpi( dpi );
211-
jobMapSettings.setMapUnits( ms.mapUnits() );
211+
jobMapSettings.setMapUnits( crs().mapUnits() );
212212
jobMapSettings.setBackgroundColor( Qt::transparent );
213213
jobMapSettings.setOutputImageFormat( ms.outputImageFormat() );
214214
jobMapSettings.setRotation( mEvaluatedMapRotation );
@@ -233,7 +233,7 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, QSizeF s
233233
}
234234
jobMapSettings.setLayers( layers );
235235
jobMapSettings.setLayerStyleOverrides( layerStyleOverridesToRender( expressionContext ) );
236-
jobMapSettings.setDestinationCrs( ms.destinationCrs() );
236+
jobMapSettings.setDestinationCrs( crs() );
237237
jobMapSettings.setCrsTransformEnabled( ms.hasCrsTransformEnabled() );
238238
jobMapSettings.setFlags( ms.flags() );
239239
jobMapSettings.setFlag( QgsMapSettings::DrawSelection, false );
@@ -625,7 +625,7 @@ QMap<QString, QString> QgsComposerMap::layerStyleOverridesToRender( const QgsExp
625625
double QgsComposerMap::scale() const
626626
{
627627
QgsScaleCalculator calculator;
628-
calculator.setMapUnits( mComposition->mapSettings().mapUnits() );
628+
calculator.setMapUnits( crs().mapUnits() );
629629
calculator.setDpi( 25.4 ); //QGraphicsView units are mm
630630
return calculator.calculate( *currentMapExtent(), rect().width() );
631631
}
@@ -717,7 +717,7 @@ void QgsComposerMap::zoomContent( const double factor, const QPointF point, cons
717717
//and also apply to the map's original extent (see #9602)
718718
//we can't use the scaleRatio calculated earlier, as the scale can vary depending on extent for geographic coordinate systems
719719
QgsScaleCalculator calculator;
720-
calculator.setMapUnits( mComposition->mapSettings().mapUnits() );
720+
calculator.setMapUnits( crs().mapUnits() );
721721
calculator.setDpi( 25.4 ); //QGraphicsView units are mm
722722
double scaleRatio = scale() / calculator.calculate( mExtent, rect().width() );
723723
mExtent.scale( scaleRatio );
@@ -871,6 +871,18 @@ QgsRectangle* QgsComposerMap::currentMapExtent()
871871
}
872872
}
873873

874+
QgsCoordinateReferenceSystem QgsComposerMap::crs() const
875+
{
876+
if ( mComposition )
877+
{
878+
return mComposition->mapSettings().destinationCrs();
879+
}
880+
else
881+
{
882+
return QgsCoordinateReferenceSystem();
883+
}
884+
}
885+
874886
const QgsRectangle* QgsComposerMap::currentMapExtent() const
875887
{
876888
//const version
@@ -905,7 +917,7 @@ void QgsComposerMap::setNewScale( double scaleDenominator, bool forceUpdate )
905917
//and also apply to the map's original extent (see #9602)
906918
//we can't use the scaleRatio calculated earlier, as the scale can vary depending on extent for geographic coordinate systems
907919
QgsScaleCalculator calculator;
908-
calculator.setMapUnits( mComposition->mapSettings().mapUnits() );
920+
calculator.setMapUnits( crs().mapUnits() );
909921
calculator.setDpi( 25.4 ); //QGraphicsView units are mm
910922
scaleRatio = scaleDenominator / calculator.calculate( mExtent, rect().width() );
911923
mExtent.scale( scaleRatio );
@@ -1826,9 +1838,10 @@ QgsExpressionContext QgsComposerMap::createExpressionContext() const
18261838

18271839
if ( mComposition )
18281840
{
1829-
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mComposition->mapSettings().destinationCrs().authid(), true ) );
1830-
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mComposition->mapSettings().destinationCrs().toProj4(), true ) );
1831-
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mComposition->mapSettings().mapUnits() ), true ) );
1841+
QgsCoordinateReferenceSystem mapCrs = crs();
1842+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mapCrs.authid(), true ) );
1843+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapCrs.toProj4(), true ) );
1844+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapCrs.mapUnits() ), true ) );
18321845
}
18331846

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

19801993
mapX = annotation->mapPosition().x();
19811994
mapY = annotation->mapPosition().y();
1982-
QgsCoordinateReferenceSystem crs = annotation->mapPositionCrs();
1995+
QgsCoordinateReferenceSystem annotationCrs = annotation->mapPositionCrs();
19831996

1984-
if ( crs != mComposition->mapSettings().destinationCrs() )
1997+
if ( annotationCrs != crs() )
19851998
{
19861999
//need to reproject
1987-
QgsCoordinateTransform t( crs, mComposition->mapSettings().destinationCrs() );
2000+
QgsCoordinateTransform t( annotationCrs, crs() );
19882001
double z = 0.0;
19892002
t.transformInPlace( mapX, mapY, z );
19902003
}

‎src/core/composer/qgscomposermap.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgis_core.h"
2222
#include "qgscomposeritem.h"
2323
#include "qgsrectangle.h"
24+
#include "qgscoordinatereferencesystem.h"
2425
#include <QFont>
2526
#include <QGraphicsRectItem>
2627

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

170+
/**
171+
* Returns the map's coordinate reference system.
172+
* @note added in QGIS 3.0
173+
*/
174+
QgsCoordinateReferenceSystem crs() const;
175+
169176
PreviewMode previewMode() const {return mPreviewMode;}
170177
void setPreviewMode( PreviewMode m );
171178

‎src/core/composer/qgscomposermapgrid.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ void QgsComposerMapGrid::draw( QPainter* p )
651651
QList< QPair< double, QLineF > > horizontalLines;
652652

653653
//is grid in a different crs than map?
654-
if ( mGridUnit == MapUnit && mCRS.isValid() && mCRS != ms.destinationCrs() )
654+
if ( mGridUnit == MapUnit && mCRS.isValid() && mCRS != mComposerMap->crs() )
655655
{
656656
drawGridCrsTransform( context, dotsPerMM, horizontalLines, verticalLines );
657657
}
@@ -1416,7 +1416,7 @@ QString QgsComposerMapGrid::gridAnnotationString( double value, QgsComposerMapGr
14161416
}
14171417
else if ( mComposerMap && mComposerMap->composition() )
14181418
{
1419-
geographic = mComposerMap->composition()->mapSettings().destinationCrs().isGeographic();
1419+
geographic = mComposerMap->crs().isGeographic();
14201420
}
14211421

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

23412341
try
23422342
{
2343-
QgsCoordinateTransform tr( mComposerMap->composition()->mapSettings().destinationCrs(), mCRS );
2343+
QgsCoordinateTransform tr( mComposerMap->crs(), mCRS );
23442344
QPolygonF mapPolygon = mComposerMap->transformedMapPolygon();
23452345
QRectF mbr = mapPolygon.boundingRect();
23462346
QgsRectangle mapBoundingRect( mbr.left(), mbr.bottom(), mbr.right(), mbr.top() );
@@ -2372,7 +2372,7 @@ int QgsComposerMapGrid::crsGridParams( QgsRectangle& crsRect, QgsCoordinateTrans
23722372
}
23732373

23742374
inverseTransform.setSourceCrs( mCRS );
2375-
inverseTransform.setDestinationCrs( mComposerMap->composition()->mapSettings().destinationCrs() );
2375+
inverseTransform.setDestinationCrs( mComposerMap->crs() );
23762376
}
23772377
catch ( QgsCsException & cse )
23782378
{

‎src/core/composer/qgscomposerpicture.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ void QgsComposerPicture::updateMapRotation()
426426
case TrueNorth:
427427
{
428428
QgsPoint center = mRotationMap->currentMapExtent()->center();
429-
QgsCoordinateReferenceSystem crs = mComposition->mapSettings().destinationCrs();
429+
QgsCoordinateReferenceSystem crs = mRotationMap->crs();
430430

431431
try
432432
{

‎src/core/composer/qgscomposerscalebar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ double QgsComposerScaleBar::mapWidth() const
303303
{
304304
QgsDistanceArea da;
305305
da.setEllipsoidalMode( true );
306-
da.setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
306+
da.setSourceCrs( mComposerMap->crs().srsid() );
307307
da.setEllipsoid( mComposition->project()->ellipsoid() );
308308

309309
QgsUnitTypes::DistanceUnit units = QgsUnitTypes::DistanceMeters;

‎src/core/composer/qgscomposition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2850,7 +2850,7 @@ void QgsComposition::georeferenceOutput( const QString& file, QgsComposerMap* ma
28502850
//TODO - metadata can be set here, e.g.:
28512851
GDALSetMetadataItem( outputDS, "AUTHOR", "me", nullptr );
28522852
#endif
2853-
GDALSetProjection( outputDS, mMapSettings.destinationCrs().toWkt().toLocal8Bit().constData() );
2853+
GDALSetProjection( outputDS, map->crs().toWkt().toLocal8Bit().constData() );
28542854
GDALClose( outputDS );
28552855
}
28562856
CPLSetConfigOption( "GDAL_PDF_DPI", nullptr );

0 commit comments

Comments
 (0)
Please sign in to comment.