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

  • 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. – smiller Oct 19 '18 at 17:49
  • 1
    What error do you receive in the second codeblock? – smiller Oct 19 '18 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. – Vince Oct 19 '18 at 18:05
  • Oh, good catch! I'll edit my answer to include the leading r – smiller Oct 19 '18 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 – Mona Coder Oct 19 '18 at 18:16
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")
  • Thanks smiller but I am still getting the same error – Mona Coder Oct 19 '18 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. – smiller Oct 19 '18 at 18:01
  • the 000372 error – Mona Coder Oct 19 '18 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? – smiller Oct 19 '18 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 – Mona Coder Oct 19 '18 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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