Skip to content

Commit 92e3d64

Browse files
committedOct 22, 2014
[composer] Catch CRS exceptions when drawing grid lines
Prevents bad grid parameters for a transformed grid flooding the user with hundreds of unclosable error message boxes.
1 parent 528c4ca commit 92e3d64

File tree

1 file changed

+52
-27
lines changed

1 file changed

+52
-27
lines changed
 

‎src/core/composer/qgscomposermapgrid.cpp

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgssymbollayerv2utils.h"
2727
#include "qgssymbolv2.h"
2828
#include "qgscoordinatereferencesystem.h"
29+
#include "qgslogger.h"
2930

3031
#include <QPainter>
3132
#include <QPen>
@@ -1525,8 +1526,16 @@ int QgsComposerMapGrid::xGridLinesCRSTransform( const QgsRectangle& bbox, const
15251526
cont = false;
15261527
}
15271528

1528-
QgsPoint mapPoint = t.transform( currentX, currentLevel ); //transform back to map crs
1529-
gridLine.append( mComposerMap->mapToItemCoords( QPointF( mapPoint.x(), mapPoint.y() ) ) ); //transform back to composer coords
1529+
try
1530+
{
1531+
QgsPoint mapPoint = t.transform( currentX, currentLevel ); //transform back to map crs
1532+
gridLine.append( mComposerMap->mapToItemCoords( QPointF( mapPoint.x(), mapPoint.y() ) ) ); //transform back to composer coords
1533+
}
1534+
catch ( QgsCsException & cse )
1535+
{
1536+
QgsDebugMsg( QString( "Caught CRS exception %1" ).arg( cse.what() ) );
1537+
}
1538+
15301539
currentX += step;
15311540
if ( crosses180 && currentX > 180.0 )
15321541
{
@@ -1583,10 +1592,18 @@ int QgsComposerMapGrid::yGridLinesCRSTransform( const QgsRectangle& bbox, const
15831592
{
15841593
cont = false;
15851594
}
1586-
//transform back to map crs
1587-
QgsPoint mapPoint = t.transform( currentLevel, currentY );
1588-
//transform back to composer coords
1589-
gridLine.append( mComposerMap->mapToItemCoords( QPointF( mapPoint.x(), mapPoint.y() ) ) );
1595+
try
1596+
{
1597+
//transform back to map crs
1598+
QgsPoint mapPoint = t.transform( currentLevel, currentY );
1599+
//transform back to composer coords
1600+
gridLine.append( mComposerMap->mapToItemCoords( QPointF( mapPoint.x(), mapPoint.y() ) ) );
1601+
}
1602+
catch ( QgsCsException & cse )
1603+
{
1604+
QgsDebugMsg( QString( "Caught CRS exception %1" ).arg( cse.what() ) );
1605+
}
1606+
15901607
currentY += step;
15911608
}
15921609
//clip grid line to map polygon
@@ -2018,39 +2035,47 @@ int QgsComposerMapGrid::crsGridParams( QgsRectangle& crsRect, QgsCoordinateTrans
20182035
return 1;
20192036
}
20202037

2021-
QgsCoordinateTransform tr( mComposerMap->composition()->mapSettings().destinationCrs(), mCRS );
2022-
QPolygonF mapPolygon = mComposerMap->transformedMapPolygon();
2023-
QRectF mbr = mapPolygon.boundingRect();
2024-
QgsRectangle mapBoundingRect( mbr.left(), mbr.bottom(), mbr.right(), mbr.top() );
2025-
2026-
2027-
if ( mCRS.geographicFlag() )
2038+
try
20282039
{
2029-
//handle crossing the 180 degree longitude line
2030-
QgsPoint lowerLeft( mapBoundingRect.xMinimum(), mapBoundingRect.yMinimum() );
2031-
QgsPoint upperRight( mapBoundingRect.xMaximum(), mapBoundingRect.yMaximum() );
2040+
QgsCoordinateTransform tr( mComposerMap->composition()->mapSettings().destinationCrs(), mCRS );
2041+
QPolygonF mapPolygon = mComposerMap->transformedMapPolygon();
2042+
QRectF mbr = mapPolygon.boundingRect();
2043+
QgsRectangle mapBoundingRect( mbr.left(), mbr.bottom(), mbr.right(), mbr.top() );
20322044

2033-
lowerLeft = tr.transform( lowerLeft.x(), lowerLeft.y() );
2034-
upperRight = tr.transform( upperRight.x(), upperRight.y() );
20352045

2036-
if ( lowerLeft.x() > upperRight.x() )
2046+
if ( mCRS.geographicFlag() )
20372047
{
2038-
//we've crossed the line
2039-
crsRect = tr.transformBoundingBox( mapBoundingRect, QgsCoordinateTransform::ForwardTransform, true );
2048+
//handle crossing the 180 degree longitude line
2049+
QgsPoint lowerLeft( mapBoundingRect.xMinimum(), mapBoundingRect.yMinimum() );
2050+
QgsPoint upperRight( mapBoundingRect.xMaximum(), mapBoundingRect.yMaximum() );
2051+
2052+
lowerLeft = tr.transform( lowerLeft.x(), lowerLeft.y() );
2053+
upperRight = tr.transform( upperRight.x(), upperRight.y() );
2054+
2055+
if ( lowerLeft.x() > upperRight.x() )
2056+
{
2057+
//we've crossed the line
2058+
crsRect = tr.transformBoundingBox( mapBoundingRect, QgsCoordinateTransform::ForwardTransform, true );
2059+
}
2060+
else
2061+
{
2062+
//didn't cross the line
2063+
crsRect = tr.transformBoundingBox( mapBoundingRect );
2064+
}
20402065
}
20412066
else
20422067
{
2043-
//didn't cross the line
20442068
crsRect = tr.transformBoundingBox( mapBoundingRect );
20452069
}
2070+
2071+
inverseTransform.setSourceCrs( mCRS );
2072+
inverseTransform.setDestCRS( mComposerMap->composition()->mapSettings().destinationCrs() );
20462073
}
2047-
else
2074+
catch ( QgsCsException & cse )
20482075
{
2049-
crsRect = tr.transformBoundingBox( mapBoundingRect );
2076+
QgsDebugMsg( QString( "Caught CRS exception %1" ).arg( cse.what() ) );
2077+
return 1;
20502078
}
2051-
2052-
inverseTransform.setSourceCrs( mCRS );
2053-
inverseTransform.setDestCRS( mComposerMap->composition()->mapSettings().destinationCrs() );
20542079
return 0;
20552080
}
20562081

0 commit comments

Comments
 (0)
Please sign in to comment.