Python Script error on Focal Statistics

4825
14
Jump to solution
10-07-2015 10:21 AM
ShawnKeizer1
New Contributor


I have this script that run fine until it hits the final part FocalStatistics.  Any ideas as to why it is failing? Below is the script and below the script is the error i get when i run the script.

#-------------------------------------------------------------------------------
# Name:
# Purpose:
#
# Author:      Shawn Keizer
#
# Created:     06/10/2015
# Copyright:   (c) Government of Alberta
#-------------------------------------------------------------------------------
 
def focalStats(inRaster, neighbourHood, outRaster):
    """Calculates for each input cell location a statistic of the values within a specified neighborhood around it."""
    arcpy.CheckOutExtension("Spatial")
    outraster = FocalStatistics(inRaster, neighbourHood, "MEAN", "DATA")
    outraster.save(outRaster)
 
 
 
 
 
 
def main():
    AOI     = r"C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\AOI.shp" # the area of interest to find the rasters for
    rFolder = "//env.gov.ab.ca/GIS/GISdata/ttm/data/base/topo/25mDEM" # the folder containing the rasters to search from
    oFolder = r"C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript" # the folder to copy to
    desc = arcpy.Describe(AOI) # get the extent of the AOI
    sExt = desc.extent
    arcpy.env.workspace = rFolder
    env.overwriteOutput = True
    try:
        arcpy.AddField_management(AOI,"UTM_Zone","TEXT","","", 255)
    except:
        print "UTM_Zone field already exist..."
    myLyr = arcpy.MakeFeatureLayer_management(AOI, "AOI_Layer")
    arcpy.CalculateUTMZone_cartography("AOI_Layer", "UTM_Zone")
    for row in arcpy.da.SearchCursor("AOI_Layer", ["UTM_Zone"]):
        myStr = str(row)
        if "North American 1983 UTM Zone 11U" in myStr:
            zone = "NAD_1983_UTM_Zone_11N"
        else:
            zone = "NAD_1983_UTM_Zone_12N"
    del myLyr
    myRasLst = []
    for ThisRas in arcpy.ListRasters():
        rDesc = arcpy.Describe(ThisRas)
        rExt  = rDesc.extent
        sr = rDesc.spatialReference
        if sr.name == zone:
            if sExt.disjoint(rExt):
                pass
            else:
                arcpy.AddMessage("Raster %s overlaps " % (ThisRas)  + sr.name)
                print "Raster %s overlaps " % (ThisRas)  + sr.name
                outFile = os.path.join(oFolder,ThisRas)
                arcpy.Copy_management(os.path.join(rFolder,ThisRas),outFile)
                myRasLst.append(os.path.join(oFolder,ThisRas))
    if len(myRasLst) > 1:
        for rs in myRasLst:
            if rs != myRasLst[0]:
                arcpy.Mosaic_management(rs,myRasLst[0],"MEAN")
    focalStats(myRasLst[0], NbrRectangle(100, 100, "CELL"), os.path.join(oFolder,"mElev100Ha"))
if __name__ == '__main__':
    import os, sys, arcpy
    from arcpy import env
    from arcpy.sa import *
    main()

Traceback (most recent call last):

  File "<string>", line 254, in run_nodebug

  File "C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\Test.py", line 67, in <module>

    main()

  File "C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\Test.py", line 61, in main

    focalStats(myRasLst[0], NbrRectangle(100, 100, "CELL"), os.path.join(oFolder,"mElev100Ha"))

  File "C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\Test.py", line 14, in focalStats

    outraster = FocalStatistics(inRaster, neighbourHood, "MEAN", "DATA")

  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\sa\Functions.py", line 4830, in FocalStatistics

    ignore_nodata)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\sa\Utils.py", line 47, in swapper

    result = wrapper(*args, **kwargs)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\sa\Functions.py", line 4824, in wrapper

    ignore_nodata)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 498, in <lambda>

    return lambda *args: val(*gp_fixargs(args, True))

arcgisscripting.ExecuteError: ERROR 999999: Error executing function.

Failed to create raster dataset

Failed to execute (FocalStatistics).

Message was edited by: Dan Patterson I took the liberty of formatting the code using Syntax highlighting in the >>> menu options, selecting Python

0 Kudos
14 Replies
LukeSturtevant
Occasional Contributor III

I guess my last suggestion would be to try casting the list entry as a raster object: Raster(myRasLst[0]) to use in the focal stats.

0 Kudos
StephenPalka1
New Contributor III

I tried recreating your folder structure on my computer and running the code you supplied with a test raster and everything ran fine for me. Could it be an issue with the raster you are processing?

The code I ran is below.

import arcpy
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

# Execute FocalStatistics
outFocalStatistics = FocalStatistics(r"C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\ec_1vtop25eg", NbrRectangle(100, 100, "CELL"))
# Save the output
outFocalStatistics.save(r"C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript\mElev100Ha")

EDIT: Can you check to see if you can write data to the output folder you are using through ArcGIS. Could it be an issue with permissions?

JoshuaBixby
MVP Esteemed Contributor

The error messages give the impression it has something to do with how arguments are being passed.  Since "DATA" is the default argument for the ignore_nodata, how about leaving it off in your call

outraster = FocalStatistics(inRaster, neighbourHood, "MEAN")

or passing as a keyword argument

outraster = FocalStatistics(inRaster, neighbourHood, "MEAN", ignore_nodata="DATA")

Although not common, I have run across bugs where tools error out when using positional arguments but not keyword arguments for optional parameters.

Also, I noticed you are using ArcGIS 10.1.  It could be you are running into a bug that has since been addressed in later releases.  I don't have all of the "Issues Addressed" documents pulled up, but it might be worth checking to see what Focal Statistics bugs have been fixed since ArcGIS 10.1.

0 Kudos
SteveLynch
Esri Regular Contributor

…or just outraster = FocalStatistics(inRaster)

0 Kudos
curtvprice
MVP Esteemed Contributor

I think there is an issue of being able to write to the scratch workspace, as the tool is failing before you try to save the raster, when you are writing to scratch.  The 999999 error often comes up when the system cannot write data.  I have not had good luck with UNC paths, you may want to try mapping to a letter drive.

When you are doing map algebra processing it is best to set the workspace and scratch workspace to a local folder to avoid writing across the network. Network latency can also cause these kinds of errors.

RasterWorkspace: optimize environment for faster arcpy map algebra

I really think you could solve this problem by simply setting the output workspace to where your data are being written before you run your function:

arcpy.env.workspace = oFolder
arcpy.env.scratchWorkspace = oFolder
focalStats(myRasLst[0], NbrRectangle(100, 100, "CELL"), "mElev100Ha")