Apply

摘要

通过将栅格函数或函数链应用于一个或多个输入栅格来创建栅格对象。

说明

使用 Apply 函数处理一个或多个应用了栅格函数或函数链的栅格数据集。 此函数将创建一个栅格对象,此栅格对象是应用了栅格函数的输入栅格对象。

输出栅格对象所引用的栅格数据集是临时性的。 要将其设置为永久,可以调用栅格对象的 save 方法。

语法

Apply (in_raster, raster_function, {raster_function_arguments})
参数说明数据类型
in_raster
[in_raster,...]

The input raster dataset or datasets. The input can include the following:

  • A single raster—For raster functions that require one raster input, and the raster function argument name for the raster input is Raster. For example, to apply the NDVI raster function to a raster object, the input would be raster_object.
  • A collection of rasters in a tuple—For raster functions that require two or more raster inputs, and the raster function argument names for the raster inputs are Raster, Raster2, Raster3, etc. For example, to apply the Arithmetic raster function to perform a mathematical operation on two rasters, the input would be (raster_object_1,raster_object_2).
  • An array of rasters in a list—For raster functions that require an array of raster inputs, and the raster function argument name for the inputs is Rasters. For example, to apply the Composite Bands raster function to combine three rasters into a single raster, the input would be [raster_object_1,raster_object_2,raster_object_3].
  • A dictionary with names and rasters—For raster functions that require a set of names raster inputs. For example, to apply the Heat Index raster function to calculate apparent temperature, the input would be ['temperature': raster_object_1,'rh': raster_object_2].

Raster
raster_function

The name of a raster function or the path to a custom raster function (.rft.xml file) to apply to the input.

String
raster_function_arguments

The parameters and values associated with the raster function or function chain. If not specified, and if applicable, default values will be used.

For example, the Tasseled Cap raster function does not require any arguments; the input is a single raster, so there is no need to specify arguments.

The Arithmetic raster function, however, requires 5 input parameters: Raster, Raster2, Operation, Cellsize Type and Extent Type. To use Arithmetic with the Apply function, Raster and Raster2 are defined in the in_raster parameter, and the remaining parameters have default values, so if nothing is specified for those parameters, the default values will be used.

For information about the function arguments for each raster function, see Raster function objects.

Dictionary
返回值
数据类型说明
Raster

应用了函数的输出栅格数据集。

代码示例

应用示例 1

将 NDVI、算数和波段合成函数应用于一个或多个栅格数据集。

# Apply NDVI, Arithmetic, and Composite Bands functions to one or more raster datasets.

# Import system modules
import arcpy
from arcpy.sa import *
from arcpy import env

# Set environment settings
env.workspace = "C:/sapyexamples/data"

# Set input raster for NDVI
Landsat = Raster("Landsat") 

# Apply NDVI raster function to a single raster
out_NDVI_raster = Apply(Landsat, "NDVI", {'VisibleBandID':4,'InfraredBandID':5})

# Set input rasters for Arithmetic
DEM = Raster("DEM.tif")
Buildings = Raster("BuildingHeights.tif") 

# Apply Arithmetic raster function to add building height to ground elevation
out_Arithmetic_raster = Apply((DEM, Buildings),'Arithmetic',{'operation':1})

# Set input rasters for Composite Bands
Band1 = Raster("blue.tif") 
Band2 = Raster("green.tif") 
Band3 = Raster("red.tif") 

# Apply Composite Bands function to combine three bands into a single raster
out_composite_rasters = Apply([Band1, Band2, Band3], 'CompositeBand')
应用示例 2

将炎热指数函数应用于两个多维栅格子集。

# Apply Heat Index function to multidimensional raster objects

# Import system modules
import arcpy
from arcpy.sa import *

# Set input multidimensional raster dataset
in_raster = Raster("C:/sapyexamples/data/ClimateData.nc", True)

# Select the relative humidity variable
RH = Subset(in_raster, variables = 'rh')

# Select the temperature variable
Temp = Subset(in_raster, variables = 't')

# Apply the Heat Index function to the two multidimensional raster subsets
# The output is a multidimensional raster where each slice is the heat index for the slice's time
heat_index_raster = Apply({'temperature': Temp, 'rh': RH}, 'HeatIndex', {'units':'Kelvin', 'outUnits':'Fahrenheit'})

# Save the output as crf
heat_index_raster = ("C:/sapyexamples/output/HeatIndexRaster.crf")