0

I'm trying to simply add a shapefile to 2 data frames of a Map Document, the shapefile is already in a third dataframe of the same Map Document. I was using the guide below but am getting the following error: ValueError: Object: CreateObject Layer invalid data source

Adding shapefile or feature class as layer in ArcGIS Desktop using Python/ArcPy?

import arcpy
from arcpy import env
import arcpy.mapping 
env.workspace = "C:/Users/Chris/Documents/501_Customization /lab4/E_ercise_10_export/Austin"
#Get Shapefile
parkslayer = arcpy.mapping.Layer("C:/Users/Chris/Documents/501_Customization/lab4/E_ercise_10_export/Austin/parks.shp")
#Get Map Document
mxd = arcpy.mapping.MapDocument("CURRENT")
#Get data frames
df = arcpy.mapping.ListDataFrames(mxd, "*")[0, 1]
# add the layer to the map at the bottom of the TOC in data frame 0 and 1
arcpy.mapping.AddLayer(df, parkslayer, "BOTTOM")
# Refresh things
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, df1, parks
print "Shapefile successfully added to data frames"
asked Feb 5, 2017 at 17:34
1
  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Question and Answer format. Please edit your question to include the error message text in full Commented Feb 5, 2017 at 17:37

1 Answer 1

1

The Mapping Layer class arcpy.mapping.Layer() must refer to either a Layer File or a Layer in the ArcMap TOC. It can't reference a shapefile

parkslayer = arcpy.mapping.Layer("C:/Users/Chris/Documents/501_Customization/lab4/E_ercise_10_export/Austin/parks.shp")

You have a few options here:

  1. Point arcpy.mapping.Layer() to your existing layer (in dataframe 3) and add that layer into your other two dataframes. This would be the way I'd do it, as this would also pull through any symbology and definition queries if present.
  2. Use arcpy.MakeFeatureLayer() to allow arcpy to see it as a layer
  3. Create a layer file that references your shapefile, and point your arcpy.mapping.Layer() to that

Try this update to your code for option #1 above:

import arcpy
arcpy.env.workspace = "C:/Users/Chris/Documents/501_Customization /lab4/E_ercise_10_export/Austin"
mxd = arcpy.mapping.MapDocument("CURRENT")
df3 = arcpy.mapping.ListDataFrames(mxd, "Name of DF 3")[0]
parkslayer = arcpy.mapping.ListLayers(mxd, "name of layer in DF 3", df3)[0]
# Get other dataframes
dfs = arcpy.mapping.ListDataFrames(mxd, "*")[0, 1]
for df in dfs:
 # add the layer to the map at the bottom of the TOC in data frame 0 and 1
 arcpy.mapping.AddLayer(df, parkslayer, "BOTTOM")
# Refresh things
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
print "Shapefile successfully added to data frames"
answered Feb 5, 2017 at 17:44
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.