|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import matplotlib.ticker as ticker |
| 5 | +import numpy as np |
| 6 | +from osgeo import gdal |
| 7 | +from osgeo import osr |
| 8 | + |
| 9 | +# open the BAG file |
| 10 | +bag_path = os.path.join(os.path.dirname(__file__), os.pardir, "data", "sh2007.bag") |
| 11 | +dataset = gdal.Open(bag_path, gdal.GA_ReadOnly) |
| 12 | +if not dataset: |
| 13 | + raise RuntimeError("Issue in opening the BAG file: %s" % bag_path) |
| 14 | + |
| 15 | +# retrieve geospatial information |
| 16 | +projection = dataset.GetProjection() |
| 17 | +srs = osr.SpatialReference(wkt=projection) |
| 18 | +projection_name = srs.GetAttrValue('projcs') |
| 19 | +gt = dataset.GetGeoTransform() |
| 20 | +if not gt: |
| 21 | + raise RuntimeError("Issue in retrieving the geotransfrom") |
| 22 | + |
| 23 | +# retrieve elevation |
| 24 | +elev_band = dataset.GetRasterBand(1) |
| 25 | +elev_nodata = elev_band.GetNoDataValue() |
| 26 | +elev = elev_band.ReadAsArray() |
| 27 | +elev[elev == elev_nodata] = np.nan |
| 28 | +elev = -elev |
| 29 | +bag_extent = [gt[0], gt[0] + elev.shape[1] * gt[1], gt[3] + elev.shape[0] * gt[5], gt[3]] |
| 30 | + |
| 31 | +# plot elevation |
| 32 | +plt.figure(figsize=(15, 10)) # to make the plot bigger |
| 33 | +ax = plt.axes() |
| 34 | +plt.ticklabel_format(useOffset=False) # to avoid the display of an offset for the labels |
| 35 | +ax.xaxis.set_major_locator(ticker.MultipleLocator(100.0)) # to set the distance between labels (x axis) |
| 36 | +ax.yaxis.set_major_locator(ticker.MultipleLocator(100.0)) # to set the distance between labels (y axis) |
| 37 | +img = ax.imshow(elev, origin='upper', extent=bag_extent, cmap='viridis_r') # display the array using the passed extent |
| 38 | +ax.set_aspect('equal') # avoid deformations for the displayed data |
| 39 | +title = "%s\nProjection: %s" % (os.path.basename(bag_path), projection_name) |
| 40 | +plt.title(title) # put the filename and the projection as part of the title |
| 41 | +plt.xlabel("Easting") |
| 42 | +plt.ylabel("Northing") |
| 43 | +cb = plt.colorbar(img) # pass the 'img' object used to create the colorbar |
| 44 | +cb.set_label('Bathymetry [meter]') |
| 45 | +plt.grid() |
| 46 | +plt.show() |
| 47 | + |
| 48 | +# retrieve uncertainty |
| 49 | +unc_band = dataset.GetRasterBand(2) |
| 50 | +unc_nodata = elev_band.GetNoDataValue() |
| 51 | +unc = unc_band.ReadAsArray() |
| 52 | +unc[unc == unc_nodata] = np.nan |
| 53 | +bag_extent = [gt[0], gt[0] + unc.shape[1] * gt[1], gt[3] + unc.shape[0] * gt[5], gt[3]] |
| 54 | + |
| 55 | +# plot uncertainty |
| 56 | +plt.figure(figsize=(15, 10)) |
| 57 | +ax = plt.axes() |
| 58 | +plt.ticklabel_format(useOffset=False) |
| 59 | +ax.xaxis.set_major_locator(ticker.MultipleLocator(100.0)) |
| 60 | +ax.yaxis.set_major_locator(ticker.MultipleLocator(100.0)) |
| 61 | +img = ax.imshow(unc, origin='upper', extent=bag_extent) |
| 62 | +ax.set_aspect('equal') |
| 63 | +title = "%s\nProjection: %s" % (os.path.basename(bag_path), projection_name) |
| 64 | +plt.title(title) # put the filename and the projection as part of the title |
| 65 | +plt.xlabel("Easting") |
| 66 | +plt.ylabel("Northing") |
| 67 | +cb = plt.colorbar(img) |
| 68 | +cb.set_label('Uncertainty [meter]') |
| 69 | +plt.grid() |
| 70 | +plt.show() |
0 commit comments