Select to view content in your preferred language

Send a feature to a map using Python

3290
13
Jump to solution
10-30-2014 10:30 AM
StephenEldridge
Deactivated User


I have python script that creates a point feature class.  How do I send that feature to an existing map using Python?

1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor
13 Replies
DanPatterson_Retired
MVP Emeritus

do you mean makefeaturelayer ?

0 Kudos
StephenEldridge
Deactivated User

My script already makes the feature and puts it into a file geodatabase.  I would like it to also take that same feature class and drop it into an ArcMap.

0 Kudos
DanPatterson_Retired
MVP Emeritus

so I assume you have ArcMap set to add results to display and set visibility on?  And I have to ask the obvious...you are running this from within ArcMap?  some people have tried to add layers to arcmap without it being open

0 Kudos
StephenEldridge
Deactivated User

I am using Python script that runs in the background.

# Import arcpy module
import arcpy

#Import Time and Date
import time
import datetime

#Sets a variable to hold today's date in year month day (yyyymmdd)format to tag feature output name with the current date
currentdate = datetime.date.today().strftime("%Y""%m""%d")

# Overwrite pre-existing files
arcpy.env.overwriteOutput = True

# Local variables:
Changes = "\\\\MyData\\Working_Data.mdb\\Changes"
Changes_Layer = "Changes_Layer"
Updates = "\\\\MyData\\My_Data.gdb\\Updates\\Updates"+str(currentdate)

# Process: Make XY Event Layer
arcpy.MakeXYEventLayer_management(Changes, "longitude", "latitude", Changes_Layer, "", "")

# Process: Feature To Point
arcpy.FeatureToPoint_management(Changes_Layer, Updates, "CENTROID")

Now I would like to add Python script that will take "Updates"+str(currentdate)" and add it to MyDataUpdates.mxd

0 Kudos
DarrenWiens2
MVP Honored Contributor

I believe you're looking for arcpy.mapping.AddLayer().

DanPatterson_Retired
MVP Emeritus

grief...

0 Kudos
StephenEldridge
Deactivated User

addLayer is what  am trying to use now but I keep getting this error:

Traceback (most recent call last):

  File "\\c:\MyLocation\Tools\Scripts\Changes.py", line 39, in <module>

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

IndexError: list index out of range

This one has me stumped.

Here is the code:

# Import arcpy module
import arcpy

#Import Time and Date
import time
import datetime

#Sets a variable to hold today's date in year month day (yyyymmdd)format to tag feature output name with the current date
currentdate = datetime.date.today().strftime("%Y""%m""%d")

# Overwrite pre-existing files
arcpy.env.overwriteOutput = True

# Local variables:
Changes = "\\\\c:\\MyLocation\\\\Working_Data.mdb\\Changes"
Changes_Layer = "Changes_Layer"
Update = "\\\\c:\\MyLocation\\MyGeo.gdb\\Updates\\Updates"+str(currentdate)

# Process: Make XY Event Layer
arcpy.MakeXYEventLayer_management(Changes, "longitude", "latitude", Changes_Layer, "", "")

# Process: Feature To Point
arcpy.FeatureToPoint_management(Changes_Layer, Updates, "CENTROID")

#Add layer into primary map document

addLayer = Changes_Layer +str(currentdate)

mxd = arcpy.mapping.MapDocument("\\\\c:\myLocation\\MyMap.mxd

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

arcpy.mapping.AddLayer(df, addLayer, "TOP")

0 Kudos
StephenEldridge
Deactivated User

The script also corrupts my mxd to the point I cannot save it.

0 Kudos
DanPatterson_Retired
MVP Emeritus

have you tried this on a local drive to rule out network issues?

0 Kudos