logger.setLevel(logging.NOTSET) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # change to WARNING to reduce verbosity, DEBUG for high verbosity ch_formatter = logging.Formatter('%(levelname)-9s %(name)s.%(funcName)s:%(lineno)d > %(message)s') ch.setFormatter(ch_formatter) logger.addHandler(ch) from hydroffice.bag import BAGFile from hydroffice.bag import BAGError from hydroffice.bag.helper import Helper bag_file = os.path.join(Helper.samples_folder(), "bdb_00.bag") if os.path.exists(bag_file): print("- file_bag_0: %s" % bag_file) bag = BAGFile(bag_file) bag_meta = bag.populate_metadata() print(bag_meta) bag_uncertainty = bag.uncertainty(mask_nan=True) print(type(bag.elevation(mask_nan=True)), bag.elevation(mask_nan=True).shape, bag.elevation(mask_nan=True).dtype) from hydroffice.bag.uncertainty import Uncertainty2Gdal Uncertainty2Gdal(bag_uncertainty=bag_uncertainty, bag_meta=bag_meta, fmt="ascii", epsg=4326) Uncertainty2Gdal(bag_uncertainty=bag_uncertainty, bag_meta=bag_meta, fmt="geotiff", epsg=4326) Uncertainty2Gdal(bag_uncertainty=bag_uncertainty, bag_meta=bag_meta, fmt="xyz", epsg=4326)
# - open a BAG file # - read the whole elevation and uncertainty layers # - read a selected range of rows for the elevation and uncertainty layers file_bag_0 = os.path.join(Helper.samples_folder(), "bdb_00.bag") if os.path.exists(file_bag_0): logger.debug("- file_bag_0: %s" % file_bag_0) # - open a file bag_0 = BAGFile(file_bag_0) logger.debug("\n%s\n" % bag_0) # - get elevation shape logger.info("elevation shape: %s" % (bag_0.elevation_shape(), )) # - read the full elevation full_elevation = bag_0.elevation(mask_nan=True) logger.info("elevation array:\n type: %s\n shape: %s\n dtype: %s" % (type(full_elevation), full_elevation.shape, full_elevation.dtype)) ax = plt.contourf(full_elevation) plt.colorbar(ax) plt.show() # - read the first 10 rows of the elevation layers selection_slice = slice(0, 10) sliced_elevation = bag_0.elevation(mask_nan=True, row_range=selection_slice) logger.info("sliced elevation array:\n type: %s\n shape: %s\n dtype: %s" % (type(sliced_elevation), sliced_elevation.shape, sliced_elevation.dtype)) ax = plt.contourf(sliced_elevation) plt.colorbar(ax) plt.show() # - get uncertainty shape
ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # change to WARNING to reduce verbosity, DEBUG for high verbosity ch_formatter = logging.Formatter('%(levelname)-9s %(name)s.%(funcName)s:%(lineno)d > %(message)s') ch.setFormatter(ch_formatter) logger.addHandler(ch) from hydroffice.bag import BAGFile from hydroffice.bag import BAGError from hydroffice.bag.helper import Helper bag_file = os.path.join(Helper.samples_folder(), "bdb_00.bag") if os.path.exists(bag_file): print("- file_bag_0: %s" % bag_file) bag = BAGFile(bag_file) bag_meta = bag.populate_metadata() print(bag_meta) print("has elevation? %s" % bag.has_elevation()) bag_elevation = bag.elevation(mask_nan=False) print(type(bag.elevation()), bag.elevation().shape, bag.elevation().dtype) from hydroffice.bag.elevation import Elevation2Gdal Elevation2Gdal(bag_elevation=bag_elevation, bag_meta=bag_meta, fmt="ascii") Elevation2Gdal(bag_elevation=bag_elevation, bag_meta=bag_meta, fmt="geotiff") Elevation2Gdal(bag_elevation=bag_elevation, bag_meta=bag_meta, fmt="xyz")
def main(): logger = logging.getLogger() logger.setLevel(logging.NOTSET) import argparse from hydroffice.bag import BAGFile, is_bag, __version__ app_name = "bag_elevation" app_info = "Extraction of elevation layer from an OpenNS BAG file, using hydroffice.bag r%s" % __version__ formats = ["ascii", "geotiff", "xyz"] parser = argparse.ArgumentParser(prog=app_name, description=app_info) parser.add_argument("bag_file", type=str, help="a valid BAG file from which to extract metadata") parser.add_argument( "-f", "--format", help="one of the available file format: " + ", ".join(formats), choices=formats, default="geotiff", metavar="", ) parser.add_argument("-o", "--output", help="the output file", type=str) parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") args = parser.parse_args() if args.verbose: print("> verbosity: ON") ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # change to WARNING to reduce verbosity, DEBUG for high verbosity ch_formatter = logging.Formatter("%(levelname)-9s %(name)s.%(funcName)s:%(lineno)d > %(message)s") ch.setFormatter(ch_formatter) logger.addHandler(ch) if args.verbose: print("> input: %s" % args.bag_file) if args.output: args.output = os.path.abspath(args.output) print("> output: %s" % args.output) else: args.output = None print("> output: [default]") print("> format: %s" % args.format) if not os.path.exists(args.bag_file): parser.exit(1, "ERROR: the input valid does not exist: %s" % args.bag_file) if not is_bag(args.bag_file): parser.exit(1, "ERROR: the input valid does not seem a BAG file: %s" % args.bag_file) bf = BAGFile(args.bag_file) bag_meta = None try: bag_meta = bf.populate_metadata() except Exception as e: parser.exit(1, "ERROR: issue in metadata population: %s" % e) bag_elevation = None try: bag_elevation = bf.elevation(mask_nan=False) except Exception as e: parser.exit(1, "ERROR: issue in elevation population: %s" % e) try: from hydroffice.bag.elevation import Elevation2Gdal Elevation2Gdal(bag_elevation=bag_elevation, bag_meta=bag_meta, fmt=args.format, out_file=args.output) except Exception as e: parser.exit(1, "ERROR: issue in output creation: %s" % e) if args.verbose: print("> DONE")