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")
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")
2 Answers 2
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 errorMona Coder– Mona Coder2018年10月19日 18:00:18 +00:00Commented Oct 19, 2018 at 18:00
-
1The 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– SMiller2018年10月19日 18:01:13 +00:00Commented Oct 19, 2018 at 18:01
-
the 000372 errorMona Coder– Mona Coder2018年10月19日 18:01:58 +00:00Commented 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?SMiller– SMiller2018年10月19日 18:06:12 +00:00Commented 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
Mona Coder– Mona Coder2018年10月19日 20:52:08 +00:00Commented Oct 19, 2018 at 20:52
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
lyr
variable declaration -- the path with characters "\a","\A", "\G", "\D" and "\K" does not exist.you left out the "raw" modifier on the lyr variable declaration -- the path with characters "\a","\A", "\G", "\D" and "\K" does not exist