def site_exposure(raw_aspect, raw_slope, exposure_output):
    """
    Description: calculates 32-bit float site exposure
    Inputs: 'raw_aspect' -- an input raw aspect raster
            'raw_slope' -- an input raster digital elevation model
            'exposure_output' -- an output exposure raster
    Returned Value: Returns a raster dataset on disk
    Preconditions: requires an input aspect and slope raster
    """

    # Import packages
    import arcpy
    from arcpy.sa import Cos
    from arcpy.sa import Divide
    from arcpy.sa import Minus
    from arcpy.sa import Raster
    from arcpy.sa import Times

    # Set overwrite option
    arcpy.env.overwriteOutput = True

    # Calculate cosine of modified aspect
    print('\t\tCalculating cosine of modified aspect...')
    cosine = Cos(Divide(Times(3.142, Minus(Raster(raw_aspect), 180)), 180))

    # Calculate site exposure index and save output
    print('\t\tCalculating site exposure index...')
    out_raster = Times(Raster(raw_slope), cosine)
    out_raster.save(exposure_output)
Exemple #2
0
def main(bpi_raster=None, out_raster=None):
    arcpy.env.compression = "LZW"
    try:
        # Get raster properties
        message = ("Calculating properties of the Bathymetric "
                   "Position Index (BPI) raster...")
        utils.msg(message)
        utils.msg("  input raster: {}\n   output: {}".format(
            bpi_raster, out_raster))
        # convert to a path
        desc = arcpy.Describe(bpi_raster)
        bpi_raster_path = desc.catalogPath

        bpi_mean = utils.raster_properties(bpi_raster_path, "MEAN")
        utils.msg("BPI raster mean: {}.".format(bpi_mean))
        bpi_std_dev = utils.raster_properties(bpi_raster_path, "STD")
        utils.msg("BPI raster standard deviation: {}.".format(bpi_std_dev))

        # Create the standardized Bathymetric Position Index (BPI) raster
        std_msg = "Standardizing the Bathymetric Position Index (BPI) raster..."
        utils.msg(std_msg)
        arcpy.env.rasterStatistics = "STATISTICS"
        outRaster = Int(Plus(Times(Divide(
            Minus(bpi_raster_path, bpi_mean), bpi_std_dev), 100), 0.5))
        out_raster = utils.validate_path(out_raster)
        arcpy.CopyRaster_management(outRaster, out_raster)

    except Exception as e:
        utils.msg(e, mtype='error')
def compound_topographic(elevation_input, flow_accumulation, raw_slope,
                         cti_output):
    """
    Description: calculates 32-bit float compound topographic index
    Inputs: 'elevation_input' -- an input raster digital elevation model
            'flow_accumulation' -- an input flow accumulation raster with the same spatial reference as the elevation raster
            'raw_slope' -- an input raw slope raster in degrees with the same spatial reference as the elevation raster
            'cti_output' -- an output compound topographic index raster
    Returned Value: Returns a raster dataset on disk
    Preconditions: requires input elevation, flow accumulation, and raw slope raster
    """

    # Import packages
    import arcpy
    from arcpy.sa import Con
    from arcpy.sa import Divide
    from arcpy.sa import Ln
    from arcpy.sa import Plus
    from arcpy.sa import Raster
    from arcpy.sa import Times
    from arcpy.sa import Tan

    # Set overwrite option
    arcpy.env.overwriteOutput = True

    # Get spatial properties for the input elevation raster
    description = arcpy.Describe(elevation_input)
    cell_size = description.meanCellHeight

    # Convert degree slope to radian slope
    print('\t\tConverting degree slope to radians...')
    slope_radian = Divide(Times(Raster(raw_slope), 1.570796), 90)

    # Calculate slope tangent
    print('\t\tCalculating slope tangent...')
    slope_tangent = Con(slope_radian > 0, Tan(slope_radian), 0.001)

    # Correct flow accumulation
    print('\t\tModifying flow accumulation...')
    accumulation_corrected = Times(Plus(Raster(flow_accumulation), 1),
                                   cell_size)

    # Calculate compound topographic index as natural log of corrected flow accumulation divided by slope tangent
    print('\t\tCalculating compound topographic index...')
    out_raster = Ln(Divide(accumulation_corrected, slope_tangent))
    out_raster.save(cti_output)
