Changeset d0a651d for src/common/gnuboat_common
- Timestamp:
- Apr 4, 2020, 12:39:38 PM (7 years ago)
- Branches:
- debian/bullseye, master
- Children:
- c5ca247
- Parents:
- b72d9c4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/common/gnuboat_common/spatial/tilegenerator.py
rb72d9c4 rd0a651d 27 27 import math 28 28 import copy 29 from pathlib import Path 30 from PIL import Image 29 31 30 32 try: … … 84 86 85 87 class LngLatBbox(dict): 86 def __init__(self, east, south, west, north): 87 super(LngLatBbox, self).__init__({'east': east, 'south': south, 'west': west, 'north': north}) 88 88 def __init__(self, west, south, east, north): 89 super(LngLatBbox, self).__init__({'west': west, 'south': south, 'east': east, 'north': north}) 90 91 def west(self): 92 return self['west'] 93 94 def south(self): 95 return self['south'] 96 89 97 def east(self): 90 98 return self['east'] 91 99 92 def south(self):93 return self['south']94 95 def west(self):96 return self['west']97 98 100 def north(self): 99 101 return self['north'] … … 103 105 104 106 def from_mercantile_bbox(mercantile_bbox): 105 return LngLatBbox(mercantile_bbox. east, mercantile_bbox.south, mercantile_bbox.west, mercantile_bbox.north)107 return LngLatBbox(mercantile_bbox.west, mercantile_bbox.south, mercantile_bbox.east, mercantile_bbox.north) 106 108 107 109 def from_dict(dictionary): 108 return LngLatBbox(dictionary[' east'], dictionary['south'], dictionary['west'], dictionary['north'])110 return LngLatBbox(dictionary['west'], dictionary['south'], dictionary['east'], dictionary['north']) 109 111 110 112 class RasterSlice(dict): … … 121 123 super(RasterSlice, self).__init__({'lng_lat_box': lng_lat_box, 'zoom_range': zoom_range, 'tile_size': tile_size, 'grid_width': grid_width, 'grid_height': grid_height}) 122 124 123 def lng_lat_b ox(self):125 def lng_lat_bbox(self): 124 126 return self['lng_lat_box'] 125 127 … … 170 172 lng_lat_box is of Type LngLatBbox 171 173 """ 172 tile_bounding = mercantile.bounding_tile(lng_lat_box.west , lng_lat_box.south, lng_lat_box.east, lng_lat_box.north)174 tile_bounding = mercantile.bounding_tile(lng_lat_box.west(), lng_lat_box.south(), lng_lat_box.east(), lng_lat_box.north()) 173 175 if tile_bounding.z > zoom_range.minimum(): 174 176 return LngLatBbox.from_mercantile_bbox(mercantile.bounds(mercantile.parent(tile_bounding, zoom=zoom_range.minimum()))) … … 193 195 for tile_size in tile_sizes: 194 196 # fitting to tile borders 195 lng_lat_box = self.fit_extent_to_tiles(mercantile.LngLatBbox(west, south, east, north), zoom_range) 196 tiles_zoom_min = mercantile.tiles(lng_lat_box.west(), lng_lat_box.south(), lng_lat_box.east(), lng_lat_box.north(), zoom_range.minimum()) 197 lng_lat_box = self.fit_extent_to_tiles(LngLatBbox(west, south, east, north), zoom_range) 198 # The lng_lat_box is exactly on the border between tiles. Since there are rounding errors, 199 # we move the corner edges a very little bit (about a few mm) into the tile. This makes sure, 200 # that we only get those tiles returned, which are really in the lng_lat_box. 201 tiles_zoom_min = mercantile.tiles(lng_lat_box.west() + 1*10**-7, lng_lat_box.south() + 1*10**-7, lng_lat_box.east() - 1*10**-7, lng_lat_box.north() - 1*10**-7, zoom_range.minimum()) 197 202 198 # calculating size of raster file for GDT_Float32 = 24bits (at least the tif'S seen at my pc) 199 tile_count_max = math.floor(raster_file_size_max / (tile_size**2 * 24)) 200 delta_zoom = math.floor(math.log(tile_count_max) / math.log(4)) 203 # calculating size of raster file for GDT_Float32 = 32bits 204 delta_zoom = math.floor(math.log(raster_file_size_max / (tile_size**2 * 32)) / math.log(4)) 201 205 steps = math.floor((zoom_range.maximum() - zoom_range.minimum()) / delta_zoom) 202 206 zoom_steps = [] … … 204 208 while step < zoom_range.maximum(): 205 209 zoom_steps.append(step) 206 if step + math.floor(1.5 * delta_zoom) > zoom_range.maximum(): 207 break 208 step += delta_zoom 210 step += delta_zoom + 1 209 211 for tile in tiles_zoom_min: 210 raster_slices += self._slice_tile(tile, zoom_steps, zoom_range.maximum(), tile_ count_max, tile_size)212 raster_slices += self._slice_tile(tile, zoom_steps, zoom_range.maximum(), tile_size, source_layer) 211 213 return raster_slices 212 214 213 def _slice_tile(self, parent_tile, zoom_steps, zoom_total_max, tile s_per_slice, tile_size):215 def _slice_tile(self, parent_tile, zoom_steps, zoom_total_max, tile_size, source_layer, tiles_with_geometry = True): 214 216 if len(zoom_steps) < 1: 215 217 raise ValueError('No zoomsteps given to slice tile generation') … … 232 234 zoom_min_new = zoom_steps_new[0] 233 235 for child in mercantile.children(parent_tile, zoom=zoom_min_new): 234 raster_slices += self._slice_tile(child, zoom_steps_new, zoom_total_max, tiles_per_slice, tile_size) 236 # test, if tile has geometry 237 lng_lat_box = mercantile.bounds(child) 238 source_layer.SetSpatialFilterRect(lng_lat_box.west, lng_lat_box.south, lng_lat_box.east, lng_lat_box.north) 239 if source_layer.GetNextFeature() or not tiles_with_geometry: 240 raster_slices += self._slice_tile(child, zoom_steps_new, zoom_total_max, tile_size, source_layer, tiles_with_geometry = tiles_with_geometry) 241 source_layer.SetSpatialFilter(None) 235 242 return raster_slices 236 243 … … 309 316 height = tile_slice.grid_height(), 310 317 outputSRS = "WGS84", 311 outputBounds = [tile_slice.lng_lat_b ox().east(), tile_slice.lng_lat_box().south(), tile_slice.lng_lat_box().west(), tile_slice.lng_lat_box().north()],318 outputBounds = [tile_slice.lng_lat_bbox().west(), tile_slice.lng_lat_bbox().north(), tile_slice.lng_lat_bbox().east(), tile_slice.lng_lat_bbox().south()], 312 319 noData = nodata_value, 313 320 layers = layers, 314 #spatFilter = (tile_slice.lng_lat_box().east(), tile_slice.lng_lat_box().south(), tile_slice.lng_lat_box().west(), tile_slice.lng_lat_box().north()),321 spatFilter = (tile_slice.lng_lat_bbox().west(), tile_slice.lng_lat_bbox().south(), tile_slice.lng_lat_bbox().east(), tile_slice.lng_lat_bbox().north()), 315 322 algorithm = gridding_algorithm 316 323 ) … … 363 370 subprocess.run(['gdal2tiles-gnuboat.py', '-q', '-w', 'none', '--tilesize={}'.format(tile_size), '-z', tile_zoom_option, '-t', title, input_file, output_directory], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 364 371 365 def move_tiles_in_place(self, source_path, target_path): 366 if not os.path.exists(target_path): 367 target_parent = os.path.dirname(target_path) 368 if not os.path.exists(target_parent): 369 os.makedirs(target_parent) 370 shutil.copytree(source_path, target_path) 371 return 372 for item in os.listdir(source_path): 373 src = os.path.join(source_path, item) 374 if os.path.isdir(src): 375 target_path_new = os.path.join(target_path, item) 376 if os.path.exists(target_path_new): 377 self.move_tiles_in_place(src, target_path_new) 378 else: 379 if not os.path.exists(target_path): 380 os.makedirs(target_path) 381 shutil.copytree(src, target_path_new) 372 def move_tiles_in_place(self, source_path, target_path, skip_empty = True): 373 """ 374 Moves the generated tiles from the source_path into the target_path. 375 It moves file per file, so that the tiles are going to be integrated into an existing folder. 376 If skip_emtpy = True only one empty tile is copied to the top level directory. All other empty 377 tiles are not moved into target_path. 378 """ 379 copied_empty_tile = False 380 381 src_path = Path(source_path) 382 trg_path = Path(target_path) 383 empty_tile = trg_path.joinpath('empty_tile.png') 384 for path in Path(source_path).rglob('*.png'): 385 # Substract source_path from path and make it relative to 386 # target_path 387 relative = path.relative_to(src_path) 388 relative_path = trg_path.joinpath(relative) 389 if self._tile_is_empty(path) and skip_empty: 390 if not copied_empty_tile: 391 if not empty_tile.parent.exists(): 392 os.makedirs(empty_tile.parent) 393 shutil.copy(path, empty_tile) 394 copied_empty_tile = True 382 395 else: 383 if not os.path.exists(target_path):384 os.makedirs( target_path)385 shutil.copy( src, target_path)396 if not relative_path.parent.exists(): 397 os.makedirs(relative_path.parent) 398 shutil.copy(path, relative_path) 386 399 387 400 def _datasource_open(self, source_file): … … 392 405 raise ReferenceError('No layers in input file') 393 406 return datasource 407 408 def _tile_is_empty(self, tile_file_path): 409 is_empty = True 410 colors_tmp = set() 411 colors_tmp_transparent = set() 412 413 img = Image.open(tile_file_path) 414 # calculate histogram for blue channel 415 colors_img = img.getcolors(256) 416 if not colors_img: 417 return False 418 else: 419 for color in colors_img: 420 color_hex = ''.join('{:02X}'.format(value) for value in color[1]) 421 if color[1][3] != 0: 422 return False 423 return True 394 424 395 425 #def contours_create():
Note:
See TracChangeset
for help on using the changeset viewer.
