Importing the Spatial Analyst module

Spatial Analyst (arcpy.sa) is a module of the ArcPy site package. The simplest way to access Spatial Analyst functionality, including tools, operators, functions, and classes, is to import from the sa module. Using this import method makes it possible to access Spatial Analyst functionality without providing a name space and imports overloaded operators, which allows rasters to be used with operators.

The recommended sequence of imports to utilize Spatial Analyst functionality is shown below.

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

This approach allows you to:

To learn more about your options when importing ArcPy, Map Algebra or other functionality, see Importing ArcPy.

LizenzLizenz:

In Python (or when in the Python window and the Spatial Analyst extension has not already been enabled), you will need to check out a Spatial Analyst license before running a tool.

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

Customizing your interactive Python experience

The Python window (or any other interactive Python interpreter) recognizes the system environment variable PYTHONSTARTUP. If PYTHONSTARTUP has been previously set to a Python file, Python will automatically execute that file's code when the Python window is opened. This is a useful way to ensure that your Python window experience starts with all your commonly used Python utilities preloaded.

# File: pythonstartup.py
# Description: Used to customize the state of the python start up environment 
#   upon startup of ArcGIS application
#   Can specify module imports, variables, messages
# Requirements: Spatial Analyst Extension
# Author: ESRI

# Print to screen
print "%s\n%s\n%s" %("import os, sys, string","import arcpy", "from arcpy.sa import *")

# Imports
import os, sys, string
import arcpy
from arcpy import env
from arcpy.sa import *

Gehen Sie wie folgt vor, um die Umgebungseinstellung PYTHONSTARTUP hinzuzufügen:

  1. Klicken Sie mit der rechten Maustaste auf Arbeitsplatz, und wählen Sie Eigenschaften.
  2. Klicken Sie auf die Registerkarte Erweitert und dann auf Umgebungsvariablen.
  3. Klicken Sie unter Systemvariablen auf Neu.
  4. Fügen Sie PYTHONSTARTUP unter Name der Variablen hinzu.
  5. Fügen Sie den Pfad der Python-Datei unter Wert der Variablen hinzu, und klicken Sie auf OK.
  6. Klicken Sie auf OK.

Verwandte Themen


7/10/2012