Sunday 2 July 2017

arcgis 10.1 - How to add feature class to MXD with ArcPy (Python)?




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?



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...