ERROR 999998 - tabulate area tool in Python script

1662
15
Jump to solution
12-13-2017 12:48 PM
MollyMoore
Occasional Contributor

I have a Python script that I would like to operate as a standalone script. When I copy and paste it into the Python window in a fresh ArcMap .mxd, it operates fine. However, when I attempt to run it as a standalone script within my Python IDE, it fails during the tabulate area tool that is written like this:

     tab_area = TabulateArea(target_features,"unique_id",sdm,"Value",os.path.join(outTable,"tab_area_temp"),"30")

I am getting the ambiguous 999998 error message:

    ExecuteError: ERROR 999998: Unexpected Error.

Does anyone have any ideas as to why this might be working fine when copied and pasted into the Python window in ArcMap, but failing as a standalone script?

Full Python script is below for reference:

import arcpy, os, datetime
from arcpy import env
from arcpy.sa import *
from itertools import groupby
from operator import itemgetter

print "start time: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

arcpy.env.overwriteOutput = True

target_features = r'C:\Users\mmoore\Documents\ArcGIS\COA.gdb\PlanningUnit_Hex10acre'
sdm_folder = r'C:\Users\mmoore\Documents\ArcGIS\SDMs.gdb'
outTable = r'C:\Users\mmoore\Documents\ArcGIS\out_tables.gdb'
arcpy.env.workspace = sdm_folder
sdms = arcpy.ListRasters()

species_tables = []

for sdm in sdms:
    print sdm + " started at: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    sdm_path = os.path.join(sdm_folder,sdm)
    arcpy.CheckOutExtension("Spatial")
    tab_area = TabulateArea(target_features,"unique_id",sdm,"Value",os.path.join(outTable,"tab_area_temp"),"30")
    sdm_table = arcpy.TransposeFields_management(tab_area,"VALUE_1 Low;VALUE_2 Medium",os.path.join(outTable,sdm),"OccProb","PERCENTAGE","unique_id")
    arcpy.AddField_management(sdm_table,"ElSeason","TEXT","","",20,"ElSeason")
    with arcpy.da.UpdateCursor(sdm_table,["OccProb","PERCENTAGE","ElSeason"]) as cursor:
        for row in cursor:
            row[1] = (int(row[1])/40468.383184)*100
            cursor.updateRow(row)
            row[2] = sdm
            cursor.updateRow(row)
            if row[0] == "VALUE_1":
                row[0] = "Low"
            elif row[0] == "VALUE_2":
                row[0] = "Medium"
                cursor.updateRow(row)

    with arcpy.da.UpdateCursor(sdm_table, "PERCENTAGE") as cursor:
        for row in cursor:
            if row[0] > 10:
                pass
            else:
                cursor.deleteRow()

    case_fields = ["unique_id", "ElSeason"]
    max_field = "PERCENTAGE"
    sql_orderby = "ORDER BY {}, {} DESC".format(",".join(case_fields), max_field)

    # use groupby cursor to order by percent coverage
    with arcpy.da.UpdateCursor(sdm_table, "*", sql_clause=(None, sql_orderby)) as cursor:
        case_func = itemgetter(*(cursor.fields.index(fld) for fld in case_fields))
        for key, group in groupby(cursor, case_func):
            next(group)
            for extra in group:
                cursor.deleteRow()

    species_tables.append(sdm_table)
    arcpy.Delete_management(tab_area)
    print sdm + " finished at: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

merge = arcpy.Merge_management(species_tables, os.path.join(outTable, "SGCN_SDMxPU"))
print "end time: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The full error message is as follows:

Traceback (most recent call last):
  File "H:\Scripts\COA\COA\COA_species_populator v.7_SDMraster.py", line 23, in <module>
    tab_area = TabulateArea(target_features,"unique_id",sdm,"Value",os.path.join(outTable,"tab_area_temp"),30)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 6183, in TabulateArea
    processing_cell_size)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 6175, in Wrapper
    processing_cell_size)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 999998: Unexpected Error.
Failed to execute (TabulateArea).
0 Kudos
15 Replies
MollyMoore
Occasional Contributor

I tried the cell size as an integer and it is giving the same error.

I inserted some print statements and all the paths seem to be correct.  I'm a bit baffled by this one.

0 Kudos
MitchHolley1
MVP Regular Contributor

Could you try creating a layers out of the rasters before processing them.  The below code creates layers for the target and input rasters. 

target_features = r'C:\Users\mmoore\Documents\ArcGIS\COA.gdb\PlanningUnit_Hex10acre'

#target raster layer
arcpy.MakeRasterLayer_management(target_features, "targetLyr")

for sdm in sdms:
    print sdm + " started at: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    sdm_path = os.path.join(sdm_folder,sdm)
    arcpy.MakeRasterLayer_management(sdm_path,"{0}Lyr".format(sdm))#makes in-mem raster
    rastTable = r"{0}_{1}".format(os.path.join(outTable, "tab_area_temp"),
                                  str(sdm)
                                  )
    tab_area = TabulateArea("targetLyr",
                            "unique_id",
                            "{0}Lyr".format(sdm),
                            "Value",
                            rastTable,
                            30
                            )‍‍‍‍‍
    arcpy.Delete_management("{0}Lyr".format(sdm))
DanPatterson_Retired
MVP Emeritus

The only thing left to try is to move the origin files to a new folder location and specify a new destination location.  Repeating, the same thing is getting you no where.  If that doesn't work then there is something else fundamentally wrong.

MollyMoore
Occasional Contributor

I have tried converting both inputs to layers and tried running the script with origin files in new locations.  Both, to no avail - I am still getting the same error message.

0 Kudos
MatthewDriscoll
MVP Alum

I suspect the License is not getting checked out.  Try catching an exceptions on that before running the Tabulate.  See the Code Sample here: CheckOutExtension—Help | ArcGIS for Desktop 

MollyMoore
Occasional Contributor

I finally got this to work. But, I'm still wondering why it wouldn't work originally. Here are the different things I tried:

I was originally using a large (2.9 million features) feature class to delineate 'zones' within which to tabulate area of the raster. I tried clipping this to a smaller area and the stand-alone script then ran fine. It still would not work on the larger dataset.

Finally, I converted my zone feature class to a raster and the stand-alone script then ran on the whole dataset. 

0 Kudos