Changeset c5b13e1 for src/python


Ignore:
Timestamp:
Jan 20, 2019, 5:21:35 PM (8 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, master
Children:
74c3571
Parents:
46f871f
Message:
  • gnuboat-log-analyser
    • Combines hillshade and color-relief in one tiff
    • Generating tiles from combined tiff with transperency for nodata
  • Added high and low accuracy layer to map
File:
1 edited

Legend:

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

    r46f871f rc5b13e1  
    3333from gnuboat.datahandler import loghandler, logdataset
    3434
    35 
     35# -999 is a workaround, as long gdal python api does not handle addAlpha
    3636colors = {
     37        -999: "255 255 255",
    3738        0: "0 0 139",
    3839        2: "30 144 255",
    … …  
    4142}
    4243input_epsg = 'EPSG:4326'
    43 target_nodata_value = 0.0
     44target_nodata_value = -999.0
    4445target_zoomlevel = 19
    45 density_min = 20 # number of points per densitiy_base squaremetres needed to set a value different from nodata
    46 densitiy_base = 100 # squaremetre used as base for density_min
     46density_min = 10 # number of points per density_base squaremetres needed to set a value different from nodata
     47density_base = 100 # squaremetre used as base for density_min
    4748
    4849class CsvCallback:
    … …  
    7374
    7475def main(argv):
    75         global color, input_epsg, target_nodata_value, target_zoomlevel
     76        global color, input_epsg, target_nodata_value, target_zoomlevel, density_min, density_base
    7677
    7778        input_file_path = None
    7879        output_directory = None
    7980        statistics = False
     81       
     82        gdal.UseExceptions()
    8083
    8184        parser = argparse.ArgumentParser(description='Generates the digital terrain model from gnuboat logfiles')
    … …  
    8790                        help='Print some statistics of the input and result datasets')
    8891        parser.add_argument('-p', '--points-min', dest='points_min', type=int,
    89                         help='Print min points per 100 squaremetre', default=10)
     92                        help='Print min points per {} squaremetre'.format(density_base), default=density_min)
    9093        arguments = parser.parse_args(argv)
    9194
    … …  
    111114                density_min = arguments.points_min
    112115               
    113         gdal_file_contours = os.path.join(output_directory, "contours")
     116        gdal_file_contours = os.path.join(output_directory, 'contours_{}_{}'.format(density_min, density_base))
    114117        print("Cleaning up / Preparing output directory")
    115118        if os.path.exists(output_directory):
    … …  
    201204        lat_metre_max = 111694 # metre / degree
    202205       
    203         circle_radius = math.sqrt(densitiy_base) / math.pi
     206        circle_radius = math.sqrt(density_base) / math.pi
    204207        radius_x = circle_radius / lon_metre_max
    205208        radius_y = circle_radius / lat_metre_max
    … …  
    214217                print("  degree / pixel x and y: {}, {}".format(degree_per_pixel_lon, degree_per_pixel_lat))
    215218                print("  metre / pixel x and y: {}, {}".format(degree_per_pixel_lon * lon_metre_max, degree_per_pixel_lat * lat_metre_max))
    216                 print("  Min density per squaredegree: {}".format(density_min * densitiy_base / lon_metre_max / lat_metre_max))
    217                 print("  Min density per pixel: {}".format(density_min * densitiy_base / (degree_per_pixel_lon * lon_metre_max) / (degree_per_pixel_lat * lat_metre_max)))
     219                print("  Min density per squaredegree: {}".format(density_min * density_base / lon_metre_max / lat_metre_max))
     220                print("  Min density per pixel: {}".format(density_min * density_base / (degree_per_pixel_lon * lon_metre_max) / (degree_per_pixel_lat * lat_metre_max)))
    218221                print("  gridding algorithm {}".format(gridding_algorithm))
    219222       
    … …  
    230233        )
    231234        gdal.Grid(gdal_file, source_xml_file, options = grid_options)
    232         print("  Created raster DEM in memory")
     235        print("  Created raster Grid in memory")
    233236       
    234237        # hillshade
    235         gdal_file_hillshade  = os.path.join(output_directory, "water_depth_hillshade.tif")
     238        gdal_file_hillshade  = '/vsimem/water_depth_hillshade.tif'
    236239        gdal.DEMProcessing(gdal_file_hillshade, gdal_file, "hillshade", options = gdal.DEMProcessingOptions(format = "GTiff", zFactor = -1))
    237         print("  Created hillshade in {}".format(gdal_file_hillshade))
     240        print("  Created hillshade in memory")
    238241       
    239242        # color relief
    240         gdal_file_color_relief = os.path.join(output_directory, "water_depth_color_relief.tif")
     243        gdal_file_color_relief = '/vsimem/water_depth_color_relief.tif'
    241244        gdal_file_color_relief_config = os.path.join(output_directory, "tmp_water_depth_color_relief_config.txt")
    242245        with open(gdal_file_color_relief_config, 'w') as color_config:
    … …  
    244247                        color_config.write("{} {}\n".format(depth, colors[depth]))
    245248        gdal.DEMProcessing(gdal_file_color_relief, gdal_file, "color-relief", options = gdal.DEMProcessingOptions(format = "GTiff", colorFilename = gdal_file_color_relief_config), addAlpha = True)
    246         print("  Created color relief in {}".format(gdal_file_color_relief))
     249        print("  Created color relief in memory")
    247250       
    248251        # slopeshade
     252        gdal_file_slopeshade  = os.path.join(output_directory, "water_depth_slopeshade.tif")
    249253        gdal_file_slopeshade_tmp = "/vsimem/tmp_water_depth_slopeshade.tif"
    250254        gdal_file_slopeshade_config = os.path.join(output_directory, "tmp_water_depth_slopeshade_config.txt")
    … …  
    252256                shade_config.write("0 255 255 255\n")
    253257                shade_config.write("90 0 0 0\n")
     258                shade_config.write("270 0 0 0\n")
     259                shade_config.write("360 255 255 255\n")
    254260        gdal.DEMProcessing(gdal_file_slopeshade_tmp, gdal_file, "slope", options = gdal.DEMProcessingOptions(format = "GTiff"), slopeFormat = 'degree', zeroForFlat = True, addAlpha = True)
    255261        gdal.DEMProcessing(gdal_file_slopeshade, gdal_file_slopeshade_tmp, "color-relief", options = gdal.DEMProcessingOptions(format = "GTiff", colorFilename = gdal_file_slopeshade_config), addAlpha = True)
    … …  
    266272        proj.SetWellKnownGeogCS(input_epsg)
    267273        for depth in colors:
    268                 contour_layer = contour_output.CreateLayer('contour-{}'.format(depth), srs = proj)
     274                contour_layer = contour_output.CreateLayer('contour_{}_{}_{}'.format(density_min, density_base, depth), srs = proj)
    269275                field_definition = ogr.FieldDefn("ID", ogr.OFTInteger)
    270276                contour_layer.CreateField(field_definition)
    … …  
    276282        del contour_output
    277283       
    278         #overlaid = gdal.Warp(gdal_file_slopeshade, [color_relief, hillshade], format='GTiff')
    279         ##gdal.Translate(gdal_file_slopeshade, overlaid, creationOptions=['compress=LZW', 'alpha=yes'])
    280         #gdal.Translate(gdal_file_slopeshade, overlaid, creationOptions=['alpha=yes'])
    281         gdal.Warp('/tmp/gnuboat/test.tif', gdal_file_color_relief, options = gdal.WarpOptions(format = 'GTiff', dstAlpha = True, cutlineDSName = '/tmp/gnuboat/contours/contour-0.shp'))
    282         #gdal.Warp('/tmp/gnuboat/test.tif', gdal_file_color_relief, options = gdal.WarpOptions(format = 'GTiff', dstAlpha = True))
     284        # combining hillshade and color relief
     285       
     286       
     287        # cropping the tifs
     288        #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))
     289
     290        # dstNodata is a workaround for not working addAlpha in color-relief
     291        gdal_final_result = '/tmp/gnuboat/water_depth_{}_{}.tif'.format(density_min, density_base)
     292        gdal_color_relief_alpha_tmp = '/vsimem/tmp_color_relief.tif'
     293        gdal.Warp(gdal_final_result, [gdal_file_color_relief, gdal_file_hillshade], options = gdal.WarpOptions(format = 'GTiff', dstAlpha = False, dstNodata = "255 255 255"))
    283294
    284295        # generate tiles
    285296        tile_zoom_option = "0-19"
    286297        tile_commands = [
    287                 #hillshade
    288                 {"file": gdal_file_hillshade, "title": 'Hillshade', "output": 'hillshade'},
    289                 #color relief
    290                 {"file": gdal_file_color_relief, "title": 'Color relief', "output": 'color_relief'},
    291                 #slopeshade
    292                 {"file": gdal_file_slopeshade, "title": 'Slopeshade', "output": 'slopeshade'}
     298                #combined
     299                {"file": gdal_final_result, "title": 'GNUBoat {} datasets per {} squaremetre'.format(density_min, density_base), "output": 'gnuboat_{}_{}'.format(density_min, density_base)},
    293300        ]
    294301        print("Expanding {} tifs to tiles".format(len(tile_commands)))
Note: See TracChangeset for help on using the changeset viewer.