I have a script that successfully converts CSVs to Feature Classes. The only problem is that the final line is causing my target feature class to be removed from the Table of Contents in the .mxd.

Code Snippet:

import arcpy
Source_CSV = r"\\int.ec\Data\Stations.csv"
Target_FC = r"Q:\Data\Stations.gdb\Stations"
Description = arcpy.Describe(Target_FC)
Target_FC_name = Description.name
Target_GDB = os.path.split(Description.catalogPath) [0]
Temp_Layer = "Temp_Layer"
SR = arcpy.Describe(Target_FC).spatialReference
Field_Mapping = """Muni "Muni" true true false 255 Text 0 0 ,First,#,Temp_Layer,Muni,-1,-1;LSName "LSName" true true false 255 Text 0 0 ,First,#,Temp_Layer,LSName,-1,-1;Lat "Lat" true true false 8 Double 0 0 ,First,#,Temp_Layer,Lat,-1,-1;Lng "Lng" true true false 8 Double 0 0 ,First,#,Temp_Layer,Lng,-1,-1;PercFlow "PercFlow" true true false 255 Text 0 0 ,First,#,Temp_Layer,PercFlow,-1,-1;OutLat "OutLat" true true false 8 Double 0 0 ,First,#,Temp_Layer,OutLat,-1,-1;OutLng "OutLng" true true false 8 Double 0 0 ,First,#,Temp_Layer,OutLng,-1,-1;AlarmSystemType "AlarmSystemType" true true false 255 Text 0 0 ,First,#,Temp_Layer,AlarmSystemType,-1,-1;CanOverFlow "CanOverFlow" true true false 255 Text 0 0 ,First,#,Temp_Layer,CanOverFlow,-1,-1;Address "Address" true true false 255 Text 0 0 ,First,#,Temp_Layer,Address,-1,-1;CSSPUrl "CSSPUrl" true true false 255 Text 0 0 ,First,#,Temp_Layer,CSSPUrl,-1,-1;Comment "Comment" true true false 8000 Text 0 0 ,First,#,Temp_Layer,Comment,-1,-1"""
arcpy.MakeXYEventLayer_management(Source_CSV, "Lng", "Lat", Temp_Layer, SR, "")
arcpy.AddMessage('Beginning Feature Class to Feature Class conversion...') 
arcpy.FeatureClassToFeatureClass_conversion(Temp_Layer, Target_GDB, Target_FC_name, "", Field_Mapping)

I'm quite sure the problem is with the last line because when the print statement just before it prints my target FC is still there. Seconds later I see it disappear from the TOC and the script ends successfully.

Here are my parameters. Though you can't see it here the 2nd one is set to "Direction: Input". Don't worry about the 3rd parameter, it's just used (successfully) in the validation script to auto-populate certain fields and variables.

enter image description here

The 4th parameter I recently added at the suggestion of MichaelStimson, but obviously I misunderstood something because now I get the following error:

ERROR 000733
New Feature Class: Same as input

I have since removed the 4th parameter.

Again, there's no problem with the execution of the script per se, I just want to know how to prevent my target FC from being removed from the map project. Ideas?

  • 1
    In your tool dialog what is your output data type? Is it file, feature class or layer? Have you got a line with arcpy.SetParameter resources.arcgis.com/en/help/main/10.2/index.html#/SetParameter/…? It sounds like your output is temporary so it's only added to the TOC while you're in the script and then removed as soon as the script finishes. Specifying the output as a parameter and then using SetParameter (SetParameterAsText) should make the result permanent. – Michael Stimson Jun 5 '18 at 23:08
  • 1
    Is this a full on tool, like Michael suggests, or are you just running a script in the python window? If its just a Python script, change FC2FC for CopyFeatures. It does the same thing, unless you absolutely need the field mapping. – KHibma Jun 5 '18 at 23:51
  • 1
    @MichaelStimson - ah, so unless I have a parameter set to "Direction: Output" Arc will remove the final product from the TOC? My second parameter, "Target Feature Class", is set to "Direction: Input". But since my application of FC2FC overwrites the Target Feature Class, as desired, I suppose I have to add another parameter, also called "Target Feature Class", and set it to "Direction: Output"? Seems a bit redundant but perhaps that is the fix? – Waterman Jun 6 '18 at 0:05
  • @KHibma - My version of Arc (10.2.2) limits FC2FC to 256 characters / field so I need the field mapping to force it to the proper 8000 characters for a feature class within a gdb. I could try CopyFeatures but fear there is a good chance it will also limit me to 256 characters in Arc 10.2.2? – Waterman Jun 6 '18 at 0:09
  • 1
    I suggest putting your tool aside for now, taking your "final line" out into a new script and seeing how much more code you need to add into it before you have created a minimum length code snippet that illustrates precisely what you have tried and where you are stuck. The code ideas suggested by @MichaelStimson look like they should be part of that snippet. – PolyGeo Jun 6 '18 at 1:25

Without seeing a code snippet we are left to guess what may be causing this but my suspicion is that you have:

arcpy.env.overwriteOutput = True

which causes the data source for that layer to be deleted as part of the overwrite (which removes it from the Contents window), and you have not written any code to add the replacement data as a new layer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.