Index: src/common/bin/gnuboat-log-analyser
===================================================================
--- src/common/bin/gnuboat-log-analyser	(revision 9af063801900ffaaadeaa8dcb5a6d31a87e2caf5)
+++ src/common/bin/gnuboat-log-analyser	(revision 9f5877dc45dcf00238a6d2e688fcc536e3373191)
@@ -32,4 +32,5 @@
 
 from gnuboat_common.datahandler import loghandler, logdataset
+from gnuboat_common.spatial import tilegenerator
 
 # -999 is a workaround, as long gdal python api does not handle addAlpha
@@ -192,120 +193,6 @@
 	output_file.close()
 
-	# processing data with gdal
-	ogr.UseExceptions()
-	source_data = ogr.Open(source_xml_file)
-	source_layer = source_data.GetLayer(source_data_layer)
-	source_x_min, source_x_max, source_y_min, source_y_max = source_layer.GetExtent()
-		
-	# create the grid data
-	gdal_file = "/vsimem/water_depth.tif"
-	tile_resolution = tile_width(target_zoomlevel)
-	target_x_res = int(tile_resolution["tile_size"] / tile_resolution["longitude"] * (source_x_max - source_x_min))
-	target_y_res = int(tile_resolution["tile_size"] / tile_resolution["latitude"] * (source_y_max - source_y_min))
-	
-	lon_metre_max = 60 * 1852 * math.cos(math.radians(min(source_y_min, source_y_max))) # metre / degree
-	lat_metre_max = 111694 # metre / degree
-	
-	circle_radius = math.sqrt(density_base) / math.pi
-	radius_x = circle_radius / lon_metre_max
-	radius_y = circle_radius / lat_metre_max
-	gridding_algorithm = "average:radius1={}:radius2={}:angle=0.0:min_points={}:nodata={}".format(radius_x, radius_y, density_min, target_nodata_value)
-	
-	if statistics:
-		print("Source statistics:")
-		print("  source EPSG: {}".format(input_epsg))
-		print("  extents (x_min, y_min, x_max, y_max): {}, {}, {}, {}".format(source_x_min, source_y_min, source_x_max, source_y_max))
-		degree_per_pixel_lon = tile_resolution["longitude"] / tile_resolution["tile_size"]
-		degree_per_pixel_lat = tile_resolution["latitude"] / tile_resolution["tile_size"]
-		print("  degree / pixel x and y: {}, {}".format(degree_per_pixel_lon, degree_per_pixel_lat))
-		print("  metre / pixel x and y: {}, {}".format(degree_per_pixel_lon * lon_metre_max, degree_per_pixel_lat * lat_metre_max))
-		print("  Min density per squaredegree: {}".format(density_min * density_base / lon_metre_max / lat_metre_max))
-		print("  Min density per pixel: {}".format(density_min * density_base / (degree_per_pixel_lon * lon_metre_max) / (degree_per_pixel_lat * lat_metre_max)))
-		print("  gridding algorithm {}".format(gridding_algorithm))
-	
-	grid_options = gdal.GridOptions(
-		outputType = gdal.GDT_Float32,
-		format = "GTiff",
-		width = target_x_res,
-		height = target_y_res,
-		outputSRS = "WGS84",
-		noData = target_nodata_value,
-		layers = source_data_layer,
-		#algorithm = "invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata={}".format(target_nodata_value)
-		algorithm = gridding_algorithm
-	)
-	gdal.Grid(gdal_file, source_xml_file, options = grid_options)
-	print("  Created raster Grid in memory")
-	
-	# hillshade
-	gdal_file_hillshade  = '/vsimem/water_depth_hillshade.tif'
-	gdal.DEMProcessing(gdal_file_hillshade, gdal_file, "hillshade", options = gdal.DEMProcessingOptions(format = "GTiff", zFactor = -1))
-	print("  Created hillshade in memory")
-	
-	# color relief
-	gdal_file_color_relief = '/vsimem/water_depth_color_relief.tif'
-	gdal_file_color_relief_config = os.path.join(output_directory, "tmp_water_depth_color_relief_config.txt")
-	with open(gdal_file_color_relief_config, 'w') as color_config:
-		for depth in colors:
-			color_config.write("{} {}\n".format(depth, colors[depth]))
-	gdal.DEMProcessing(gdal_file_color_relief, gdal_file, "color-relief", options = gdal.DEMProcessingOptions(format = "GTiff", colorFilename = gdal_file_color_relief_config), addAlpha = True)
-	print("  Created color relief in memory")
-	
-	# slopeshade
-	gdal_file_slopeshade  = os.path.join(output_directory, "water_depth_slopeshade.tif")
-	gdal_file_slopeshade_tmp = "/vsimem/tmp_water_depth_slopeshade.tif"
-	gdal_file_slopeshade_config = os.path.join(output_directory, "tmp_water_depth_slopeshade_config.txt")
-	with open(gdal_file_slopeshade_config, 'w') as shade_config:
-		shade_config.write("0 255 255 255\n")
-		shade_config.write("90 0 0 0\n")
-		shade_config.write("270 0 0 0\n")
-		shade_config.write("360 255 255 255\n")
-	gdal.DEMProcessing(gdal_file_slopeshade_tmp, gdal_file, "slope", options = gdal.DEMProcessingOptions(format = "GTiff"), slopeFormat = 'degree', zeroForFlat = True, addAlpha = True)
-	gdal.DEMProcessing(gdal_file_slopeshade, gdal_file_slopeshade_tmp, "color-relief", options = gdal.DEMProcessingOptions(format = "GTiff", colorFilename = gdal_file_slopeshade_config), addAlpha = True)
-	print("  Created slope shade in {}".format(gdal_file_slopeshade))
-	
-	
-	# generate contour lines
-	# @todo Cut Tifs / warp
-	contour_input = gdal.Open(gdal_file, gdalconst.GA_ReadOnly)
-	contour_input_band = contour_input.GetRasterBand(1)
-	#Generate layer to save Contourlines in
-	contour_output = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource(gdal_file_contours)
-	proj = osr.SpatialReference()
-	proj.SetWellKnownGeogCS(input_epsg)
-	for depth in colors:
-		contour_layer = contour_output.CreateLayer('contour_{}_{}_{}'.format(density_min, density_base, depth), srs = proj)
-		field_definition = ogr.FieldDefn("ID", ogr.OFTInteger)
-		contour_layer.CreateField(field_definition)
-		field_definition = ogr.FieldDefn("elev", ogr.OFTReal)
-		contour_layer.CreateField(field_definition)
-		#Generate Contourlines
-		gdal.ContourGenerate(contour_input_band, depth, 0, [], 0, 0, contour_layer, 0, 1)
-	contour_output = None
-	del contour_output
-	
-	# combining hillshade and color relief
-	
-	
-	# cropping the tifs
-	#gdal.Warp('/tmp/gnuboat/test.tif', gdal_file_color_relief, options = gdal.WarpOptions(format = 'GTiff', dstAlpha = True, srcNodata = "255 255 255", dstNodata = target_nodata_value, cutlineDSName = '/tmp/gnuboat/contours/contour-0.shp', cutlineLayer="contour-0", cropToCutline=True))
-
-	# dstNodata is a workaround for not working addAlpha in color-relief
-	gdal_final_result = '/tmp/gnuboat/water_depth_{}_{}.tif'.format(density_min, density_base)
-	gdal_color_relief_alpha_tmp = '/vsimem/tmp_color_relief.tif'
-	gdal.Warp(gdal_final_result, [gdal_file_color_relief, gdal_file_hillshade], options = gdal.WarpOptions(format = 'GTiff', dstAlpha = False, dstNodata = "255 255 255"))
-
-	# generate tiles
-	tile_zoom_option = "0-19"
-	tile_commands = [
-		#combined
-		{"file": gdal_final_result, "title": 'GNUBoat {} datasets per {} squaremetre'.format(density_min, density_base), "output": 'gnuboat_{}_{}'.format(density_min, density_base)},
-	]
-	print("Expanding {} tifs to tiles".format(len(tile_commands)))
-	for tile_command in tile_commands:
-		title = tile_command["title"]
-		output = os.path.join(output_directory, tile_command["output"])
-		os.system('gdal2tiles.py -q -w none -z {} -t "GNUBoat {}" {} {}'.format(tile_zoom_option, title, tile_command["file"], output))
-		print("  Created tiles for {} in {}".format(title, output))
+	tilegen = tilegenerator.TileGenerator(zoom_range=5-19, debug=statistics)
+	tilegen.run(source_xml_file, output_directory)
 
 	sys.exit(0)
