Ejemplo n.º 1
0
def raster_average_mk2(rasterobject_list, outras):
    # this function improves the previous version in that no value data is considered
    from arcpy.sa import Con, SetNull, CellStatistics

    n = len(rasterobject_list)

    # get mask
    rastermask_list = list()
    for each in rasterobject_list:
        eachmask = Con(each > 32760, 1, 0)
        rastermask_list.append(eachmask)

    sum_mask = CellStatistics(rastermask_list, "SUM")

    # flip values and set null for mask
    # only do this for pixels having more than 6 NoData
    ##    sum_mask = Con(sum_mask>0, None, 1)
    sum_mask = SetNull(sum_mask > 6, 1)

    # it doesn't honor mask
    outras_mask = r"C:\mask_temp.tif"
    sum_mask.save(outras_mask)

    # average, only operate on those valid values
    arcpy.env.mask = outras_mask

    # average
    avg_raster = CellStatistics(rasterobject_list, "MEAN", "DATA")
    avg_raster.save(outras)

    # clear mask
    arcpy.env.mask = None
Ejemplo n.º 2
0
def night_image_mean():
    """
    从多年的night images生成一张均值image的栅格文件
    """
    env.workspace = "G:\yang\ozone_process/night_images"
    out_raster_name = "night_mean.tif"
    if not os.path.exists(os.path.join(env.workspace, out_raster_name)):
        night_images = get_night_images()
        out_rasters = night_images_extract(night_images)
        out_raster = CellStatistics(out_rasters, "MEAN", "DATA")
        out_raster.save(out_raster_name)
    return os.path.join(env.workspace, out_raster_name)
Ejemplo n.º 3
0
def evi_year_mean_stat():
    years = YEARS
    for year in years:
        evi_raster_paths = glob.glob(
            os.path.join(EVI_RASTER_SUBDIR,
                         "US{}*.250m_16_days_EVI.tif".format(str(year))))
        output_raster_path = "evi_{}_mean_stat.tif".format(str(year))
        if os.path.exists(os.path.join(env.workspace, output_raster_path)):
            print("{} has been created before".format(output_raster_path))
            continue
        print("CellStatistics is working for {}".format(str(year)))
        evi_year_stat = CellStatistics(evi_raster_paths, "MAXIMUM", "NODATA")
        evi_year_stat.save(output_raster_path)
Ejemplo n.º 4
0
    def calcTypeDisturbance(anthroType, subtypeRasters, AnthroDisturbanceType):
        """combine anthropogenic disturbance for all subtypes of a specific type"""
        arcpy.AddMessage("   Combining effects of "
                         + str(anthroType) + " features")
        typeRaster100 = CellStatistics(subtypeRasters, "MINIMUM")
        typeRaster = typeRaster100 / 100
        typeRaster.save(AnthroDisturbanceType + "_" + anthroType
                        + "_Type_Disturbance")

        return typeRaster
Ejemplo n.º 5
0
def calculate_climate_mean(**kwargs):
    """
    Description: calculates mean annual climate properties from a set of month-year climate rasters
    Inputs: 'input_array' -- an array containing the input month-year climate rasters
            'output_array' -- an array containing the output mean annual climate raster
            'denominator' -- an integer value of the number of years, which is the denominator in the mean calculation
    Returned Value: Returns a raster dataset on disk containing the mean annual climate property
    Preconditions: requires input month-year climate rasters that can be downloaded from SNAP at UAF
    """

    # Import packages
    import arcpy
    from arcpy.sa import CellStatistics
    from arcpy.sa import Int
    import datetime
    import time

    # Parse key word argument inputs
    denominator = kwargs['denominator']
    input_rasters = kwargs['input_array']
    output_raster = kwargs['output_array'][0]

    # Set overwrite option
    arcpy.env.overwriteOutput = True

    # Set snap raster
    arcpy.env.snapRaster = input_rasters[0]

    # Add all rasters in input array and divide by denominator
    iteration_start = time.time()
    print(
        f'\tCalculating climate mean from {len(input_rasters)} input rasters...'
    )
    climate_sum = CellStatistics(input_rasters, 'SUM', 'NODATA', 'SINGLE_BAND')
    climate_mean = Int((climate_sum / denominator) + 0.5)
    # End timing
    iteration_end = time.time()
    iteration_elapsed = int(iteration_end - iteration_start)
    iteration_success_time = datetime.datetime.now()
    # Report success
    print(
        f'\tCompleted at {iteration_success_time.strftime("%Y-%m-%d %H:%M")} (Elapsed time: {datetime.timedelta(seconds=iteration_elapsed)})'
    )
    print('\t----------')

    # Export climate mean to output raster
    iteration_start = time.time()
    print(f'\tExporting climate mean to output raster...')
    arcpy.management.CopyRaster(climate_mean, output_raster, '', '', '-32768',
                                'NONE', 'NONE', '16_BIT_SIGNED', 'NONE',
                                'NONE', 'TIFF', 'NONE')
    # End timing
    iteration_end = time.time()
    iteration_elapsed = int(iteration_end - iteration_start)
    iteration_success_time = datetime.datetime.now()
    # Report success
    print(
        f'\tCompleted at {iteration_success_time.strftime("%Y-%m-%d %H:%M")} (Elapsed time: {datetime.timedelta(seconds=iteration_elapsed)})'
    )
    print('\t----------')
    out_process = f'Finished calculating climate mean.'
    return out_process
