2

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.

5
  • Couple things: 1) try using copy features to disk instead of MFL 2) try CONVERT instead of PRESERVE 3) What arc version?
    – KHibma
    Feb 10, 2016 at 20:32
  • The input feature 'outShp' is hard written to disk, just tried your suggestion of CONVERT, to no avail. My current version is 10.1
    – geodranic
    Feb 11, 2016 at 3:17
  • 10.1 eh. We've fixed a few (not a lot, but some) 999999s since then. Hard to say exactly what it might be without reproing. The data just simple FC?
    – KHibma
    Feb 11, 2016 at 4:20
  • Just a simple polygon shapefile of raster footprints. I have run the Package Layer tool (which as stated above, runs fine), copied the snippet from the geoprocessing window.. And boom, implement it into the script and it fails.
    – geodranic
    Feb 11, 2016 at 4:24
  • Just for some background, all it is doing prior to this point in the code is grabbing a single shapefile in a single folder, copying the feature to a different output, defining a spatial reference, and adding / calculating a field. Given it is producing a folder duplicating the raw data at the PackageLayer point, this has to be where the hang up is.
    – geodranic
    Feb 11, 2016 at 4:48

2 Answers 2

2
+50

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
        #...<the beginning of your code>...

        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
2
  • 1
    I realize that you don't want to just use the current map. You'll have to change my code some to feed in a specific MXD path. Maybe add a parameter for that. I ran out of time.
    – RHB
    Feb 15, 2016 at 19:22
  • Definitely works as a stand alone, having some issues getting it to read an external mxd (pretty much kept it the same, but as you suggested, made an additional parameter for the mxd path, which seems like it should have worked).
    – geodranic
    Feb 16, 2016 at 14:36
2

The minimal standalone code to create a layer package from a feature class, e.g. a shapefile, is as follows:

import arcpy

outShp = r'D:\my.shp'
outName = 'outLayerName'
outPackagePath = r"D:\SE\LPackage\test.lpk"

arcpy.MakeFeatureLayer_management(outShp, outName, "", "", "")
layer = arcpy.mapping.Layer(outName)
layer.description = outName

arcpy.PackageLayer_management(in_layer=layer,output_file=outPackagePath,convert_data="PRESERVE",convert_arcsde_data="CONVERT_ARCSDE",extent="#",apply_extent_to_arcsde="ALL",schema_only="ALL",version="ALL",additional_files="#",summary="#",tags="#")

This standalone script is tested and works fine with an ArcGIS 10.2.2. (Please test with your 10.1 Installation).

Adding the created layer or layer package to the mxd is seperate story that can be achieved via the following snippet:

mxd_path = r"c:\my.mxd"
mxd = arcpy.mapping.MapDocument(mxd_path)
dframe = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.mapping.AddLayer(dframe, layer, "AUTO_ARRANGE")
mxd.save()
6
  • I copied this verbatim to test on ArcGIS 10.1, and it throws the same error, and fails at the exact same point... Unfortunately, after trying this - I'm pretty sure this is not a user end error, given the 999999 and multiple suggestions for a work around. I guess I'll just make the shapefile and generate the LPK manually through ArcMap. Thanks for the answer!
    – geodranic
    Feb 14, 2016 at 4:00
  • It seems OP not sure where script fails at all, he is just guessing. Chances are it is somewhere else
    – FelixIP
    Feb 14, 2016 at 22:42
  • @FelixIP, he neither stated in his question nor in his comments at which line the error throws. Feb 15, 2016 at 7:44
  • I mentioned that it fails at package layer, because it generates the folder necessary for it during processing. As in, a temp folder is generated from package layer tool as it packages the file, during which, it fails and throws the error. All other processes run properly
    – geodranic
    Feb 15, 2016 at 9:51
  • Also as described above, I tried running it against the file with just the minimal amount of code necessary to run package layer in a script, and it threw the same error.
    – geodranic
    Feb 15, 2016 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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