Index: src/common/gnuboat_common/spatial/tilegenerator.py
===================================================================
--- src/common/gnuboat_common/spatial/tilegenerator.py	(revision 5e16955853266e68c24fd278d8f0c96f74f4120c)
+++ src/common/gnuboat_common/spatial/tilegenerator.py	(revision 831127686ec0d86c0d75ef010cd7b51fa129d608)
@@ -27,4 +27,6 @@
 import math
 import copy
+from pathlib import Path
+from PIL import Image
 
 try:
@@ -84,16 +86,16 @@
 
 class LngLatBbox(dict):
-	def __init__(self, east, south, west, north):
-		super(LngLatBbox, self).__init__({'east': east, 'south': south, 'west': west, 'north': north})
-		
+	def __init__(self, west, south, east, north):
+		super(LngLatBbox, self).__init__({'west': west, 'south': south, 'east': east, 'north': north})
+		
+	def west(self):
+		return self['west']
+	
+	def south(self):
+		return self['south']
+	
 	def east(self):
 		return self['east']
 	
-	def south(self):
-		return self['south']
-	
-	def west(self):
-		return self['west']
-	
 	def north(self):
 		return self['north']
@@ -103,8 +105,8 @@
 	
 	def from_mercantile_bbox(mercantile_bbox):
-		return LngLatBbox(mercantile_bbox.east, mercantile_bbox.south, mercantile_bbox.west, mercantile_bbox.north)
+		return LngLatBbox(mercantile_bbox.west, mercantile_bbox.south, mercantile_bbox.east, mercantile_bbox.north)
 	
 	def from_dict(dictionary):
-		return LngLatBbox(dictionary['east'], dictionary['south'], dictionary['west'], dictionary['north'])
+		return LngLatBbox(dictionary['west'], dictionary['south'], dictionary['east'], dictionary['north'])
 
 class RasterSlice(dict):
@@ -121,5 +123,5 @@
 		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})
 		
-	def lng_lat_box(self):
+	def lng_lat_bbox(self):
 		return self['lng_lat_box']
 	