Ejemplo n.º 6
0
def build_cn_raster(landcover_raster,
                    lookup_csv,
                    soils_polygon,
                    soils_hydrogroup_field="SOIL_HYDRO",
                    reference_raster=None,
                    out_cn_raster=None):
    """Build a curve number raster from landcover raster, soils polygon, and a crosswalk between 
    landcover classes, soil hydro groups, and curve numbers.

    :param lookup_csv: [description]
    :type lookup_csv: [type]
    :param landcover_raster: [description]
    :type landcover_raster: [type]
    :param soils_polygon: polygon containing soils with a hydro classification. 
    :type soils_polygon: [type]
    :param soils_hydrogroup_field: [description], defaults to "SOIL_HYDRO" (from the NCRS soils dataset)
    :type soils_hydrogroup_field: str, optional
    :param out_cn_raster: [description]
    :type out_cn_raster: [type]    
    """

    # GP Environment ----------------------------
    msg("Setting up GP Environment...")
    # if reference_raster is provided, we use it to set the GP environment for
    # subsequent raster operations
    if reference_raster:
        if not isinstance(reference_raster, Raster):
            # read in the reference raster as a Raster object.
            reference_raster = Raster(reference_raster)
    else:
        reference_raster = Raster(landcover_raster)

    # set the snap raster, cell size, and extent, and coordinate system for subsequent operations
    env.snapRaster = reference_raster
    env.cellSize = reference_raster.meanCellWidth
    env.extent = reference_raster
    env.outputCoordinateSystem = reference_raster

    cs = env.outputCoordinateSystem.exportToString()

    # SOILS -------------------------------------

    msg("Processing Soils...")
    # read the soils polygon into a raster, get list(set()) of all cell values from the landcover raster
    soils_raster_path = so("soils_raster")
    PolygonToRaster_conversion(soils_polygon, soils_hydrogroup_field,
                               soils_raster_path, "CELL_CENTER")
    soils_raster = Raster(soils_raster_path)

    # use the raster attribute table to build a lookup of raster values to soil hydro codes
    # from the polygon (that were stored in the raster attribute table after conversion)
    if not soils_raster.hasRAT:
        msg("Soils raster does not have an attribute table. Building...",
            "warning")
        BuildRasterAttributeTable_management(soils_raster, "Overwrite")
    # build a 2D array from the RAT
    fields = ["Value", soils_hydrogroup_field]
    rows = [fields]
    # soils_raster_table = MakeTableView_management(soils_raster_path)
    with SearchCursor(soils_raster_path, fields) as sc:
        for row in sc:
            rows.append([row[0], row[1]])
    # turn that into a dictionary, where the key==soil hydro text and value==the raster cell value
    lookup_from_soils = {v: k for k, v in etl.records(rows)}
    # also capture a list of just the values, used to iterate conditionals later
    soil_values = [v['Value'] for v in etl.records(rows)]

    # LANDCOVER ---------------------------------
    msg("Processing Landcover...")
    if not isinstance(landcover_raster, Raster):
        # read in the reference raster as a Raster object.
        landcover_raster_obj = Raster(landcover_raster)
    landcover_values = []
    with SearchCursor(landcover_raster, ["Value"]) as sc:
        for row in sc:
            landcover_values.append(row[0])

    # LOOKUP TABLE ------------------------------
    msg("Processing Lookup Table...")
    # read the lookup csv, clean it up, and use the lookups from above to limit it to just
    # those values in the rasters
    t = etl\
        .fromcsv(lookup_csv)\
        .convert('utc', int)\
        .convert('cn', int)\
        .select('soil', lambda v: v in lookup_from_soils.keys())\
        .convert('soil', lookup_from_soils)\
        .select('utc', lambda v: v in landcover_values)

    # This gets us a table where we the landcover class (as a number) corresponding to the
    # correct value in the converted soil raster, with the corresponding curve number.

    # DETERMINE CURVE NUMBERS -------------------
    msg("Assigning Curve Numbers...")
    # Use that to reassign cell values using conditional map algebra operations
    cn_rasters = []
    for rec in etl.records(t):
        cn_raster_component = Con(
            (landcover_raster_obj == rec.utc) & (soils_raster == rec.soil),
            rec.cn, 0)
        cn_rasters.append(cn_raster_component)

    cn_raster = CellStatistics(cn_rasters, "MAXIMUM")

    # REPROJECT THE RESULTS -------------------
    msg("Reprojecting and saving the results....")
    if not out_cn_raster:
        out_cn_raster = so("cn_raster", "random", "in_memory")

    ProjectRaster_management(in_raster=cn_raster,
                             out_raster=out_cn_raster,
                             out_coor_system=cs,
                             resampling_type="NEAREST",
                             cell_size=env.cellSize)

    # cn_raster.save(out_cn_raster)
    return out_cn_raster
