Example #1
0
def DinfFlowDir():
    import argparse

    parser = argparse.ArgumentParser(
        description='Assigns a flow direction based on the D-infinity flow'
        ' method using the steepest slope of a triangular facet (Tarboton, '
        '1997, "A New Method for the Determination of Flow Directions and '
        'Contributing Areas in Grid Digital Elevation Models," Water Resources'
        ' Research, 33(2): 309-319).')
    parser.add_argument('Input_Pit_Filled_Elevation',
                        help='The input pit-filled elevation file in '
                            'geotiff format.',
                        )
    parser.add_argument('Input_Number_of_Chunks',
                        help="The approximate number of chunks that the input "
                        'file will be divided into for processing (potentially'
                        ' on multiple processors).', type=int, default=1, nargs='?')
    parser.add_argument('Output_D_Infinity_Flow_Direction',
                        help='Output filename for the flow direction. Default'
                        ' value = ang.tif .', default='ang.tif', nargs='?')
    parser.add_argument('Output_D_Infinity_Slope',
                        help='Output filename for the flow direction. Default'
                        ' value = mag.tif .', default='mag.tif', nargs='?')
    args = parser.parse_args()
    fn = args.Input_Pit_Filled_Elevation
    n_chunks = args.Input_Number_of_Chunks
    fn_ang = args.Output_D_Infinity_Flow_Direction
    fn_mag = args.Output_D_Infinity_Slope

    from pydem.dem_processing import DEMProcessor
    dem_proc = DEMProcessor(fn)
    shape = dem_proc.data.shape
    chunk_size = max(shape[0],  shape[1]) / n_chunks
    dem_proc.chunk_size_slp_dir = chunk_size
    dem_proc.calc_slopes_directions()
    dem_proc.save_array(dem_proc.mag, fn_mag, as_int=False)
    dem_proc.save_array(dem_proc.direction, fn_ang, as_int=False)
Example #2
0
if __name__ == "__main__":
    from pydem.test_pydem import get_test_data
    from pydem.dem_processing import DEMProcessor
    from pydem.test_pydem import make_file_names


    import sys
    if len(sys.argv) <= 1:
        testnum = 30  # Default test number

    NN = 64  # Resolution of tile
    
    # Get test data and create DEMProcessor
    elev, ang, test_data = get_test_data(testnum, NN)
    filename = make_file_names(testnum, NN)['fel']
    dem_proc = DEMProcessor(filename, dx_dy_from_file=False)
    
    # Calculate magnitude and direction of slopes
    mag, direction = dem_proc.calc_slopes_directions()

    # Calculate UCA/TWI on entire tile
    area1 = dem_proc.calc_uca(0)
    twi1 = dem_proc.calc_twi()

    # Calculate UCA/TWI on chunks
    dem_proc.resolve_edges = True  # If false, won't do edge resolution
    dem_proc.chunk_size_uca = 9  # Chunk size
    dem_proc.chunk_overlap_uca = 3  #Overlap between chunks
    area2 = dem_proc.calc_uca(0)  
    twi2 = dem_proc.calc_twi()
Example #3
0
    if testnum >= 28:
        filename = make_file_names(testnum, NN)["elev"]
    else:
        filename = make_file_names(testnum, NN)["fel"]

    elev, ang, test_data = get_test_data(testnum, NN)

    # Make the filenames of the results
    filename2 = make_file_names(testnum, NN)["uca"]
    if os.path.exists(filename2):
        uca_file = GdalReader(file_name=filename2)
        uca, = uca_file.raster_layers

    # Make the pydem Processor object and process the elevation
    dem_proc = DEMProcessor(filename, dx_dy_from_file=dx_dy_from_file)
    mag, direction = dem_proc.calc_slopes_directions()
    area = dem_proc.calc_uca(0)

    twi = dem_proc.calc_twi()
    #
    # Now process using tauDEM
    os.chdir("testtiff")
    try:
        os.remove("test_sca.tif")
        os.remove("test_ang.tif")
        os.remove("test_slp.tif")
    except:
        pass
    cmd = 'dinfflowdir -fel "%s" -ang "%s" -slp "%s"' % (os.path.split(filename)[-1], "test_ang.tif", "test_slp.tif")
    taudem._run(cmd)