Exemple #4
0
def integrated_moisture(elevation_input, flow_accumulation, zFactor,
                        imi_output):
    """
    Description: calculates 32-bit float integrated moisture index
    Inputs: elevation_input' -- an input raster digital elevation model
            'flow_accumulation' -- an input flow accumulation raster with the same spatial reference as the elevation raster
            'zFactor' -- a unit scaling factor for calculations that involve comparisons of xy to z
            'imi_output' -- an output compound topographic index raster
    Returned Value: Returns a raster dataset on disk
    Preconditions: requires input elevation,
    """

    # Import packages
    import arcpy
    from arcpy.sa import Curvature
    from arcpy.sa import Hillshade
    from arcpy.sa import Plus
    from arcpy.sa import Times
    from arcpy.sa import Raster

    # Set overwrite option
    arcpy.env.overwriteOutput = True

    # Adjust flow accumulation
    print('\t\tScaling flow accumulation...')
    adjusted_accumulation = Times(Raster(flow_accumulation), 0.35)

    # Calculate and adjust curvature
    print('\t\tCalculating curvature...')
    curvature = Curvature(Raster(elevation_input), zFactor)
    adjusted_curvature = Times(curvature, 0.15)

    # Calculate and adjust hillshade
    print('\t\tCalculating hillshade...')
    hillshade = Hillshade(Raster(elevation_input), "#", "#", "#", zFactor)
    adjusted_hillshade = Times(hillshade, 0.5)

    # Calculate integrated moisture index
    out_raster = Plus(Plus(adjusted_accumulation, adjusted_curvature),
                      adjusted_hillshade)
    out_raster.save(imi_output)
Exemple #5
0
def slopeaspect(path):
    import arcpy, os, re
    from arcpy.sa import Slope,Times,Aspect,Int
    from arcpy import env
    arcpy.env.workspace = path

    print('creating slope and aspect raster map')
    arcpy.CheckOutExtension("Spatial")  # activating spetial analyst module

    # Transforming altitude, soil type, landuse and slope raster maps to polygon
    # cellsize = arcpy.GetRasterProperties_management("altitude.tif","CELLSIZEX")  #Extracting the raster cell size
    # cellsize1 = float(cellsize.getOutput(0))
    # cellarea = cellsize1*cellsize1/1000000.0

    slope1 = Slope("altitude.tif", 'DEGREE')  # SLOPE IN DEGREE
    slope1.save("slope_in_deg.tif")
    const = 100.0
    OutRas = Times("slope_in_deg.tif", 100.0)
    intslope = Int(OutRas)
    intslope.save("times.tif")
    #
    aspect1 = Aspect("altitude.tif")  # ATTENTION: Aspect in Arcgis is calculated clockwise so East is 90. in Raven's manual Aspect is assumed to be counterclockwise i.e., west 90
    aspect1.save("aspect")
    print('done!')