@@ -170,5 +172,5 @@
 			lng_lat_box is of Type LngLatBbox
 		"""
-		tile_bounding = mercantile.bounding_tile(lng_lat_box.west, lng_lat_box.south, lng_lat_box.east, lng_lat_box.north)
+		tile_bounding = mercantile.bounding_tile(lng_lat_box.west(), lng_lat_box.south(), lng_lat_box.east(), lng_lat_box.north())
 		if tile_bounding.z > zoom_range.minimum():
 			return LngLatBbox.from_mercantile_bbox(mercantile.bounds(mercantile.parent(tile_bounding, zoom=zoom_range.minimum())))
@@ -193,10 +195,12 @@
 		for tile_size in tile_sizes:
 			# fitting to tile borders
-			lng_lat_box = self.fit_extent_to_tiles(mercantile.LngLatBbox(west, south, east, north), zoom_range)
-			tiles_zoom_min = mercantile.tiles(lng_lat_box.west(), lng_lat_box.south(), lng_lat_box.east(), lng_lat_box.north(), zoom_range.minimum())
+			lng_lat_box = self.fit_extent_to_tiles(LngLatBbox(west, south, east, north), zoom_range)
+			# The lng_lat_box is exactly on the border between tiles. Since there are rounding errors,
+			# we move the corner edges a very little bit (about a few mm) into the tile. This makes sure,
+			# that we only get those tiles returned, which are really in the lng_lat_box.
+			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())
 					
-			# calculating size of raster file for GDT_Float32 = 24bits (at least the tif'S seen at my pc)
-			tile_count_max =  math.floor(raster_file_size_max / (tile_size**2 * 24))
-			delta_zoom = math.floor(math.log(tile_count_max) / math.log(4))
+			# calculating size of raster file for GDT_Float32 = 32bits
+			delta_zoom = math.floor(math.log(raster_file_size_max / (tile_size**2 * 32)) / math.log(4))
 			steps = math.floor((zoom_range.maximum() - zoom_range.minimum()) / delta_zoom)
 			zoom_steps = []
@@ -204,12 +208,10 @@
 			while step < zoom_range.maximum():
 				zoom_steps.append(step)
-				if step + math.floor(1.5 * delta_zoom) > zoom_range.maximum():
-					break
-				step += delta_zoom
+				step += delta_zoom + 1
 			for tile in tiles_zoom_min:
-				raster_slices += self._slice_tile(tile, zoom_steps, zoom_range.maximum(), tile_count_max, tile_size)
+				raster_slices += self._slice_tile(tile, zoom_steps, zoom_range.maximum(), tile_size, source_layer)
 		return raster_slices
 
-	def _slice_tile(self, parent_tile, zoom_steps, zoom_total_max, tiles_per_slice, tile_size):
+	def _slice_tile(self, parent_tile, zoom_steps, zoom_total_max, tile_size, source_layer, tiles_with_geometry = True):
 		if len(zoom_steps) < 1:
 			raise ValueError('No zoomsteps given to slice tile generation')
@@ -232,5 +234,10 @@
 			zoom_min_new = zoom_steps_new[0]
 			for child in mercantile.children(parent_tile, zoom=zoom_min_new):
-				raster_slices += self._slice_tile(child, zoom_steps_new, zoom_total_max, tiles_per_slice, tile_size)
+				# test, if tile has geometry
+				lng_lat_box = mercantile.bounds(child)
+				source_layer.SetSpatialFilterRect(lng_lat_box.west, lng_lat_box.south, lng_lat_box.east, lng_lat_box.north)
+				if source_layer.GetNextFeature() or not tiles_with_geometry:
+					raster_slices += self._slice_tile(child, zoom_steps_new, zoom_total_max, tile_size, source_layer, tiles_with_geometry = tiles_with_geometry)
+				source_layer.SetSpatialFilter(None)
 		return raster_slices
 
@@ -309,8 +316,8 @@
 			height = tile_slice.grid_height(),
 			outputSRS = "WGS84",
-			outputBounds = [tile_slice.lng_lat_box().east(), tile_slice.lng_lat_box().south(), tile_slice.lng_lat_box().west(), tile_slice.lng_lat_box().north()],
+			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()],
 			noData = nodata_value,
 			layers = layers,
-			#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()), 
+			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()), 
 			algorithm = gridding_algorithm
 		)
@@ -363,25 +370,31 @@
 		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)
 
-	def move_tiles_in_place(self, source_path, target_path):
-		if not os.path.exists(target_path):
-			target_parent = os.path.dirname(target_path)
-			if not os.path.exists(target_parent):
-				os.makedirs(target_parent)
-			shutil.copytree(source_path, target_path)
-			return
-		for item in os.listdir(source_path):
-			src = os.path.join(source_path, item)
-			if os.path.isdir(src):
-				target_path_new = os.path.join(target_path, item)
-				if os.path.exists(target_path_new):
-					self.move_tiles_in_place(src, target_path_new)
-				else:
-					if not os.path.exists(target_path):
-						os.makedirs(target_path)
-					shutil.copytree(src, target_path_new)
+	def move_tiles_in_place(self, source_path, target_path, skip_empty = True):
+		"""
+			Moves the generated tiles from the source_path into the target_path.
+			It moves file per file, so that the tiles are going to be integrated into an existing folder.
+			If skip_emtpy = True only one empty tile is copied to the top level directory. All other empty
+			tiles are not moved into target_path.
+		"""
+		copied_empty_tile = False
+		
+		src_path = Path(source_path)
+		trg_path = Path(target_path)
+		empty_tile = trg_path.joinpath('empty_tile.png')
+		for path in Path(source_path).rglob('*.png'):
+			# Substract source_path from path and make it relative to
+			# target_path
+			relative = path.relative_to(src_path)
+			relative_path = trg_path.joinpath(relative)
+			if self._tile_is_empty(path) and skip_empty:
+				if not copied_empty_tile:
+					if not empty_tile.parent.exists():
+						os.makedirs(empty_tile.parent)
+					shutil.copy(path, empty_tile)
+					copied_empty_tile = True
 			else:
-				if not os.path.exists(target_path):
-					os.makedirs(target_path)
-				shutil.copy(src, target_path)
+				if not relative_path.parent.exists():
+					os.makedirs(relative_path.parent)
+				shutil.copy(path, relative_path)
 
 	def _datasource_open(self, source_file):
@@ -392,4 +405,21 @@
 			raise ReferenceError('No layers in input file') 
 		return datasource
+
+	def _tile_is_empty(self, tile_file_path):
+		is_empty = True
+		colors_tmp = set()
+		colors_tmp_transparent = set()
+		
+		img = Image.open(tile_file_path)
+		# calculate histogram for blue channel
+		colors_img = img.getcolors(256)
+		if not colors_img:
+			return False
+		else:
+			for color in colors_img:
+				color_hex = ''.join('{:02X}'.format(value) for value in color[1])
+				if color[1][3] != 0:
+					return False
+		return True
 
 	#def contours_create():