Example #4
0
if __name__ == "__main__":
    from pydem.test_pydem import get_test_data
    from pydem.dem_processing import DEMProcessor
    from pydem.test_pydem import make_file_names

    import sys, os
    if len(sys.argv) <= 1:
        testnum = 13  # Default test number

    NN = 64  # Resolution of tile

    # Get test data and create DEMProcessor
    elev, ang, test_data = get_test_data(testnum, NN)
    elev = elev.raster_data
    
    dem_proc1 = DEMProcessor(elev)
    # Calculate TWI
    twi1 = dem_proc1.calc_twi()
    
    # Stretch the latitude direction
    lats = [0, 2]  # degrees
    lons = [0, 1]  # degrees
    dem_proc2 = DEMProcessor((elev, lats, lons))
    twi2 = dem_proc2.calc_twi()

    # plot results
    import matplotlib.pyplot as plt
    plt.matshow(twi1); plt.colorbar()
    plt.title('TWI calculated from Array, dx/dy determined from array shape')
    plt.matshow(dem_proc1.data); plt.colorbar()
    plt.title('Elevation from Array (after fixing flats)')
Example #5
0
"""
if __name__ == "__main__":
    from pydem.test_pydem import get_test_data
    from pydem.dem_processing import DEMProcessor
    from pydem.test_pydem import make_file_names

    import sys
    if len(sys.argv) <= 1:
        testnum = 30  # Default test number

    NN = 64  # Resolution of tile

    # Get test data and create DEMProcessor
    elev, ang, test_data = get_test_data(testnum, NN)
    filename = make_file_names(testnum, NN)['fel']
    dem_proc = DEMProcessor(filename, dx_dy_from_file=False)

    # Calculate magnitude and direction of slopes
    mag, direction = dem_proc.calc_slopes_directions()

    # Calculate UCA/TWI on entire tile
    area1 = dem_proc.calc_uca(0)
    twi1 = dem_proc.calc_twi()

    # Calculate UCA/TWI on chunks
    dem_proc.resolve_edges = True  # If false, won't do edge resolution
    dem_proc.chunk_size_uca = 9  # Chunk size
    dem_proc.chunk_overlap_uca = 3  #Overlap between chunks
    area2 = dem_proc.calc_uca(0)
    twi2 = dem_proc.calc_twi()
Example #6
0
    if testnum >= 28:
        filename = make_file_names(testnum, NN)['elev']
    else:
        filename = make_file_names(testnum, NN)['fel']

    elev, ang, test_data = get_test_data(testnum, NN)

    # Make the filenames of the results
    filename2 = make_file_names(testnum, NN)['uca']
    if os.path.exists(filename2):
        uca_file = GdalReader(file_name=filename2)
        uca, = uca_file.raster_layers

    # Make the pydem Processor object and process the elevation
    dem_proc = DEMProcessor(filename, dx_dy_from_file=dx_dy_from_file)
    mag, direction = dem_proc.calc_slopes_directions()
    area = dem_proc.calc_uca(0)

    twi = dem_proc.calc_twi()
    #
    # Now process using tauDEM
    os.chdir('testtiff')
    try:
        os.remove('test_sca.tif')
        os.remove('test_ang.tif')
        os.remove('test_slp.tif')
    except:
        pass
    cmd = ('dinfflowdir -fel "%s" -ang "%s" -slp "%s"' %
           (os.path.split(filename)[-1], 'test_ang.tif', 'test_slp.tif'))
Example #7
0
sys.setrecursionlimit(1000000)

# Ori
tiffile = r'C:\Users\thril\Desktop\ETOPO1DEM_Bedrock_gridRegis\ETOPO1_Bed_g_geotiff.tif'
Oritif = gdal.Open(tiffile)
if Oritif is None:
    print('cannot open tif')
    sys.exit(1)
OritifPro = Oritif.GetProjection()
OritifTra = Oritif.GetGeoTransform()
tifband = Oritif.GetRasterBand(1)
tifData = tifband.ReadAsArray(0, 0, Oritif.RasterXSize, Oritif.RasterYSize)
print('over in data')

# pyDEM
dem_proc = DEMProcessor(tiffile)
dem_proc.fill_flats = False
print('begin cal TWI')
twi = dem_proc.calc_twi()
cols = np.size(twi, 0)
rows = np.size(twi, 1)
print('over pyDEM process')

# out
imgdriver = gdal.GetDriverByName('GTiff')
resultPath = 'C:\\Users\\thril\\Desktop\\ETOPO1DEM_Bedrock_gridRegis\\ETOPO1_TWI.tif'
resultds = imgdriver.Create(resultPath, cols, rows, 1, twi.DataType)
resultds.SetGeoTransform(OritifTra)
resultds.SetProjection(OritifPro)
resultds.GetRasterBand(1).WriteArray(twi)
resultds = None
Example #8
0
def TWIDinf():
    import argparse

    parser = argparse.ArgumentParser(
        description='Calculates a grid of topographic wetness index '
        ' which is the log_e(uca / mag), that is, the natural log of the ratio'
        ' of contributing area per unit contour length and the magnitude of '
        ' the slope. Note, this function takes the elevation '
        'as an input, and it calculates the '
        'slope, direction, and contributing area as intermediate steps.')
    parser.add_argument('Input_Pit_Filled_Elevation',
                        help='The input pit-filled elevation file in '
                            'geotiff format.',
                        )
    parser.add_argument('Input_Number_of_Chunks',
                        help="The approximate number of chunks that the input "
                        'file will be divided into for processing (potentially'
                        ' on multiple processors).', type=int, default=1,
                        nargs='?')
    parser.add_argument('Output_D_Infinity_TWI',
                        help='Output filename for the topographic wetness '
                        'index. Default value = twi.tif .', default='twi.tif',
                        nargs='?')
    parser.add_argument('--save-all', '--sa',
                        help='If set, will save all intermediate files as well.',
                        action='store_true')
    args = parser.parse_args()
    fn = args.Input_Pit_Filled_Elevation
    n_chunks = args.Input_Number_of_Chunks
    fn_twi = args.Output_D_Infinity_TWI
    sa = args.save_all

    from pydem.dem_processing import DEMProcessor
    dem_proc = DEMProcessor(fn)
    shape = dem_proc.data.shape
    chunk_size = max(shape[0],  shape[1]) / n_chunks
    dem_proc.chunk_size_slp_dir = chunk_size
    dem_proc.chunk_size_uca = chunk_size
    dem_proc.calc_slopes_directions()
    dem_proc.calc_uca()
    if sa:
        dem_proc.save_array(dem_proc.mag, 'mag.tif', as_int=False)
        dem_proc.save_array(dem_proc.direction, 'ang.tif', as_int=False)
        dem_proc.save_array(dem_proc.uca, 'uca.tif', as_int=False)
    dem_proc.calc_twi()
    dem_proc.save_array(dem_proc.twi, fn_twi, as_int=True)
Example #9
0
def AreaDinf():
    import argparse

    parser = argparse.ArgumentParser(
        description='Calculates a grid of specific catchment area which is the'
        ' contributing area per unit contour length using the multiple flow '
        'direction D-infinity approach. Note, this is different from the '
        'equivalent tauDEM function, in that it takes the elevation '
        '(not the flow direction) as an input, and it calculates the '
        'slope and direction as intermediate steps.')
    parser.add_argument('Input_Pit_Filled_Elevation',
                        help='The input pit-filled elevation file in '
                            'geotiff format.',
                        )
    parser.add_argument('Input_Number_of_Chunks',
                        help="The approximate number of chunks that the input "
                        'file will be divided into for processing (potentially'
                        ' on multiple processors).', type=int, default=1,
                        nargs='?')
    parser.add_argument('Output_D_Infinity_Specific_Catchment_Area',
                        help='Output filename for the flow direction. Default'
                        ' value = uca.tif .', default='uca.tif', nargs='?')
    parser.add_argument('--save-all', '--sa',
                        help='If set, will save all intermediate files as well.',
                        action='store_true')
    args = parser.parse_args()
    fn = args.Input_Pit_Filled_Elevation
    n_chunks = args.Input_Number_of_Chunks
    fn_uca = args.Output_D_Infinity_Specific_Catchment_Area
    sa = args.save_all

    from pydem.dem_processing import DEMProcessor
    dem_proc = DEMProcessor(fn)
    shape = dem_proc.data.shape
    chunk_size = max(shape[0],  shape[1]) / n_chunks
    dem_proc.chunk_size_slp_dir = chunk_size
    dem_proc.chunk_size_uca = chunk_size
    dem_proc.calc_slopes_directions()
    if sa:
        dem_proc.save_array(dem_proc.mag, 'mag.tif', as_int=False)
        dem_proc.save_array(dem_proc.direction, 'ang.tif', as_int=False)
    dem_proc.calc_uca()
    dem_proc.save_array(dem_proc.uca, fn_uca, as_int=False)