13

I am using ArcGIS 10.1 and I am trying to add a feature class that I create to an MXD, so that I can join to it.

This all needs to be done using Python.

Is this possible?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
ianbroad
  • 9,141
  • 4
  • 42
  • 88
  • 1
    When i run this script i see the layer pop up in the layers by drawing orger content but once the script completes it disapears... ?? – Moon47 Sep 30 '13 at 22:25
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. – BradHards Sep 30 '13 at 23:00

1 Answers1

32

Yes, it is possible. Before you can add a feature class you need to turn it into a feature layer.

This arcpy code should help:

import arcpy

FC = r"C:\...\featureclass"
arcpy.MakeFeatureLayer_management(FC, "nameoffeatureclass")

MXD = arcpy.mapping.MapDocument(r"C:\...\your.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]

layer = arcpy.mapping.Layer("nameoffeatureclass")
arcpy.mapping.AddLayer(DF, layer, "AUTO_ARRANGE")

MXD.save()

del MXD

Edit: the above code adds the layer to the first dataframe (index 0) - keep this in mind if you have multiple.

Radar
  • 10,659
  • 9
  • 60
  • 113
  • 2
    It's actually not necessary to create a feature layer first. Skip arcpy.MakeFeatureLayer_management(FC, "nameoffeatureclass"). If you leave it in you actually have two layers at the end of your code instead of one. – Emil Brundage Apr 26 '18 at 22:27