Ejemplo n.º 7
0
def calculate_lst_warmth_index(**kwargs):
    """
    Description: sums LST values from multiple months and reprojects to AKALB
    Inputs: 'cell_size' -- a cell size for the output LST
            'input_projection' -- the machine number for the LST input projection
            'output_projection' -- the machine number for the output projection
            'geographic_transform' -- the ESRI code for the necessary geographic transformation (can be blank)
            'input_array' -- an array containing the study area raster (must be first) and the mean LST rasters for May-September
            'output_array' -- an array containing the output LST warmth index raster
    Returned Value: Returns a raster dataset on disk containing the reformatted LST
    Preconditions: requires mean decadal monthly LST downloaded from Google Earth Engine
    """

    # Import packages
    import arcpy
    from arcpy.sa import CellStatistics
    from arcpy.sa import Int
    from arcpy.sa import Nibble
    import datetime
    import os
    import time

    # Parse key word argument inputs
    cell_size = kwargs['cell_size']
    input_projection = kwargs['input_projection']
    output_projection = kwargs['output_projection']
    geographic_transform = kwargs['geographic_transform']
    input_rasters = kwargs['input_array']
    study_area = input_rasters.pop(0)
    lst_processed = kwargs['output_array'][0]

    # Set overwrite option
    arcpy.env.overwriteOutput = True

    # Set snap raster
    arcpy.env.snapRaster = input_rasters[0]

    # Define intermediate dataset
    reprojected_raster = os.path.splitext(
        lst_processed)[0] + '_reprojected.tif'

    # Define the coordinate systems
    input_system = arcpy.SpatialReference(input_projection)
    output_system = arcpy.SpatialReference(output_projection)

    # Calculate LST sum
    print('\tCalculating MODIS LST Warmth Index...')
    iteration_start = time.time()
    lst_sum = CellStatistics(input_rasters, 'SUM', 'NODATA', 'SINGLE_BAND')
    lst_warmth = Int((lst_sum / 10) + 0.5)
    # End timing
    iteration_end = time.time()
    iteration_elapsed = int(iteration_end - iteration_start)
    iteration_success_time = datetime.datetime.now()
    # Report success
    print(
        f'\tCompleted at {iteration_success_time.strftime("%Y-%m-%d %H:%M")} (Elapsed time: {datetime.timedelta(seconds=iteration_elapsed)})'
    )
    print('\t----------')

    # Set snap raster
    arcpy.env.snapRaster = study_area

    # Reproject and resample LST raster
    print('\tReprojecting LST warmth index to AKALB...')
    iteration_start = time.time()
    arcpy.management.ProjectRaster(lst_warmth, reprojected_raster,
                                   output_system, 'BILINEAR', cell_size,
                                   geographic_transform, '', input_system)
    # End timing
    iteration_end = time.time()
    iteration_elapsed = int(iteration_end - iteration_start)
    iteration_success_time = datetime.datetime.now()
    # Report success
    print(
        f'\tCompleted at {iteration_success_time.strftime("%Y-%m-%d %H:%M")} (Elapsed time: {datetime.timedelta(seconds=iteration_elapsed)})'
    )
    print('\t----------')

    # Expand the raster into the NoData region
    iteration_start = time.time()
    print(f'\tExpanding lst warmth index into NoData...')
    expanded_lst = Nibble(reprojected_raster, reprojected_raster, 'DATA_ONLY',
                          'PROCESS_NODATA')
    # End timing
    iteration_end = time.time()
    iteration_elapsed = int(iteration_end - iteration_start)
    iteration_success_time = datetime.datetime.now()
    # Report success
    print(
        f'\tCompleted at {iteration_success_time.strftime("%Y-%m-%d %H:%M")} (Elapsed time: {datetime.timedelta(seconds=iteration_elapsed)})'
    )
    print('\t----------')

    # Export the reprojected raster as a 16 bit unsigned raster
    print('\tExporting the expanded raster...')
    iteration_start = time.time()
    arcpy.management.CopyRaster(expanded_lst, lst_processed, '', '', '-32768',
                                'NONE', 'NONE', '16_BIT_SIGNED', 'NONE',
                                'NONE', 'TIFF', 'NONE')
    # Delete intermediate raster
    if arcpy.Exists(reprojected_raster) == 1:
        arcpy.management.Delete(reprojected_raster)
    # End timing
    iteration_end = time.time()
    iteration_elapsed = int(iteration_end - iteration_start)
    iteration_success_time = datetime.datetime.now()
    # Report success
    print(
        f'\tCompleted at {iteration_success_time.strftime("%Y-%m-%d %H:%M")} (Elapsed time: {datetime.timedelta(seconds=iteration_elapsed)})'
    )
    print('\t----------')
    out_process = f'Successfully created MODIS LST Warmth Index.'
    return out_process
Ejemplo n.º 8
0
def calcAverageHabitatQuality(seasonalHabitatRasters):
    statisticsType = "MEAN"
    ignoreNoData = "DATA"
    averageRaster = CellStatistics(seasonalHabitatRasters, statisticsType,
                                   ignoreNoData)
    return averageRaster