adding a feture class / layer to an existing map ducument using python

2249
2
08-20-2014 06:07 AM
Michael_LesBober
New Contributor

Can someone tell me the best way to add data points into an existing mxd document within a python script.

I can create a feature set with the points in python but do not know if I should create an xy event layer, or an in memory layer and then add it to the mxd then symbolize and label the points somehow for display on the map output.  The documentation on this seems limited and I have tried several times but cannot get anything to add to the mxd document in python.  Any help on this would be greatly appreciated.

0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

Are you trying to this through arcpy.mapping? if so see this thread

0 Kudos
RiyasDeen
Occasional Contributor III

Hi Michael,

Try below script, this creates an event layer and adds it to an MXD file. You can use UniqueValuesSymbology class in mapping module to build your symbology ArcGIS Help 10.1

import arcpy

import os

table = r"C:\Users\deenr\Desktop\Data.xlsx\Sheet1$"

layerName = "EVENT_LAYER"

# Temp layer file location

lyrFile = r"C:\Users\deenr\Desktop\delete_event_layer.lyr"

# IMPORTANT, set your spatial reference

sRef = ""

arcpy.MakeXYEventLayer_management(table, "X", "Y", layerName, sRef, "")

arcpy.SaveToLayerFile_management(layerName, lyrFile)

# Create Layer object

layer = arcpy.mapping.Layer(lyrFile)

# Open MXD

mxd = arcpy.mapping.MapDocument(r"C:\Users\deenr\Desktop\forum_delete_1.mxd")

# Get your first data frame

df = arcpy.mapping.ListDataFrames(mxd)[0]

# add layer and save MXD

arcpy.mapping.AddLayer(df, layer)

mxd.save()

#Delete layer file

os.remove(lyrFile)

0 Kudos