Select to view content in your preferred language

RepairGeometry example 2 stand-alone script does not work

499
5
12-02-2024 07:55 AM
Labels (3)
KatClifton
Occasional Contributor

Attempted to use the RepairGeometry example 2 stand-alone script provided in Esri documentation to back up erroneous geometries and then repair them. Script fails on the CopyFeatures line, meaning that erroneous geometries are not able to be copied to another feature class before being corrected.

I did update the path to the table that was generated by the Check Geometry tool.

Repair Geometry (Data Management)—ArcGIS Pro | Documentation

Here is the sample script directly from the Esri documentation.

# Description: 
#   Goes through the table generated by the Check Geometry tool and does 
#   the following
#   1) backs-up all features which will be 'fixed' to a "_bad_geom" feature class
#   2) runs repairGeometry on all feature classes listed in the table 

import arcpy
import os
 
# Table that was produced by Check Geometry tool
table = r"c:\temp\data.gdb\cg_sample1"
 
# Create local variables
fcs = []
 
# Loop through the table and get the list of fcs
for row in arcpy.da.SearchCursor(table, ("CLASS")):
    # Get the class (feature class) from the cursor
    if not row[0] in fcs:
        fcs.append(row[0])
 
# Now loop through the fcs list, backup the bad geometries into fc + "_bad_geom"
# then repair the fc
print("> Processing {0} feature classes".format(len(fcs)))
for fc in fcs:
    print("Processing " + fc)
    lyr = 'temporary_layer'
    if arcpy.Exists(lyr):
        arcpy.Delete_management(lyr)
    
    tv = "cg_table_view"
    if arcpy.Exists(tv):
        arcpy.Delete_management(tv)

    arcpy.MakeTableView_management(table, tv, ("\"CLASS\" = '%s'" % fc))
    arcpy.MakeFeatureLayer_management(fc, lyr)
    arcpy.AddJoin_management(lyr, arcpy.Describe(lyr).OIDFieldName, tv, "FEATURE_ID")
    arcpy.CopyFeatures_management(lyr, fc + "_bad_geom")
    arcpy.RemoveJoin_management(lyr, os.path.basename(table))
    arcpy.RepairGeometry_management(lyr)

 

The following error is returned:

ExecuteError: ERROR 160144: An expected Field was not found or could not be retrieved properly.
Failed to execute (CopyFeatures).
0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

That is an example script.  Did you modify it's inputs to match your situation? (ie. featureclass name, field name)


... sort of retired...
0 Kudos
KatClifton
Occasional Contributor

Hi @DanPatterson , yes I modified the script to read the input in my environment. 

In my original post I indicated that I did update the path to the table that was generated by the Check Geometry tool.

Thanks.

0 Kudos
DanPatterson
MVP Esteemed Contributor

and the field name for the searchcursor?  Was it also called "CLASS"

SearchCursor—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
KatClifton
Occasional Contributor

@DanPatterson - yes, the search cursor was called CLASS in my table as well.  

0 Kudos
DanPatterson
MVP Esteemed Contributor

Well that leaves the .OIDFieldName

and the error message, which I assume you saw already

160144: An expected field was not found or could not be retrieved properly.—ArcGIS Pro | Documentati...

only suggests one course of action, so I would contact Tech Support if your layer does have an object id name

or try adding a check to the code to test whether there is an OIDFieldName using 'hasOID' from the table properties method

Table properties—ArcGIS Pro | Documentation

Tech Support may have other bug not listed below that return similar responses

Esri Support Search Results


... sort of retired...
0 Kudos