0

I am trying to add a Feature Class from a Geodatabase Dataset to an MXD and save a copy like this but I am getting following errors on each methos

import arcpy
import os
arcpy.env.workspace = r"C:\arcgis\ArcTutor\AAA\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\arcgis\ArcTutor\AAA\MXDs\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
lyr = arcpy.mapping.Layer("C:\arcgis\ArcTutor\AAA\GDBs\Delaware.gdb\Kent\Kent")
arcpy.mapping.AddLayer(DF, layer, "AUTO_ARRANGE")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")

enter image description here

and

import arcpy
import os
arcpy.env.workspace = r"C:\arcgis\ArcTutor\AAA\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\arcgis\ArcTutor\AAA\MXDs\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
lyr = "C:\arcgis\ArcTutor\AAA\GDBs\Delaware.gdb\Kent\Kent"
arcpy.MakeFeatureLayer_management(lyr, 'shplyr')
arcpy.mapping.AddLayer(DF, lyr, "AUTO_ARRANGE")
print(" Step 4")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")

enter image description here

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 19, 2018 at 17:43
7
  • Per the documentation for arcpy.mapping.layer, this line is looking for a layer instead of a feature class. You can try to create a layer (arcpy.MakeFeatureLayer_management) first. Commented Oct 19, 2018 at 17:49
  • 1
    What error do you receive in the second codeblock? Commented Oct 19, 2018 at 17:50
  • 1
    You left out the "raw" modifier on the lyr variable declaration -- the path with characters "\a","\A", "\G", "\D" and "\K" does not exist. Commented Oct 19, 2018 at 18:05
  • Oh, good catch! I'll edit my answer to include the leading r Commented Oct 19, 2018 at 18:06
  • Thanks Vince but can you please let me know what do you mean exactly by you left out the "raw" modifier on the lyr variable declaration -- the path with characters "\a","\A", "\G", "\D" and "\K" does not exist Commented Oct 19, 2018 at 18:16

2 Answers 2

1

Try the following adjustment to your code. Per the documentation for arcpy.mapping.layer, this line is looking for a layer instead of a feature class. You already tried to create a layer (arcpy.MakeFeatureLayer_management) first, but you are referencing the feature class again instead of the layer.

import arcpy
import os
arcpy.env.workspace = r"C:\arcgis\ArcTutor\AAA\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\arcgis\ArcTutor\AAA\MXDs\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
lyr = r"C:\arcgis\ArcTutor\AAA\GDBs\Delaware.gdb\Kent\Kent"
arcpy.MakeFeatureLayer_management(lyr, 'shplyr')
arcpy.mapping.AddLayer(DF, 'shplyr', "AUTO_ARRANGE")
print(" Step 4")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")
answered Oct 19, 2018 at 17:52
6
  • Thanks smiller but I am still getting the same error Commented Oct 19, 2018 at 18:00
  • 1
    The valueerror or 000372? At what line does it fail? You can add additional print statements to see at what point the code is failing. Commented Oct 19, 2018 at 18:01
  • the 000372 error Commented Oct 19, 2018 at 18:01
  • Edited to include "r" at the beginning of lyr path. If this doesn't work Double check the paths -- e.g. Are you certain that there is a feature class named Kent, within a feature dataset named Kent, in the Delaware GDB, in that directory? The arcpy example shows MakeFeatureLayer can be used with GDBs: arcpy.MakeFeatureLayer_management("C:/data/mexico.gdb/cities","cities_lyr"). The docs do say that complex feature classes can't be converted in this manner - what data type is the Kent feature class? Commented Oct 19, 2018 at 18:06
  • Thanks again smiller but now I am getting erroe like File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\mapping.py", line 49, in AddLayer assert isinstance(add_layer, Layer) AssertionError Commented Oct 19, 2018 at 20:52
0

The below worked for me, in a standalone python script. The mxd.saveAsCopy seems to have issues, thus would suggest that you first copy the original mxd, rename it and then use it. To do this you can use os and shutil.

import arcpy, os, shutil
dest = r'C:\Users\........\Desktop'
mymxd_old = "lakes1.mxd" # This can be a parameter / input
mymxd_new = "Lakes2.mxd" # This could be parameter / input
shutil.copy(os.path.join(dest, mymxd_old), os.path.join(dest,mymxd_new))
mymxd = arcpy.mapping.MapDocument( os.path.join(dest, mymxd_new))
df = arcpy.mapping.ListDataFrames(mymxd)[0]
fc = r'C:\Users\......2018円.gdb\Rivers'
arcpy.MakeFeatureLayer_management(fc, "Add_Rivers")
layer = arcpy.mapping.Layer("Add_Rivers")
arcpy.mapping.AddLayer(df, layer, "AUTO_ARRANGE")
mymxd.save()
del mymxd
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Oct 22, 2018 at 6:39

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.