ERROR 999998 - tabulate area tool in Python script

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

Accepted Solutions
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. 

View solution in original post

0 Kudos
15 Replies
MitchHolley1
MVP Regular Contributor

Can you post the entire code here using the 'Syntax Highlighter' in the "More" menu.  We need to know what the variables you're using refer to. 

MollyMoore
Occasional Contributor

Just posted the whole script.

DanPatterson_Retired
MVP Esteemed Contributor

too much is missing, but I can surmise that

  • either arcpy can't be found... 
  • the paths to the files can't be found
  • arcpy.overwriteOutput hasn't been set to True or it isn't even in your script if you are not using a new file as output.
  • many other possible details, but they would be script and/or environment specific
DanPatterson_Retired
MVP Esteemed Contributor

One thing I did notice is that you checkout SA within the loop, but you try to import it at the top of the script.  Within ArcMap, the rules are more accommodating.  Perhaps try checking it out first, then import SA and I would throw in some print statements to make sure everything is going out as planned.

MitchHolley1
MVP Regular Contributor

So the code is erroring out on line 23?  Could you post the entire error.  

In the UpdateCursor loop starting at line 26, you do not need to update the row every time you specify a row value.

with arcpy.da.UpdateCursor(sdm_table,["OccProb","PERCENTAGE","ElSeason"]) as cursor:
    for row in cursor:
        row[1] = (int(row[1])/40468.383184)*100
        row[2] = sdm
        if row[0] == "VALUE_1":
            row[0] = "Low"
        elif row[0] == "VALUE_2":
            row[0] = "Medium"
        cursor.updateRow(row)

del cursor

Also, I would delete cursors after you're done with them. 

MollyMoore
Occasional Contributor

Thanks for the suggestions.  I will make those updates.

I edited my post to include the full error message that I'm getting.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

If that is a layer or raster layer... a standalone script will fail unless it knows the path to the data ... which you appear to have.

that leaves the fact that you are trying to join a raster to a path...  but I would still throw a print statement in

0 Kudos
MitchHolley1
MVP Regular Contributor

Try specifying the processing cell size as an integer and not as a string. 

Also, if you're looping over multiple rasters, you're going to need a specific output for each one.  Right now, you're looping over the rasters and saving the TabulateArea output table as a single name.  I would try something like this:

rastTable = r"{0}_{1}".format(os.path.join(outTable, "tab_area_temp"),
                              str(sdm))

tab_area = TabulateArea(target_features,
                        "unique_id",
                        sdm,
                        "Value",
                        rastTable,
                        30)‍‍‍‍
DanPatterson_Retired
MVP Esteemed Contributor

good catch!  But I though Molly said the script worked in ArcMap... strange.. if that is the case... string in one, but number in another?  Hope it is that simple