Skip to content

Commit 66d04ac

Browse files
committed
Add more tolerance when determining whether to show grid annotations
close to map edges
1 parent 02b67e4 commit 66d04ac

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

src/core/layout/qgslayoutitemmapgrid.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454

5555
using namespace Qt::StringLiterals;
5656

57+
#define MAX_GRID_LINES 1000 //maximum number of horizontal or vertical grid lines to draw
58+
5759
QgsLayoutItemMapGridStack::QgsLayoutItemMapGridStack( QgsLayoutItemMap *map )
5860
: QgsLayoutItemMapItemStack( map )
5961
{}
@@ -1591,17 +1593,22 @@ void QgsLayoutItemMapGrid::drawCoordinateAnnotation(
15911593
facingLeft = !facingLeft;
15921594
facingRight = !facingRight;
15931595
}
1596+
const QRectF mapRect = mMap->rect();
15941597
if ( annot.border == Qgis::MapGridBorderSide::Top
1595-
&& ( ( facingLeft && annot.position.x() < mRotatedAnnotationsMarginToCorner ) || ( facingRight && annot.position.x() > mMap->rect().width() - mRotatedAnnotationsMarginToCorner ) ) )
1598+
&& ( ( facingLeft && !qgsDoubleGreaterThanOrNear( annot.position.x(),mRotatedAnnotationsMarginToCorner,ANNOTATION_CLOSE_TO_EDGE_TOLERANCE_MM) )
1599+
|| ( facingRight && !qgsDoubleLessThanOrNear( annot.position.x(), mapRect.width() - mRotatedAnnotationsMarginToCorner, ANNOTATION_CLOSE_TO_EDGE_TOLERANCE_MM ) ) ) )
15961600
return;
15971601
if ( annot.border == Qgis::MapGridBorderSide::Bottom
1598-
&& ( ( facingLeft && annot.position.x() > mMap->rect().width() - mRotatedAnnotationsMarginToCorner ) || ( facingRight && annot.position.x() < mRotatedAnnotationsMarginToCorner ) ) )
1602+
&& ( ( facingLeft && !qgsDoubleLessThanOrNear( annot.position.x(), mapRect.width() - mRotatedAnnotationsMarginToCorner, ANNOTATION_CLOSE_TO_EDGE_TOLERANCE_MM ) )
1603+
|| ( facingRight && !qgsDoubleGreaterThanOrNear( annot.position.x(), mRotatedAnnotationsMarginToCorner, ANNOTATION_CLOSE_TO_EDGE_TOLERANCE_MM ) ) ) )
15991604
return;
16001605
if ( annot.border == Qgis::MapGridBorderSide::Left
1601-
&& ( ( facingLeft && annot.position.y() > mMap->rect().height() - mRotatedAnnotationsMarginToCorner ) || ( facingRight && annot.position.y() < mRotatedAnnotationsMarginToCorner ) ) )
1606+
&& ( ( facingLeft && !qgsDoubleLessThanOrNear( annot.position.y(), mapRect.height() - mRotatedAnnotationsMarginToCorner, ANNOTATION_CLOSE_TO_EDGE_TOLERANCE_MM ) )
1607+
|| ( facingRight && !qgsDoubleGreaterThanOrNear( annot.position.y(), mRotatedAnnotationsMarginToCorner, ANNOTATION_CLOSE_TO_EDGE_TOLERANCE_MM ) ) ) )
16021608
return;
16031609
if ( annot.border == Qgis::MapGridBorderSide::Right
1604-
&& ( ( facingLeft && annot.position.y() < mRotatedAnnotationsMarginToCorner ) || ( facingRight && annot.position.y() > mMap->rect().height() - mRotatedAnnotationsMarginToCorner ) ) )
1610+
&& ( ( facingLeft && !qgsDoubleGreaterThanOrNear( annot.position.y(), mRotatedAnnotationsMarginToCorner, ANNOTATION_CLOSE_TO_EDGE_TOLERANCE_MM ) )
1611+
|| ( facingRight && !qgsDoubleLessThanOrNear( annot.position.y(), mapRect.height() - mRotatedAnnotationsMarginToCorner, ANNOTATION_CLOSE_TO_EDGE_TOLERANCE_MM ) ) ) )
16051612
return;
16061613

