Select to view content in your preferred language

Feature classes being removed from table of contents during geoprocessing in arcpro.

2656
13
05-21-2020 12:46 PM
BryceBarth
Emerging Contributor

I have a python script that copies features based on a query and saves them to a new GDB.  When the script copies a feature class, that feature class is removed from the table of contents.  How do I keep pro or the script from removing the feature classes when it makes the copies?

Tags (1)
0 Kudos
13 Replies
DanPatterson
MVP Esteemed Contributor

Perhaps you could share the salient portions of the script if it is long, or the script itself.  That is unexpected behaviour.


... sort of retired...
0 Kudos
BryceBarth
Emerging Contributor

I just attached the code

0 Kudos
DanPatterson
MVP Esteemed Contributor

Bryce... you are 

  • making a layer from a  featureclass from a gdb
  • then you do make a selection from that layer
  • finally you copy the layer with its selection to a new gdb

hence, the selected features will be copied to the new featureclass in your destination gdb.

If you were running this with ArcGIS Pro, AND had your geoprocessing options set to add results to the display, then the layer (with its selection) should have been added to your active map display and would have been saved if the project was saved.

Since this appears to be a standalone script, only the destination gdb will get the results of the query and copying.

There is nothing in your script that shows how this is running.  If it is a standalone script, then it knows nothing about the project you are working with.

It appears from 

FeederNumber = arcpy.GetParameterAsText(0)

this script might be associated with a tool running in arctoolbox...  Can you confirm your TOC before and after running this script and whether you are overwriting existing data?

I only see one reference in the options settings about removing existing layers in a TOC

References from.

Copy Features—Data Management toolbox | Documentation 


... sort of retired...
0 Kudos
BryceBarth
Emerging Contributor

I am running this script from a toolbox.  I can confirm that after running the tool.  Any Feature class that the script copies is removed from the Table of Contents except for Transfers in this run.

  It is not always the Transfer Feature class that is kept in the table of contents, but one Feature that is copied is not removed from the TOC while the rest are removed.  

I also tried running the tool with the geoprocessing option for removing layers unchecked and I still get the same issue.

0 Kudos
BryceBarth
Emerging Contributor
import arcpy
import os


# Variables #
GlobalGDB = r"C:\Users\BB0322\Desktop\DesktopProjects\VanBuran\VanBuran\VanBuran.gdb"
plottingGDB = r"C:\Users\BB0322\Desktop\DesktopProjects\VanBuran\Plotting\Plotting.gdb"
FeederNumber = arcpy.GetParameterAsText(0)
FeederNumber1 = ("'" + FeederNumber + "'")
expression = ("FeederNumber = " + FeederNumber1)
name = FeederNumber.replace('-', '_')
#Book = r"C:\Users\BB0322\Desktop\DesktopProjects\VanBuran\VanBuran\VanBuran.gdb\StakingGrids\StakingGrid_JohnHobbs_6_1"
Book = arcpy.GetParameterAsText(1)
arcpy.env.overwriteOutput = True


