Exemplo n.º 1
0
    def _render_background(self, fullpath_to_raster):
        """
        Renders the background image that 
        will form the drape plot, e.g. a hillshade
        """
        if self._backgroundtype == "Hillshade":
            self.Hillshade = LSDP.Hillshade(self.fullpath_to_raster)
            self.colourmap = "gray"

        elif self._backgroundtype == "Terrain":
            self.Hillshade = LSDP.ReadRasterArrayBlocks(
                self.fullpath_to_raster)
            self.colourmap = LSDP.colours.UsefulColourmaps.niceterrain
            #self.colourmap = LSDP.colours.UsefulColourmaps.darkearth
            #self.colourmap = "terrain"
        else:
            print ("That background style is not yet supported. Currently only " \
                   " 'Hillshade' and 'Terrain' are supported.")
def MultiDrapeMaps(DataDir, ElevationRaster, DrapeRasterWild, cmap, drape_min_threshold=None, drape_max=None):
    """
    Plots flood extents from water depth rasters
    draped over the catchment elevation raster
    in a series of subplots
    
    Takes a wildcard for the drapes
    Expexts a fixed elevation raster, but this could be
    modified in future.
    
    Thought: consider, if plotting multiple datasets, how you
    are going to deal with min a max values in the colur range.
    imshow will automatically set vmin and vmax and stretch the colour bar 
    over this - which can be visually misleading. Ideally, you
    want to have the same colour map used for *all* subplots, and 
    this is not default behaviour.
    """
    f, ax_arr = plt.subplots(2, 2, figsize=(10, 5), sharex=True, sharey=True)
    ax_arr = ax_arr.ravel()
    
    FPFiles = sorted(glob.glob(DataDirectory+DrapeRasterWild), key=str)
    n_files = len(FPFiles)
    print("Number of files = ", n_files)
    
    elev_raster_file = DataDir + ElevationRaster
    
    hillshade = LSDP.Hillshade(elev_raster_file)
    #hillshade_array = LSDP.ReadRasterArrayBlocks(elev_raster_file)
    
    # now get the extent
    extent_raster = LSDP.GetRasterExtent(elev_raster_file)
    
    x_min = extent_raster[0]
    x_max = extent_raster[1]
    y_min = extent_raster[2]
    y_max = extent_raster[3]

    # now get the tick marks    
    n_target_tics = 5
    xlocs,ylocs,new_x_labels,new_y_labels = LSDP.GetTicksForUTM(elev_raster_file,x_max,x_min,y_max,y_min,n_target_tics)  

    print("xmax: " + str(x_max))
    print("xmin: " + str(x_min))
    print("ymax: " + str(y_max))
    print("ymin: " + str(y_min))
    
    """
    Find the maximum water depth in all rasters.
    You need this to normalize the colourscale accross
    all plots when teh imshow is done later.
    """

    try:
        print("Calculating max drape raster value by scanning rasters...")
        max_water_depth = findmaxval_multirasters(FPFiles)
        drape_max = max_water_depth
        
    except:
        print("Something went wrong trying to obtain the max value in \
                your drape raster file list.")
    finally:
        print("The drape(s) max value is set to: ", drape_max) 
    
    
    for i in range(n_files):
        
        print("The floodplain file name is: ", FPFiles[i])
        FP_raster = LSDP.ReadRasterArrayBlocks(FPFiles[i])
        #FP_raster = np.ma.masked_where(FP_raster <= 0, FP_raster)
        
        filename = os.path.basename(FPFiles[i])
        title = mplext.labels.make_line_label(filename)
        print(title)
        
        low_values_index = FP_raster < drape_min_threshold
        FP_raster[low_values_index] = np.nan
        
        im = ax_arr[i].imshow(hillshade, "gray", extent=extent_raster, interpolation="nearest")
        """
        Now we can set vmax to be the maximum water depth we calcualted earlier, making our separate
        subplots all have the same colourscale
        """
        im = ax_arr[i].imshow(FP_raster, cmap, extent=extent_raster, 
                                alpha=1.0, interpolation="none", 
                                vmin=drape_min_threshold, 
                                vmax=drape_max)
        ax_arr[i].set_title(title)
 
    f.subplots_adjust(right=0.8)
    cax = f.add_axes([0.9, 0.1, 0.03, 0.8])
    
    cbar = f.colorbar(im, cax=cax) 
    cbar.set_label("Water depth (m)")
    #cbar.set_ticks(np.linspace(0, 8, 8))
    #cbar = mplext.colours.colorbar_index(f, cax, 8, cmap, 
    #                                     drape_min_threshold, drape_max)
    cbar.set_label("Water depth (m)")
    #tick_locator = ticker.MaxNLocator(nbins=8)
    #cbar.locator = tick_locator
    #cbar.update_ticks()
    
    f.text(0.5, 0.04, 'Easting (m)', ha='center')
    f.text(0.04, 0.5, 'Northing (m)', va='center', rotation='vertical')