ArcGIS 10.4.1 arcpy - Has Raster Attribute Table been built?

750
1
Jump to solution
09-11-2018 07:58 AM
JoanneMcGraw
Occasional Contributor III

I have an arcpy script that I run in ArcGIS Catalog 10.4.1. The code creates a raster attribute table for a raster; a step which takes a couple of minutes to execute. Subsequent to that step, fields from a feature class are joined to it.

In an effort to make the code faster, I would like to check if the raster attribute table has already been created by a previous run and then check if the fields from the feature class have already been appended so I can skip these two processes. I know how to check the fields, but can't figure out how to find out if the RAT has already been built.

If I ListFields on the raster, it tells me that fields exist whether the RAT has been created or not; I imagine because they exist in memory. However, if I assume the existence of fields indicates the RAT exists, when I try to JoinFields, it fails because the RAT doesn't.

If worse comes to worst, I suppose I can try/except to JoinFields and if it fails because it can't find the table, I can build the RAT and then try JoinFields again. However, I am surprised I can't find a way to determine whether it already exists. ListTables doesn't return a RAT even it if does exist.

Is anyone aware of a method for finding out if a Raster Attribute Table has already been built?

Regards,

jtm

0 Kudos
1 Solution

Accepted Solutions
JoanneMcGraw
Occasional Contributor III

In case anyone else has the same question, here's what I've come up with...

It appears that when a raster attribute table is created in a file geodatabase, it is given the same name as the raster with "VAT_" pre-pended; e.g., "VAT_rasterName". So, in my code, I am now doing the following to determine if the RAT has already been built (note: path2FeatureClass points to the Feature Class the raster was created with):

   import arcpy
   import os

     :

   path2Fgdb = os.path.dirname(path2Raster)
   ratName = "VAT_" + os.path.basename(path2Raster)
   ratTable = os.path.join(path2Fgdb, ratName)

   if not (arcpy.Exists(ratTable)):
      arcpy.BuildRasterAttributeTable_management (path2Raster, "NONE")

   fields = arcpy.ListFields(path2Raster, "myFieldName")
   if (fields is None or len(fields) < 1):
      arcpy.JoinField_management(path2Raster, "Value", path2FeatureClass, "OBJECTID", "")

Not sure if it's safe to do this (in terms of future support), but for the moment it appears to be doing what I need it to do.


Cheers,

jtm

View solution in original post

0 Kudos
1 Reply
JoanneMcGraw
Occasional Contributor III

In case anyone else has the same question, here's what I've come up with...

It appears that when a raster attribute table is created in a file geodatabase, it is given the same name as the raster with "VAT_" pre-pended; e.g., "VAT_rasterName". So, in my code, I am now doing the following to determine if the RAT has already been built (note: path2FeatureClass points to the Feature Class the raster was created with):

   import arcpy
   import os

     :

   path2Fgdb = os.path.dirname(path2Raster)
   ratName = "VAT_" + os.path.basename(path2Raster)
   ratTable = os.path.join(path2Fgdb, ratName)

   if not (arcpy.Exists(ratTable)):
      arcpy.BuildRasterAttributeTable_management (path2Raster, "NONE")

   fields = arcpy.ListFields(path2Raster, "myFieldName")
   if (fields is None or len(fields) < 1):
      arcpy.JoinField_management(path2Raster, "Value", path2FeatureClass, "OBJECTID", "")

Not sure if it's safe to do this (in terms of future support), but for the moment it appears to be doing what I need it to do.


Cheers,

jtm

0 Kudos