Skip to content

Commit 90e7baa

Browse files
committed
Merge pull request #205 from homann/issue1993
Issue1993
2 parents b42067e + a18e396 commit 90e7baa

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/app/qgsdecorationnortharrow.cpp

+17-6
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,15 @@ bool QgsDecorationNorthArrow::calculateNorthDirection()
203203

204204
bool goodDirn = false;
205205

206-
if ( mapCanvas->layerCount() > 0 )
206+
// Get the shown extent...
207+
QgsRectangle canvasExtent = mapCanvas->extent();
208+
// ... and all layers extent, ...
209+
QgsRectangle fullExtent = mapCanvas->fullExtent();
210+
// ... and combine
211+
QgsRectangle extent = canvasExtent.intersect( & fullExtent );
212+
213+
// If no layers are added or shown, we can't get any direction
214+
if ( mapCanvas->layerCount() > 0 && ! extent.isEmpty() )
207215
{
208216
QgsCoordinateReferenceSystem outputCRS = mapCanvas->mapRenderer()->destinationCrs();
209217

@@ -216,7 +224,6 @@ bool QgsDecorationNorthArrow::calculateNorthDirection()
216224

217225
QgsCoordinateTransform transform( outputCRS, ourCRS );
218226

219-
QgsRectangle extent = mapCanvas->extent();
220227
QgsPoint p1( extent.center() );
221228
// A point a bit above p1. XXX assumes that y increases up!!
222229
// May need to involve the maptopixel transform if this proves
@@ -255,20 +262,24 @@ bool QgsDecorationNorthArrow::calculateNorthDirection()
255262
double x = cos( p1.y() ) * sin( p2.y() ) -
256263
sin( p1.y() ) * cos( p2.y() ) * cos( p2.x() - p1.x() );
257264

265+
// Use TOL to decide if the quotient is big enough.
266+
// Both x and y can be very small, if heavily zoomed
267+
// For small y/x, we set directly angle 0. Not sure
268+
// if this is needed.
258269
if ( y > 0.0 )
259270
{
260-
if ( x > TOL )
271+
if ( x > 0.0 && ( y / x ) > TOL )
261272
angle = atan( y / x );
262-
else if ( x < -TOL )
273+
else if ( x < 0.0 && ( y / x ) < -TOL )
263274
angle = PI - atan( -y / x );
264275
else
265276
angle = 0.5 * PI;
266277
}
267278
else if ( y < 0.0 )
268279
{
269-
if ( x > TOL )
280+
if ( x > 0.0 && ( y / x ) < -TOL )
270281
angle = -atan( -y / x );
271-
else if ( x < -TOL )
282+
else if ( x < 0.0 && ( y / x ) > TOL )
272283
angle = atan( y / x ) - PI;
273284
else
274285
angle = 1.5 * PI;

0 commit comments

Comments
 (0)