Thursday 27 April 2017

arcpy - PackageLayer_management failing within script, needs to run outside of an ArcMap session


I'm automating a workflow that requires outputting a layer package, it throws the general ERROR 999999 when executing and breaks. It does, however, generate a folder in the correct directory, with the correct name which contains the raw data, but seems to fail when outputting the layer package itself. A snippet of my current code is as follows:


# Generates a feature layer based on previously set args
arcpy.MakeFeatureLayer_management(outShp, outName, "", "", "")

# Dumby mxd for the output layer to reside
mxd = arcpy.mapping.MapDocument("C:\\TEMP\\parser.mxd")
dframe = arcpy.mapping.ListDataFrames(mxd)[0]
layer = arcpy.mapping.Layer(outName)
arcpy.mapping.AddLayer(dframe, layer, "AUTO_ARRANGE")

lyrLst = arcpy.mapping.ListLayers(mxd, outName, dframe)[0]
layer.description = outName

arcpy.PackageLayer_management(lyrLst, outLpk, "PRESERVE", "CONVERT_ARCSDE", "", "ALL", "ALL", "ALL", "", "Footprint", "Bounding")
mxd.save()

Not sure what the issue is, other than possible the tool might require somehow 'Analyzing' the data, as it does in the hard coded tool?


I use ArcGIS Desktop 10.1.


Update: Tried using SaveToLayerFile to hard write the layer, and it throws the same error.



Answer




I see a couple of problems.



  1. You have to refresh your TOC after you add your layer in order for it to be found.

  2. You have to re-find your layer after it is loaded to make additional changes to it. Your line of code that assigns the description to the layer is adding the description to the layer that was created before it was loaded.


I'm sorry, I code a bit differently than you, so I couldn't efficiently just add a few lines to yours. Here is my code that worked. I hope it helps you.


def load_feature_layer(fc_path, layer_name):
# loads a feature layer into the active data frame in the current map
# using a feature class path and a layer name string.
# returns a layer object.

mxd = arcpy.mapping.MapDocument("Current")
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()
l = get_layer_by_name(layer_name)
return l


def get_layer_by_name(name_string):
'''Finds a layer in the current MXD in active data frame by name.'''
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layer_list = arcpy.mapping.ListLayers(mxd, "*", df)
desired_layer = None
for l in layer_list:
if l.name.lower() == name_string.lower():
desired_layer = l

if l.isGroupLayer:
for sub_layer in l:
if sub_layer.name.lower() == name_string.lower():
desired_layer = sub_layer
return desired_layer

if __name__ == "__main__":

import arcpy, traceback, sys, os


try:

outShp = arcpy.GetParameterAsText(0) # Shapefile
outName = arcpy.GetParameterAsText(1) # String
outLpk = arcpy.GetParameterAsText(2) # File .lpk
#......

layer1 = load_feature_layer(outShp, outName)
layer1.description = outName


arcpy.PackageLayer_management(layer1, outLpk, "PRESERVE", "CONVERT_ARCSDE", "", "ALL", "ALL", "ALL", "", "Footprint", "Bounding")

except:

# PRINT ERROR MESSAGES
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)
arcpy.AddError("Python Messages: " + pymsg + " GP Messages: " + arcpy.GetMessages(2))


finally:
del outShp, outName, outLpk

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...