Coverage for pyguymer3/openstreetmap/zoom.py: 14%

7 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-08 18:47 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def zoom( 

5 lat_deg, 

6 res, 

7 /, 

8 *, 

9 scale = 1, 

10): 

11 """ 

12 Calculate the required zoom to achieve a given resolution in the centre of a tile at a given latitude. 

13 

14 Parameters 

15 ---------- 

16 lat_deg : float 

17 the latitude (in degrees) 

18 res : float 

19 the resolution (in metres/pixel) 

20 scale : int, optional 

21 the scale of the tile 

22 

23 Returns 

24 ------- 

25 zoomOfEarth : int 

26 the zoom 

27 

28 Notes 

29 ----- 

30 `OpenStreetMap have tabulated a few values <https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale>`_ . 

31 

32 Copyright 2017 Thomas Guymer [1]_ 

33 

34 References 

35 ---------- 

36 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

37 """ 

38 

39 # Import standard modules ... 

40 import math 

41 

42 # Import sub-functions ... 

43 from ..consts import CIRCUMFERENCE_OF_EARTH 

44 

45 # Create short-hand ... 

46 tileSize = scale * 256 # [px] 

47 

48 # Calculate zoom ... 

49 lat_rad = math.radians(lat_deg) # [rad] 

50 zoomOfEarth = math.log2(CIRCUMFERENCE_OF_EARTH * math.cos(lat_rad) / (float(tileSize) * res)) 

51 

52 # Return answer ... 

53 return round(math.ceil(zoomOfEarth))