# Spatial Analyst 中的地图代数

``````from arcpy.sa import *

## 运行地图代数的基础知识

• 栅格计算器工具
• Python 窗口
• Python 集成式开发环境 (IDE)

### Python 窗口

Python 窗口允许您使用 ArcGIS Pro 中的地理处理工具和 Python 功能。 可在该窗口中运行的 Python 功能包括单行代码，也包括复杂的多行代码块。 Python 窗口还提供了一个使用第三方 Python 模块和库访问附加功能的位置。

Python 窗口的部分功能包括：自动完成内置行、使用变量，以及访问 PythonArcPy 功能。

### Python 集成式开发环境

``````# Name: Slope
# Description: Identifies the rate of maximum change
#               in z-value from each cell.
# Requirements: Spatial Analyst Extension

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

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

# Set local variables
inRaster = "elevation"
outMeasurement = "DEGREE"
zFactor = 0.3043

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Run Slope
outSlope = Slope(inRaster, outMeasurement, zFactor)

# Save the output
outSlope.save("C:/output/outslope02")``````

Python 窗口一样，IDE 可用于访问所有可用的 PythonArcPy 功能。

## 使用工具

``````# In the following statement, indem is either
#   in the TOC or in the current workspace
outRas = Aspect("indem")

# In the following statement the full path is specified
outRas2 = Aspect("C:/Data/indem2.tif")``````

``````outRas = Select("inras.tif", "Value > 105")

# outRas is variable defined by the previous statement and is not quoted
outdist = EucDistance(outRas)``````

## 使用运算符

``````# set outVar to 14 using the Python + operator
outVar = 5 + 9``````

``outRas = Raster("inras1.tif") + Raster("inras2.tif")``

``outRas = Raster("inras1.tif") + 8``

## 创建复杂表达式

``````outRas = Slope("indem" * 2) / 57
outdist = EucDistance(ExtractByAttributes("inras", "Value > 105"))``````

``outRas1 = (Raster("inras1") + Raster("inras2")) / Raster("inras3")``

``outRas2 = Raster("inras1") + Raster("inras2") / Raster("inras3")``

``outRas = (Raster("a") > 2) & (Raster("a") < 5)``

## 使用类

``outRas = FocalStatistics("inRaster", NbrCircle(5, "CELL"), "SUM")``

``outReclass = Reclassify("inRaster", "VALUE", RemapRange([[0, 1], [3, 10], [4, 8]]))``

## 用于输出要素、表或文件的“地图代数”功能

``````indem = "C:/Data/indem"
contourInterval = 100
Contour(indem, "C:/output/outcontours", contourInterval)``````

## 关于运行“地图代数”语句的建议

``````import arcpy
from arcpy import env
from arcpy.sa import *

env.workspace = "C:/sapyexamples/data"

``````import arcpy
from arcpy import env
from arcpy.sa import *

env.workspace = "C:/sapyexamples/data"
myRemapRange = RemapRange([[-3, 0, 0], [0, 1.75, 25], [1.75, 3.5, 50],
[3.5, 5.25, 75], [5.25, 7, 100]])

outReclassRR = Reclassify("inreclass", "VALUE", myRemapRange)

outReclassRR.save("rclassremran")``````