0

The following code generates an layer output, but fails to add and save it to the specified mxd. There are no other layers in the mxd, and is just a 'ghost' / placeholder for running the script outside an ArcMap session.

import arcpy,os

fc_path = "C:\\TEMP\\Footprint.shp"
layer_name = "Footprint"

mxd = arcpy.mapping.MapDocument("C:\\TEMP\\Parser.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
layerfile = os.path.join(arcpy.env.scratchFolder, layer_name + ".lyr")
arcpy.MakeFeatureLayer_management(fc_path, layer_name)
arcpy.SaveToLayerFile_management(layer_name, layerfile, "ABSOLUTE")
add_layer = arcpy.mapping.Layer(layerfile)
arcpy.mapping.AddLayer(df, add_layer)
arcpy.RefreshTOC()
mxd.save()
5
  • I think there is an error in your second line of code where fc_path = "C\\TEMP\\Footprint.shp" should probably be fc_path = "C:\\TEMP\\Footprint.shp". Also I don't think Windows will let you have two folders named C:\TEMP and C:\temp on the same file system. – PolyGeo Feb 18 '16 at 7:17
  • Fat fingered it in the question, fixed. – geodranic Feb 18 '16 at 7:27
  • 1
    Remember that the code you present in a snippet here should really be a copy/paste of something that you have just tested, so that our volunteers can develop confidence that what you are describing is 100% accurate. – PolyGeo Feb 18 '16 at 7:32
  • 1
    Do you have the MXD open while running the script? It'll probably fail if that's the case (or at least just not save). Your script above works as-is for me. – Midavalo Feb 18 '16 at 7:56
  • No arcmap open, should mention.. I'm using arc 10.1. It works huh? When I run it, and open the mxd - the layer is not saved in the file. Hmmm – geodranic Feb 18 '16 at 8:11
1

You did not originally say what ArcGIS version you are using but when I run the test below with near identical code to yours at ArcGIS 10.4 Prerelease for Desktop it behaves as I would expect.

This is what my C:\Temp\Parser.mxd looks like prior to running the test:

enter image description here

I then run the code below from IDLE:

import arcpy,os

##arcpy.CreateFishnet_management(out_feature_class="C:/Temp/Footprint.shp",
##                               origin_coord="0 0",
##                               y_axis_coord="0 1",
##                               cell_width="1", cell_height="1",
##                               number_rows="2", number_columns="2",
##                               corner_coord="", labels="NO_LABELS",
##                               template="DEFAULT", geometry_type="POLYGON")

fc_path = "C:\\Temp\\Footprint.shp"
layer_name = "Footprint"

# Added to avoid ERROR 000725, if layer file exists
arcpy.env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument("C:\\Temp\\Parser.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
layerfile = os.path.join(arcpy.env.scratchFolder, layer_name + ".lyr")
arcpy.MakeFeatureLayer_management(fc_path, layer_name)

arcpy.SaveToLayerFile_management(layer_name, layerfile, "ABSOLUTE")
add_layer = arcpy.mapping.Layer(layerfile)
arcpy.mapping.AddLayer(df, add_layer)
arcpy.RefreshTOC()
mxd.save()

Things to notice in my code are:

  • I have left in, but commented out how I created my C:\Temp\Footprint.shp
  • I added an arcpy.env.overwriteOutput = True to avoid getting a 000725 error

After running the code above my C:\Temp\Parser.mxd looked like this:

enter image description here

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.