@@ -203,7 +203,15 @@ bool QgsDecorationNorthArrow::calculateNorthDirection()
203
203
204
204
bool goodDirn = false ;
205
205
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 () )
207
215
{
208
216
QgsCoordinateReferenceSystem outputCRS = mapCanvas->mapRenderer ()->destinationCrs ();
209
217
@@ -216,7 +224,6 @@ bool QgsDecorationNorthArrow::calculateNorthDirection()
216
224
217
225
QgsCoordinateTransform transform ( outputCRS, ourCRS );
218
226
219
- QgsRectangle extent = mapCanvas->extent ();
220
227
QgsPoint p1 ( extent.center () );
221
228
// A point a bit above p1. XXX assumes that y increases up!!
222
229
// May need to involve the maptopixel transform if this proves
@@ -255,20 +262,24 @@ bool QgsDecorationNorthArrow::calculateNorthDirection()
255
262
double x = cos ( p1.y () ) * sin ( p2.y () ) -
256
263
sin ( p1.y () ) * cos ( p2.y () ) * cos ( p2.x () - p1.x () );
257
264
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.
258
269
if ( y > 0.0 )
259
270
{
260
- if ( x > TOL )
271
+ if ( x > 0.0 && ( y / x ) > TOL )
261
272
angle = atan ( y / x );
262
- else if ( x < -TOL )
273
+ else if ( x < 0.0 && ( y / x ) < -TOL )
263
274
angle = PI - atan ( -y / x );
264
275
else
265
276
angle = 0.5 * PI;
266
277
}
267
278
else if ( y < 0.0 )
268
279
{
269
- if ( x > TOL )
280
+ if ( x > 0.0 && ( y / x ) < - TOL )
270
281
angle = -atan ( -y / x );
271
- else if ( x < - TOL )
282
+ else if ( x < 0.0 && ( y / x ) > TOL )
272
283
angle = atan ( y / x ) - PI;
273
284
else
274
285
angle = 1.5 * PI;
0 commit comments