Coverage for pygeoapi/provider/tile.py: 40%

52 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 08:15 +0000

1# ================================================================= 

2# 

3# Authors: Francesco Bartoli <xbartolone@gmail.com> 

4# Authors: Tom Kralidis <tomkralidis@gmail.com> 

5# 

6# Copyright (c) 2025 Francesco Bartoli 

7# Copyright (c) 2022 Tom Kralidis 

8# 

9# Permission is hereby granted, free of charge, to any person 

10# obtaining a copy of this software and associated documentation 

11# files (the "Software"), to deal in the Software without 

12# restriction, including without limitation the rights to use, 

13# copy, modify, merge, publish, distribute, sublicense, and/or sell 

14# copies of the Software, and to permit persons to whom the 

15# Software is furnished to do so, subject to the following 

16# conditions: 

17# 

18# The above copyright notice and this permission notice shall be 

19# included in all copies or substantial portions of the Software. 

20# 

21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 

22# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 

23# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 

24# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 

25# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 

26# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 

27# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

28# OTHER DEALINGS IN THE SOFTWARE. 

29# 

30# ================================================================= 

31 

32import logging 

33from http import HTTPStatus 

34 

35from pygeoapi.models.provider.base import ( 

36 TileMatrixSetEnum) 

37 

38from pygeoapi.provider.base import ( 

39 ProviderGenericError, ProviderItemNotFoundError) 

40 

41LOGGER = logging.getLogger(__name__) 

42 

43 

44class BaseTileProvider: 

45 """generic Tile Provider ABC""" 

46 

47 def __init__(self, provider_def): 

48 """ 

49 Initialize object 

50 

51 :param provider_def: provider definition 

52 

53 :returns: pygeoapi.provider.tile.BaseTileProvider 

54 """ 

55 

56 self.name = provider_def['name'] 

57 self.data = provider_def['data'] 

58 self.format_type = provider_def['format']['name'] 

59 self.mimetype = provider_def['format']['mimetype'] 

60 self.options = provider_def.get('options') 

61 self.schemes = self.options.get('schemes', []) 

62 self.tile_type = None 

63 

64 def get_layer(self): 

65 """ 

66 Get provider layer name 

67 

68 :returns: `string` of layer name 

69 """ 

70 

71 def get_fields(self): 

72 """ 

73 Get provider field information (names, types) 

74 

75 :returns: `dict` of fields 

76 """ 

77 

78 raise NotImplementedError() 

79 

80 def get_tiling_schemes(self): 

81 """ 

82 Get provider field information (names, types) 

83 

84 :returns: `dict` of tiling schemes 

85 """ 

86 

87 raise NotImplementedError() 

88 

89 def get_tiles_service(self, baseurl, servicepath, dirpath, 

90 tile_type): 

91 """ 

92 Gets tile service description 

93 

94 :param baseurl: base URL of endpoint 

95 :param servicepath: base path of URL 

96 :param dirpath: directory basepath (equivalent of URL) 

97 :param tile_type: tile format type 

98 

99 :returns: `dict` of file listing or `dict` of GeoJSON item or raw file 

100 """ 

101 

102 raise NotImplementedError() 

103 

104 def get_tiles(self, layer, tileset, z, y, x, format_): 

105 """ 

106 Gets tiles data 

107 

108 :param layer: tile layer 

109 :param tileset: tile set 

110 :param z: z index 

111 :param y: y index 

112 :param x: x index 

113 :param format_: tile format type 

114 

115 :returns: `binary` of the tile 

116 """ 

117 

118 raise NotImplementedError() 

119 

120 def get_metadata(self): 

121 """ 

122 Provide data/file metadata 

123 

124 :returns: `dict` of metadata construct (format 

125 determined by provider/standard) 

126 """ 

127 

128 raise NotImplementedError() 

129 

130 def is_in_limits(self, tilematrixset, z, x, y): 

131 """ 

132 Is within the limits of the tilematrixset 

133 

134 :param z: tilematrix 

135 :param x: x 

136 :param y: y 

137 

138 :returns: wether this tile is within the tile matrix 

139 set limits (Boolean) 

140 """ 

141 

142 try: 

143 x, y, z = map(int, (x, y, z)) 

144 except ValueError: 

145 LOGGER.warning('Unable to cast tile index to integer') 

146 return False 

147 

148 return all([ 

149 x < tilematrixset.tileMatrices[z]['matrixWidth'], 

150 y < tilematrixset.tileMatrices[z]['matrixHeight'], 

151 self.options['zoom']['min'] <= z <= self.options['zoom']['max'] 

152 ]) 

153 

154 def get_tilematrixset(self, tileMatrixSetId): 

155 """ 

156 Get tilematrixset 

157 

158 :param tileMatrixSetId: tilematrixsetid str 

159 

160 :returns: tilematrixset enum object 

161 """ 

162 

163 enums = [e.value for e in TileMatrixSetEnum] 

164 enum = None 

165 

166 try: 

167 for e in enums: 

168 if tileMatrixSetId == e.tileMatrixSet: 

169 enum = e 

170 if not enum: 

171 raise ValueError('could not find this tilematrixset') 

172 return enum 

173 

174 except ValueError as err: 

175 LOGGER.error(err) 

176 

177 

178class ProviderTileQueryError(ProviderGenericError): 

179 """provider tile query error""" 

180 default_msg = 'Tile not found' 

181 

182 

183class ProviderTileNotFoundError(ProviderItemNotFoundError): 

184 """provider tile not found error""" 

185 default_msg = 'Tile not found (check logs)' 

186 

187 

188class ProviderTilesetIdNotFoundError(ProviderTileQueryError): 

189 """provider tileset matrix query error""" 

190 default_msg = 'Tileset id not found' 

191 http_status_code = HTTPStatus.NOT_FOUND 

192 ogc_exception_code = 'NotFound'