Changeset 9f5877d


Ignore:
Timestamp:
Mar 25, 2020, 8:47:52 PM (7 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, master
Children:
5e16955
Parents:
f601952
git-author:
Joachim Langenbach <joachim.langenbach@…> (03/25/20 20:46:29)
git-committer:
Joachim Langenbach <joachim.langenbach@…> (03/25/20 20:47:52)
Message:

Moved all functions needed to generate raster data and tiles into new class TileGenerator in gnuboat_common

Location:
src/common
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/common/bin/gnuboat-log-analyser

    rf601952 r9f5877d  
    3232
    3333from gnuboat_common.datahandler import loghandler, logdataset
     34from gnuboat_common.spatial import tilegenerator
    3435
    3536# -999 is a workaround, as long gdal python api does not handle addAlpha
    … …  
    192193        output_file.close()
    193194
    194         # processing data with gdal
    195         ogr.UseExceptions()
    196         source_data = ogr.Open(source_xml_file)
    197         source_layer = source_data.GetLayer(source_data_layer)
    198         source_x_min, source_x_max, source_y_min, source_y_max = source_layer.GetExtent()
    199                
    200         # create the grid data
    201         gdal_file = "/vsimem/water_depth.tif"
    202         tile_resolution = tile_width(target_zoomlevel)
    203         target_x_res = int(tile_resolution["tile_size"] / tile_resolution["longitude"] * (source_x_max - source_x_min))
    204         target_y_res = int(tile_resolution["tile_size"] / tile_resolution["latitude"] * (source_y_max - source_y_min))
    205        
    206         lon_metre_max = 60 * 1852 * math.cos(math.radians(min(source_y_min, source_y_max))) # metre / degree
    207         lat_metre_max = 111694 # metre / degree
    208        
    209         circle_radius = math.sqrt(density_base) / math.pi
    210         radius_x = circle_radius / lon_metre_max
    211         radius_y = circle_radius / lat_metre_max
    212         gridding_algorithm = "average:radius1={}:radius2={}:angle=0.0:min_points={}:nodata={}".format(radius_x, radius_y, density_min, target_nodata_value)
    213        
    214         if statistics:
    215                 print("Source statistics:")
    216                 print("  source EPSG: {}".format(input_epsg))
    217                 print("  extents (x_min, y_min, x_max, y_max): {}, {}, {}, {}".format(source_x_min, source_y_min, source_x_max, source_y_max))
    218                 degree_per_pixel_lon = tile_resolution["longitude"] / tile_resolution["tile_size"]
    219                 degree_per_pixel_lat = tile_resolution["latitude"] / tile_resolution["tile_size"]
    220                 print("  degree / pixel x and y: {}, {}".format(degree_per_pixel_lon, degree_per_pixel_lat))
    221                 print("  metre / pixel x and y: {}, {}".format(degree_per_pixel_lon * lon_metre_max, degree_per_pixel_lat * lat_metre_max))
    222                 print("  Min density per squaredegree: {}".format(density_min * density_base / lon_metre_max / lat_metre_max))
    223                 print("  Min density per pixel: {}".format(density_min * density_base / (degree_per_pixel_lon * lon_metre_max) / (degree_per_pixel_lat * lat_metre_max)))
    224                 print("  gridding algorithm {}".format(gridding_algorithm))
    225        
    226         grid_options = gdal.GridOptions(
    227                 outputType = gdal.GDT_Float32,
    228                 format = "GTiff",
    229                 width = target_x_res,
    230                 height = target_y_res,
    231                 outputSRS = "WGS84",
    232                 noData = target_nodata_value,
    233                 layers = source_data_layer,
    234                 #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)
    235                 algorithm = gridding_algorithm
    236         )
    237         gdal.Grid(gdal_file, source_xml_file, options = grid_options)
    238         print("  Created raster Grid in memory")
    239        
    240         # hillshade
    241         gdal_file_hillshade  = '/vsimem/water_depth_hillshade.tif'
    242         gdal.DEMProcessing(gdal_file_hillshade, gdal_file, "hillshade", options = gdal.DEMProcessingOptions(format = "GTiff", zFactor = -1))
    243         print("  Created hillshade in memory")
    244        
    245         # color relief
    246         gdal_file_color_relief = '/vsimem/water_depth_color_relief.tif'
    247         gdal_file_color_relief_config = os.path.join(output_directory, "tmp_water_depth_color_relief_config.txt")
    248         with open(gdal_file_color_relief_config, 'w') as color_config:
    249                 for depth in colors:
    250                         color_config.write("{} {}\n".format(depth, colors[depth]))
    251         gdal.DEMProcessing(gdal_file_color_relief, gdal_file, "color-relief", options = gdal.DEMProcessingOptions(format = "GTiff", colorFilename = gdal_file_color_relief_config), addAlpha = True)
    252         print("  Created color relief in memory")
    253        
    254         # slopeshade
    255         gdal_file_slopeshade  = os.path.join(output_directory, "water_depth_slopeshade.tif")
    256         gdal_file_slopeshade_tmp = "/vsimem/tmp_water_depth_slopeshade.tif"
    257         gdal_file_slopeshade_config = os.path.join(output_directory, "tmp_water_depth_slopeshade_config.txt")
    258         with open(gdal_file_slopeshade_config, 'w') as shade_config:
    259                 shade_config.write("0 255 255 255\n")
    260                 shade_config.write("90 0 0 0\n")
    261                 shade_config.write("270 0 0 0\n")
    262                 shade_config.write("360 255 255 255\n")
    263         gdal.DEMProcessing(gdal_file_slopeshade_tmp, gdal_file, "slope", options = gdal.DEMProcessingOptions(format = "GTiff"), slopeFormat = 'degree', zeroForFlat = True, addAlpha = True)
    264         gdal.DEMProcessing(gdal_file_slopeshade, gdal_file_slopeshade_tmp, "color-relief", options = gdal.DEMProcessingOptions(format = "GTiff", colorFilename = gdal_file_slopeshade_config), addAlpha = True)
    265         print("  Created slope shade in {}".format(gdal_file_slopeshade))
    266        
    267        
    268         # generate contour lines
    269         # @todo Cut Tifs / warp
    270         contour_input = gdal.Open(gdal_file, gdalconst.GA_ReadOnly)
    271         contour_input_band = contour_input.GetRasterBand(1)
    272         #Generate layer to save Contourlines in
    273         contour_output = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource(gdal_file_contours)
    274         proj = osr.SpatialReference()
    275         proj.SetWellKnownGeogCS(input_epsg)
    276         for depth in colors:
    277                 contour_layer = contour_output.CreateLayer('contour_{}_{}_{}'.format(density_min, density_base, depth), srs = proj)
    278                 field_definition = ogr.FieldDefn("ID", ogr.OFTInteger)
    279                 contour_layer.CreateField(field_definition)
    280                 field_definition = ogr.FieldDefn("elev", ogr.OFTReal)
    281                 contour_layer.CreateField(field_definition)
    282                 #Generate Contourlines
    283                 gdal.ContourGenerate(contour_input_band, depth, 0, [], 0, 0, contour_layer, 0, 1)
    284         contour_output = None
    285         del contour_output
    286        
    287         # combining hillshade and color relief
    288        
    289        
    290         # cropping the tifs
    291         #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))
    292 
    293         # dstNodata is a workaround for not working addAlpha in color-relief
    294         gdal_final_result = '/tmp/gnuboat/water_depth_{}_{}.tif'.format(density_min, density_base)
    295         gdal_color_relief_alpha_tmp = '/vsimem/tmp_color_relief.tif'
    296         gdal.Warp(gdal_final_result, [gdal_file_color_relief, gdal_file_hillshade], options = gdal.WarpOptions(format = 'GTiff', dstAlpha = False, dstNodata = "255 255 255"))
    297 
    298         # generate tiles
    299         tile_zoom_option = "0-19"
    300         tile_commands = [
    301                 #combined
    302                 {"file": gdal_final_result, "title": 'GNUBoat {} datasets per {} squaremetre'.format(density_min, density_base), "output": 'gnuboat_{}_{}'.format(density_min, density_base)},
    303         ]
    304         print("Expanding {} tifs to tiles".format(len(tile_commands)))
    305         for tile_command in tile_commands:
    306                 title = tile_command["title"]
    307                 output = os.path.join(output_directory, tile_command["output"])
    308                 os.system('gdal2tiles.py -q -w none -z {} -t "GNUBoat {}" {} {}'.format(tile_zoom_option, title, tile_command["file"], output))
    309                 print("  Created tiles for {} in {}".format(title, output))
     195        tilegen = tilegenerator.TileGenerator(zoom_range=5-19, debug=statistics)
     196        tilegen.run(source_xml_file, output_directory)
    310197
    311198        sys.exit(0)
  • src/common/gnuboat_common/__init__.py

    rf601952 r9f5877d  
    1818from .datahandler.loghandler import LogHandler
    1919from .datahandler.logdataset import LogDataset
     20from .spatial.tilegenerator import TileGenerator
  • src/common/setup.py

    rf601952 r9f5877d  
    3030        "pynmea2",
    3131        "gdal",
     32        "mercantile",
    3233]
    3334
    … …  
    4344  #package_dir={'': ''},
    4445  packages=find_packages(),
    45   scripts=["bin/gnuboat-log-converter", "bin/gnuboat-log-analyser"],
     46  scripts=["bin/gnuboat-log-converter", "bin/gnuboat-log-analyser", "bin/gdal2tiles-gnuboat.py"],
    4647  long_description=README_TEXT,
    4748  install_requires=reqs_install,
Note: See TracChangeset for help on using the changeset viewer.