Python Script error on Focal Statistics

3521
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
1 Solution

Accepted Solutions
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")

View solution in original post

14 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Because the code had to be formatted, it appears that the extraneous lines got removed...can you check/confirm the line numbers in the errors to your lines in the code.

0 Kudos
ShawnKeizer1
New Contributor

Thansk Dan,

I updated the code to reflect trhe proper lines in the errors.

0 Kudos
DarrenWiens2
MVP Honored Contributor

I don't see anything immediately wrong with your code (i.e. it looks like you are passing the proper variables into your custom focalStats function, and executing the FocalStatistics call correctly). The vagueness of Error 999999 makes me wonder if restarting ArcGIS (or your Python IDE) may help.

edit: you may want to check that myRasLst[0] actually exists.

0 Kudos
SteveLynch
Esri Regular Contributor

....and persist myRasLst[0] to disk and use it in ArcMap in FocalStats

0 Kudos
ShawnKeizer1
New Contributor

so i have changhed the code to simplify the process and still get the error.  code below...  So i am sure there are no spaces and the data does exist. Any other thoughts would be most welcomed.

oFolder = r"C:\_LOCALdata\Projects\Barred_Owl\ConvertModelToScript" # the folder to copy to

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")

0 Kudos
LukeSturtevant
Occasional Contributor III

Have you tried what Steve suggested by taking the raster 'myRasLst[0] ' and try running it through the standard Focal Statistics tool from the arc toolbox? Maybe this will give you a more specific error.

0 Kudos
ShawnKeizer1
New Contributor

Hey Luke,

I have not tried that. 'myRasLst[0] ' is the first item out of 'myRasLst' which is a list. I can run the tool from Arcmap Toolbox successfully pointing to the data sources.

0 Kudos
LukeSturtevant
Occasional Contributor III

Not sure if this has anything to do with it, but I noticed your output path starts with an underscore? "C:\_LOCALdata\...." Have you tried a different output path and possibly saving the raster as a tiff or any other format?

0 Kudos
ShawnKeizer1
New Contributor

Hey Luke,

I certainly appreciate you looking into this but yes I have tried both senerios andhad the same results.

0 Kudos