16071614
// adjust to account for text alignment -- for left/right borders the alignment
@@ -1882,14 +1889,16 @@ void QgsLayoutItemMapGrid::calculateXGridLines() const
18821889
}
18831890

18841891
double currentLevel = static_cast< int >( ( mapBoundingRect.top() - gridOffsetY ) / gridIntervalY ) * gridIntervalY + gridOffsetY;
1892+
if ( !qgsDoubleGreaterThanOrNear( currentLevel, mapBoundingRect.top(), GRID_LINE_CLOSE_TO_EDGE_TOLERANCE_MAP_UNITS ) )
1893+
currentLevel += gridIntervalY;
18851894

18861895
int gridLineCount = 0;
18871896
if ( qgsDoubleNear( mMap->mapRotation(), 0.0 ) || ( mGridUnit != Qgis::MapGridUnit::MapUnits && mGridUnit != Qgis::MapGridUnit::DynamicPageSizeBased ) )
18881897
{
18891898
//no rotation. Do it 'the easy way'
18901899

18911900
double yCanvasCoord;
1892-
while ( currentLevel <= mapBoundingRect.bottom() && gridLineCount < MAX_GRID_OBJECTS )
1901+
while ( qgsDoubleLessThanOrNear( currentLevel, mapBoundingRect.bottom(), GRID_LINE_CLOSE_TO_EDGE_TOLERANCE_MAP_UNITS ) && gridLineCount < MAX_GRID_OBJECTS )
18931902
{
18941903
yCanvasCoord = mMap->rect().height() * ( 1 - ( currentLevel - mapBoundingRect.top() ) / mapBoundingRect.height() );
18951904
GridLine newLine;
@@ -1978,13 +1987,15 @@ void QgsLayoutItemMapGrid::calculateYGridLines() const
19781987
}
19791988

19801989
double currentLevel = static_cast< int >( ( mapBoundingRect.left() - gridOffsetX ) / gridIntervalX ) * gridIntervalX + gridOffsetX;
1990+
if ( !qgsDoubleGreaterThanOrNear( currentLevel, mapBoundingRect.left(), GRID_LINE_CLOSE_TO_EDGE_TOLERANCE_MAP_UNITS ) )
1991+
currentLevel += gridIntervalX;
19811992

19821993
int gridLineCount = 0;
19831994
if ( qgsDoubleNear( mMap->mapRotation(), 0.0 ) || ( mGridUnit != Qgis::MapGridUnit::MapUnits && mGridUnit != Qgis::MapGridUnit::DynamicPageSizeBased ) )
19841995
{
19851996
//no rotation. Do it 'the easy way'
19861997
double xCanvasCoord;
1987-
while ( currentLevel <= mapBoundingRect.right() && gridLineCount < MAX_GRID_OBJECTS )
1998+
while ( qgsDoubleLessThanOrNear( currentLevel, mapBoundingRect.right(), GRID_LINE_CLOSE_TO_EDGE_TOLERANCE_MAP_UNITS ) && gridLineCount < MAX_GRID_OBJECTS )
19881999
{
19892000
xCanvasCoord = mMap->rect().width() * ( currentLevel - mapBoundingRect.left() ) / mapBoundingRect.width();
19902001

src/core/layout/qgslayoutitemmapgrid.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,12 @@ class CORE_EXPORT QgsLayoutItemMapGrid : public QgsLayoutItemMapItem
958958
}
959959
};
960960

961+
static constexpr double GRID_LINE_CLOSE_TO_EDGE_TOLERANCE_MAP_UNITS = 0.00001;
962+
963+
// we need a little bit of tolerance for showing annotations right at the extremities of their edges,
964+
// as we don't want to omit annotations right at the map edges if rounding errors have occurred in the position math
965+
static constexpr double ANNOTATION_CLOSE_TO_EDGE_TOLERANCE_MM = 0.01;
966+
961967
struct GridLineAnnotation
962968
{
963969
Qgis::MapGridBorderSide border = Qgis::MapGridBorderSide::Left; // border on which the annotation is

0 commit comments

Comments
 (0)