Select to view content in your preferred language

If feature class is empty append

2342
3
Jump to solution
12-13-2018 01:35 PM
CCWeedcontrol
Frequent Contributor

How do you check if a feature class is empty and if it's empty append features to that feature class, if it's not not empty it would just pass or ignore the append. Would it best to use count or arcpy.da.SearchCursor?

with arcpy.da.SearchCursor(FcLyr, "SHAPE@")as cursor:
    for row in cursor:
        if row[0] is None:
            arcpy.Append_management(FcLyr2, FcLyr)          
        else:
            print 'Has features'‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Emeritus

Take a look at get count: Get Count—Help | ArcGIS Desktop 

That should just about do it....

View solution in original post

3 Replies
JoeBorgione
MVP Emeritus

Take a look at get count: Get Count—Help | ArcGIS Desktop 

That should just about do it....
CCWeedcontrol
Frequent Contributor

So i tried two different approaches. I am not sure which one is correct but they seem to be working.

Do these seem like reasonable approaches?

1st approach

result =  arcpy.GetCount_management(FcLyr)  
count = int(result.getOutput(0))
print(count)
if count == 0:  
 arcpy.Append_management(FcLyr2, FcLyr)  
 print "Layer empty, appending features " + FcLyr  
else:  
 print "layer has features " + FcLyr  

2nd approach

Count = int(arcpy.GetCount_management(FcLyr).getOutput(0))
if Count > 0:
    print("Lyr has features")
    
else:
    arcpy.Append_management(FcLyr2, FcLyr)
    print("Lyr append completed successfully")
0 Kudos
JoeBorgione
MVP Emeritus

Both seem okay to me: I like short and sweet so the 2nd approach.

Try it on some test data before you deploy it.

That should just about do it....
0 Kudos