Apply

摘要

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

说明

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

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

语法

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

一个或多个输入栅格数据集。输入可能包括以下内容:

  • 单个栅格 - 针对需要一个栅格输入的栅格函数,并且栅格输入的栅格函数参数名称为 Raster。例如,要将 NDVI 栅格函数应用于栅格对象,输入将为 raster_object
  • 元组中的栅格集合 - 针对需要两个或多个栅格输入的栅格函数,并且栅格输入的栅格函数参数名称为 RasterRaster2Raster3 等。例如,要应用算术栅格函数以对两个栅格执行数学运算,输入将为 (raster_object_1,raster_object_2)
  • 列表中的栅格数组 - 针对需要栅格输入数组的栅格函数,并且输入的栅格函数参数名称为 Rasters。例如,要应用波段合成栅格函数以将三个栅格组合成单个栅格,输入将为 [raster_object_1,raster_object_2,raster_object_3]
  • 包含名称和栅格的字典 - 针对需要一组名称栅格输入的栅格函数。例如,要应用炎热指数栅格函数计算体感温度,输入将为 ['temperature': raster_object_1,'rh': raster_object_2]

Raster
raster_function

要应用于输入的栅格函数的名称或自定义栅格函数(.rft.xml 文件)的路径。

String
raster_function_arguments

与栅格函数或函数链关联的参数和值。如果未指定并且如果适用,将使用默认值。

例如,缨帽栅格函数不需要任何参数;输入为单个栅格,因此无需指定参数。

但是,算术栅格函数需要 5 个输入参数:栅格Raster2运算像元大小类型范围类型。要使用算术和应用函数,栅格Raster2 将在 in_raster 参数中定义,剩余参数采用默认值,因此如果没有为这些参数指定任何值,则将使用默认值。

有关每个栅格函数的函数参数的信息,请参阅栅格函数对象

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")