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"
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
    – Midavalo
    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"
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.