[GIS] Getting AttributeError from ArcPy

arcpyattributeerrorspatial-analyst

I'm working on a simple Python code to make a raster from a shapefile using the Natural Neighbors tool, and then use the resulting raster to generate a contour using the Contour tool. I keep getting an Attribute Error saying the module has no attribute.

The following is my code:

import arcpy

arcpy.env.overwriteOutput = True

# Variable Definitions

in_point_features = "C:\\GIS\\PythonProgramming\\Lesson3\\Lesson3_Data\\WellsSubset.shp"
z_field = "TD"
out_polyline_features = "C:\\GIS\\PythonProgramming\\Lesson3\\Lesson3_Data\\ContourOut.shp"

# Generate raster from WellsSubset shapefile using Natural Neighbor interpolation

raster = arcpy.NaturalNeighbor (in_point_features, z_field)

# Use Contour tool with 1500 as contour interval on the raster

arcpy.Contour (raster, out_polyline_features, 1500)

The following is the error message I get when I run it:
Traceback (most recent call last):
File "C:/GIS/PythonProgramming/Lesson3/Crotty_Homework3.py", line 15, in

<module>
raster = arcpy.NaturalNeighbor (in_point_features, z_field)
AttributeError: 'module' object has no attribute 'NaturalNeighbor'

Can anyone steer me in the right direction here?

Best Answer

You need to properly call the Spatial Analyst license and module. In your case, the syntax would be as follows:

import arcpy
arcpy.CheckOutExtension("Spatial") # This checks out the license
from arcpy.sa import * # This imports all the SA tools

arcpy.env.overwriteOutput = True

# Variable Definitions

in_point_features = "C:\\GIS\\PythonProgramming\\Lesson3\\Lesson3_Data\\WellsSubset.shp"
z_field = "TD"
out_polyline_features = "C:\\GIS\\PythonProgramming\\Lesson3\\Lesson3_Data\\ContourOut.shp"

# Generate raster from WellsSubset shapefile using Natural Neighbor interpolation

raster = NaturalNeighbor (in_point_features, z_field)

# Use Contour tool with 1500 as contour interval on the raster

Contour (raster, out_polyline_features, 1500)
Related Question