0

When I run my script I get the following attribute error:

Traceback (most recent call last):
HouseGradeBasementElev.tbx#HouseGradeBasementElev.py", line 31, in 
<module>
AttributeError: 'module' object has no attribute 'ZonalStatistics_sa'

Failed to execute (HouseGradeBasementElev).

Here's my code:

import arcpy
import getpass

arcpy.CheckOutExtension("Spatial")
from arcpy.sa import *

from arcpy import env
arcpy.env.overwriteOutput = True

username = getpass.getuser()
GDB      = "C:\\Users\\"+username+"\\Documents\\ArcGIS\\Default.gdb\\"
arcpy.env.workspace = GDB

buildings   = arcpy.GetParameterAsText(0)
rd_cxn_line = arcpy.GetParameterAsText(1)
front_line   = arcpy.GetParameterAsText(2)

city_dem = "\\\\HRN-A001\\TUPW\\Infrastructure Services\\Infrastructure                     
Management\\Water Resources\\Wet Weather Database\\Major 
Proxy\\Results\\Areas\\Ottawa East\\LOS.gdb\\DEM_1m"

arcpy.AddField_management(buildings, "Front_Grade", "FLOAT")
arcpy.AddField_management(buildings, "Basement_Elevation", "FLOAT")

#Selecting front building face
arcpy.FeatureToLine_management(buildings, "blds_lines")
arcpy.SplitLine_management("blds_lines", "blds_splt_lines")
arcpy.MakeFeatureLayer_management("blds_splt_lines", 
     "blds_splt_lines_layer")
arcpy.SelectLayerByLocation_management("blds_splt_lines_layer", "INTERSECT", 
     rd_cxn_line, "1 Meter") 
arcpy.Select_analysis ("blds_splt_lines_layer", front_line) 

#Front building face elevation
arcpy.ZonalStatistics_sa(front_line, "FID_Ottawa_East_Buildings", city_dem, 
     "MEAN", "NO DATA", "bldg_fl_zonal_a")
arcpy.EucAllocation_sa(buildings, {}, {}, 1, "fieldID", {}, {}, 
     "bldg_euc_allo")
arcpy.ZonalStatistics_sa("bldg_euc_allo", "Value", "bldg_fl_zonal", "MEAN", 
     "NO DATA", "bldg_euc_zonal_b")
arcpy.ZonalStatistics_sa(buildings, "fieldID", "bldg_euc_zonal", "MEAN", "NO 
     DATA", "bldg_zonal_c")

To be clear, the attribute error is occurring on the first instance of the zonal statistics tool being used.

3

You are calling arcpy.ZonalStatistics_sa() but arcpy does not have that function. The function is whithin the sa module and can be called as follows:

arcpy.sa.ZonalStatistics(in_zone_data, zone_field, in_value_raster, {statistics_type}, {ignore_nodata})

However, you already ran from arcpy.sa import * so the following should be enough to run the tool:

ZonalStatistics(front_line, "FID_Ottawa_East_Buildings", city_dem, "MEAN", "NO DATA")

Note that I removed the last argument ("bldg_fl_zonal_a") which I assume is the output raster you want to create. However, this function does not accept an output raster filename as it returns that raster. You can save it running the following lines:

my_raster = ZonalStatistics(front_line, "FID_Ottawa_East_Buildings", city_dem, "MEAN", "NO DATA")
my_raster.save("bldg_fl_zonal_a")

You can always check examples of how to use these functions in ArcGIS Help.

| improve this answer | |
  • Thank you very much for the reply, the first part about changing the syntax to arcpy.sa.ZonalStatistics instead of _sa worked perfectly. Since I want to use the output raster in the following steps, would I need to use that saving method for each time I want to carry over the output? – Reed Sep 12 '19 at 15:47
  • 1
    If you want to save the raster to your disk, then yes: you need to use the save() method. However, if you just want to use that raster for further calculations and don't really want to save it, you can just pass the variable my_raster as the argument of other functions and only save the output you need. – Marcelo Villa-Piñeros Sep 12 '19 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.