Exemple #6
0
def main(in_raster=None, out_raster=None, acr_correction=True, area_raster=None):
    """
    A calculation of rugosity, based on the difference between surface
    area and planar area, as described in Jenness, J. 2002. Surface Areas
    and Ratios from Elevation Grid (surfgrids.avx) extension for ArcView 3.x,
    v. 1.2. Jenness Enterprises.

    NOTE: the VRM method implemented in ruggeddness is generally considered
          superior to this method.
    """

    # sanitize acr input
    if isinstance(acr_correction, str) and acr_correction.lower() == 'false':
        acr_correction = False

    w = utils.Workspace()
    if w.exists:
        out_workspace = w.path
    else:
        out_workspace = os.path.dirname(out_raster)
    # make sure workspace exists
    utils.workspace_exists(out_workspace)

    utils.msg("Set scratch workspace to {}...".format(out_workspace))

    # force temporary stats to be computed in our output workspace
    arcpy.env.scratchWorkspace = out_workspace
    arcpy.env.workspace = out_workspace
    pyramid_orig = arcpy.env.pyramid
    arcpy.env.pyramid = "NONE"
    # TODO: currently set to automatically overwrite, expose this as option
    arcpy.env.overwriteOutput = True

    bathy = Raster(in_raster)
    # get the cell size of the input raster; use same calculation as was
    # performed in BTM v1: (mean_x + mean_y) / 2
    cell_size = (bathy.meanCellWidth + bathy.meanCellHeight) / 2.0
    corner_dist = math.sqrt(2 * cell_size ** 2)
    flat_area = cell_size ** 2
    utils.msg("Cell size: {}\nFlat area: {}".format(cell_size, flat_area))

    try:
        # Create a set of shifted grids, with offets n from the origin X:

        #        8 | 7 | 6
        #        --|---|---
        #        5 | X | 4
        #        --|---|---
        #        3 | 2 | 1

        positions = [(1, -1), (0, -1), (-1, -1),
                     (1,  0),          (-1,  0),
                     (1,  1), (0,  1), (-1,  1)]

        corners = (1, 3, 6, 8)      # dist * sqrt(2), as set in corner_dist
        orthogonals = (2, 4, 5, 7)  # von Neumann neighbors, straight dist
        shift_rasts = [None]        # offset to align numbers
        temp_rasts = []

        for (n, pos) in enumerate(positions, start=1):
            utils.msg("Creating Shift Grid {} of 8...".format(n))
            # scale shift grid by cell size
            (x_shift, y_shift) = map(lambda n: n * cell_size, pos)

            # set explicit path on shift rasters, otherwise suffer
            # inexplicable 999999 errors.
            shift_out = os.path.join(out_workspace, "shift_{}.tif".format(n))
            shift_out = utils.validate_path(shift_out)
            temp_rasts.append(shift_out)
            arcpy.Shift_management(bathy, shift_out, x_shift, y_shift)
            shift_rasts.append(arcpy.sa.Raster(shift_out))

        edge_rasts = [None]
        # calculate triangle length grids

        # edges 1-8: pairs of bathy:shift[n]
        for (n, shift) in enumerate(shift_rasts[1:], start=1):
            utils.msg("Calculating Triangle Edge {} of 16...".format(n))
            # adjust for corners being sqrt(2) from center
            if n in corners:
                dist = corner_dist
            else:
                dist = cell_size
            edge_out = os.path.join(out_workspace, "edge_{}.tif".format(n))
            edge_out = utils.validate_path(edge_out)
            temp_rasts.append(edge_out)
            edge = compute_edge(bathy, shift, dist)
            edge.save(edge_out)
            edge_rasts.append(arcpy.sa.Raster(edge_out))

        # edges 9-16: pairs of adjacent shift grids [see layout above]
        # in BTM_v1, these are labeled A-H
        adjacent_shift = [(1, 2), (2, 3), (1, 4), (3, 5),
                          (6, 4), (5, 8), (6, 7), (7, 8)]
        for (n, pair) in enumerate(adjacent_shift, start=9):
            utils.msg("Calculating Triangle Edge {} of 16...".format(n))
            # the two shift rasters for this iteration
            (i, j) = pair
            edge_out = os.path.join(out_workspace, "edge_{}.tif".format(n))
            edge_out = utils.validate_path(edge_out)
            temp_rasts.append(edge_out)
            edge = compute_edge(shift_rasts[i], shift_rasts[j], cell_size)
            edge.save(edge_out)
            edge_rasts.append(arcpy.sa.Raster(edge_out))

        # areas of each triangle
        areas = []
        for (n, pair) in enumerate(adjacent_shift, start=1):
            utils.msg("Calculating Triangle Area {} of 8...".format(n))
            # the two shift rasters; n has the third side
            (i, j) = pair
            area_out = os.path.join(out_workspace, "area_{}.tif".format(n))
            area_out = utils.validate_path(area_out)
            temp_rasts.append(area_out)

            area = triangle_area(edge_rasts[i], edge_rasts[j], edge_rasts[n+8])
            area.save(area_out)
            areas.append(arcpy.sa.Raster(area_out))

        utils.msg("Summing Triangle Area...")
        arcpy.env.pyramid = pyramid_orig
        arcpy.env.rasterStatistics = "STATISTICS"
        arcpy.env.compression = "LZW"
        total_area = (areas[0] + areas[1] + areas[2] + areas[3] +
                      areas[4] + areas[5] + areas[6] + areas[7])
        if area_raster:
            save_msg = "Saving Surface Area Raster to " + \
                "{}.".format(area_raster)
            utils.msg(save_msg)
            arcpy.CopyRaster_management(total_area, area_raster)

        if not acr_correction:
            utils.msg("Calculating ratio with uncorrected planar area.")
            area_ratio = total_area / cell_size**2
        else:
            utils.msg("Calculating ratio with slope-corrected planar area.")
            slope_raster = arcpy.sa.Slope(in_raster, "DEGREE", "1")
            planar_area = Divide(float(cell_size**2),
                                 Cos(Times(slope_raster, 0.01745)))
            area_ratio = Divide(total_area, planar_area)

        out_raster = utils.validate_path(out_raster)
        save_msg = "Saving Surface Area to Planar Area ratio to " + \
            "{}.".format(out_raster)
        utils.msg(save_msg)
        arcpy.CopyRaster_management(area_ratio, out_raster)

    except Exception as e:
        utils.msg(e, mtype='error')

    try:
        # Delete all intermediate raster data sets
        utils.msg("Deleting intermediate data...")
        for path in temp_rasts:
            arcpy.Delete_management(path)

    except Exception as e:
        utils.msg(e, mtype='error')