try:
    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "Fibercable", "FiberCable", "", "", "")
    fibercableSelect = arcpy.SelectLayerByAttribute_management("FiberCable", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(fibercableSelect, plottingGDB + "\\" + "Fibercable")
    print("Fibercable has been saved to the Plotting.gdb")
    arcpy.AddMessage("Fibercable has been saved to the Plotting.gdb")

    arcpy.CopyFeatures_management(Book, plottingGDB + "\\" + "Book")
    print("Book has been saved to the Plotting.gdb")
    arcpy.AddMessage("Book has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "Aerial", "Aerial", "", "", "")
    AerialSelect = arcpy.SelectLayerByAttribute_management("Aerial", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(AerialSelect, plottingGDB + "\\" + "Aerial")
    print("Aerial has been saved to the Plotting.gdb")
    arcpy.AddMessage("Aerial has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "Underground", "Underground", "", "", "")
    UndergroundSelect = arcpy.SelectLayerByAttribute_management("Underground", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(UndergroundSelect, plottingGDB + "\\" + "Underground")
    print("Underground has been saved to the Plotting.gdb")
    arcpy.AddMessage("Underground has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "Storage", "Storage", "", "", "")
    StorageSelect = arcpy.SelectLayerByAttribute_management("Storage", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(StorageSelect, plottingGDB + "\\" + "Storage")
    print("Storage has been saved to the Plotting.gdb")
    arcpy.AddMessage("Storage has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "SpliceClosure", "Splice", "", "", "")
    SpliceSelect = arcpy.SelectLayerByAttribute_management("Splice", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(SpliceSelect, plottingGDB + "\\" + "Splice")
    print("Splice has been saved to the Plotting.gdb")
    arcpy.AddMessage("Splice has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "Tranfers", "Tranfers", "", "", "")
    TransferSelect = arcpy.SelectLayerByAttribute_management("Tranfers", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(TransferSelect, plottingGDB + "\\" + "Transfers")
    print("Transfers has been saved to the Plotting.gdb")
    arcpy.AddMessage("Transfers has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "Removal", "Removal", "", "", "")
    RemovalSelect = arcpy.SelectLayerByAttribute_management("Removal", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(RemovalSelect, plottingGDB + "\\" + "Removal")
    print("Removal has been saved to the Plotting.gdb")
    arcpy.AddMessage("Removal has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "Riser", "Riser", "", "", "")
    RiserSelect = arcpy.SelectLayerByAttribute_management("Riser", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(RiserSelect, plottingGDB + "\\" + "Riser")
    print("Riser has been saved to the Plotting.gdb")
    arcpy.AddMessage("Riser has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "AnchorGuy", "AnchorGuy", "", "", "")
    AnchorSelect = arcpy.SelectLayerByAttribute_management("AnchorGuy", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(AnchorSelect, plottingGDB + "\\" + "AnchorGuy")
    print("AnchorGuy has been saved to the Plotting.gdb")
    arcpy.AddMessage("AnchorGuy has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "Protection", "Protection", "", "", "")
    ProtectSelect = arcpy.SelectLayerByAttribute_management("Protection", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(ProtectSelect, plottingGDB + "\\" + "Protection")
    print("Protection has been saved to the Plotting.gdb")
    arcpy.AddMessage("Protection has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "Equipment", "Equipment", "", "", "")
    EquipmentSelect = arcpy.SelectLayerByAttribute_management("Equipment", "NEW_SELECTION", expression, "")
    arcpy.CopyFeatures_management(EquipmentSelect, plottingGDB + "\\" + "Equipment")
    print("Equipment has been saved to the Plotting.gdb")
    arcpy.AddMessage("Equipment has been saved to the Plotting.gdb")

    arcpy.MakeFeatureLayer_management(GlobalGDB + "\\" + "Billing" + "\\" + "Poles", "Poles", "", "", "")
    PolesSelect = arcpy.SelectLayerByAttribute_management("Poles", "NEW_SELECTION", "Feeder = " + FeederNumber1, "")
    arcpy.CopyFeatures_management(PolesSelect, plottingGDB + "\\" + "Poles")
    print("Poles has been saved to the Plotting.gdb")
    arcpy.AddMessage("Poles has been saved to the Plotting.gdb")


except Exception as e:
    print("ERROR: " + e.args[0])
0 Kudos
DanPatterson
MVP Esteemed Contributor

three suggestions... I will check for bugs later

Select Layer By Attribute—Data Management toolbox | Documentation 

syntax for ...

arcpy.SelectLayerByAttribute_management(chihuahua_cities, 'SUBSET_SELECTION', '"population" > 10000')

I notice that you are assigning the results to a variable... that might be the reason that they get deleted ... (my first guess)

Otherwise try

  1. remove the try-except block... you don't need it, if something goes wrong, you would get the same error anyway.  Something is reminding me about results and try-except block when they finish without errors, and I can't pin it down.
  2. this is in desperation.  Save the featurelayer within the source gdb.
  3. desperation 2.  Try featureclass to featureclass (with an sql query instead of the make featurelayer)

obviously for the last 2, no try-except block and only a smaller sample to test


... sort of retired...
BryceBarth
Emerging Contributor

The Featureclass to Feaureclass worked.  Thank you.

0 Kudos
BryceBarth
Emerging Contributor

The Feature class to feature class works for the Fibercable but every other feature is removed from the table of Contents.  I do not know what could be doing this.  

0 Kudos
DanPatterson
MVP Esteemed Contributor

Did you remove the try-except block? or just change to fc2fc?


... sort of retired...
0 Kudos