2

I have created a python toolbox that creates separate layers based on unique field values within a feature layer. For some reason arcpy.MakeFeatureLayer_management will create the layer but will not add it to the TOC even after using RefreshTOC(). I used the same tool in the python interpreter and it adds the created layer to the TOC.

The layer doesn't need to be written to disk or saved permanently; I wanted to leave the saving up to the user. Any thoughts on why there is a difference and how to get the layer added to the TOC?

EDIT

    ws = arcpy.env.workspace
    mxd = arcpy.mapping.MapDocument("CURRENT")
    data_frame = arcpy.mapping.ListDataFrames(mxd)[0]
    layer = parameters[0].valueAsText #GPFeature Layer
    field = parameters[1].valueAsText #field name

    seach_layer = arcpy.mapping.Layer(layer)
    lfields = arcpy.ListFields(seach_layer.dataSource)

    rows = arcpy.SearchCursor(layer, fields=field)
    #get they field type to use in query
    ftype = None
    for f in lfields:
        if f.name == field:
            ftype = f.type
    # get values for unique list
    value_list = []
    for row in rows:
        value = row.getValue(field)
        value_list.append(value)

    unique = set(value_list)
    len_list = len(unique)

    try:
        for name in unique:
            if ftype in ['Float','Double','SmallInteger']:
                where_clause = "\"%s\"=%s" %(field, name)
            else:
                where_clause = "\"%s\"='%s'" %(field, name)

            temp = r"%s_temp_%s" %(layer, field)

            # make a feature layer to an "outfile" that's in memory
            # add to TOC and leave up to user to save if they want
            arcpy.MakeFeatureLayer_management(layer, temp,
                                              where_clause)

    except Exception as e:
        arcpy.AddError(e)
  • 1
    Do you have the output of MFL set as a parameter on the tool? Without an output, nothing is going to be added. – KHibma Jan 22 '16 at 14:15
  • I set the output in the script so that arcpy.MakeFeatureLayer_management(layer, temp, where_clause). temp is just a string and so the layer is stored in memory I believe. The documentation for the tool says the feature layer is created and is temporary which is what I'm wanting to get, but the feature layer doesn't appear. –  Jan 22 '16 at 14:27
  • 1
    You'll probably need to add your script to the issue above to better understand the issue – KHibma Jan 22 '16 at 14:53
  • 1
    If thats the full script, its what I thought. You dont have any parameters (arcpy.SetParameterAsText(0, temp). See this help link – KHibma Jan 22 '16 at 15:31
  • 1
    http://gis.stackexchange.com/questions/35693/use-python-to-add-layers-to-toc – klewis Jan 22 '16 at 15:58
  • 1
    Also under ArcMap Geoprocessing Options, "Add results of geoprocessing operations to the display" – klewis Jan 22 '16 at 16:06
  • @klewis Thanks. I have looked at this post before but isn't using arcpy.mapping.Layer and arcpy.mapping.AddLayer` redundent since MakeFeatureLayer is supposed to add the Feature Layer? @KHibma Thanks also. Your'e saying that I need to add another parameter as a derived output then set that name to temp? I assumed I could pass a name directly into the MakeLayer. –  Jan 22 '16 at 16:22
  • 1
    After you MakefeatureLayer, add the layer to TOC, arcpy.Mapping.addLayer, then refreshTOC. – klewis Jan 22 '16 at 17:37
  • 1
    @mECH yes, derived output parameter. Only 'parameters' will get added to the ToC. Params are the driving mechanism to having the tool interact with the map. – KHibma Jan 22 '16 at 18:33
  • @KHibma your solution did indeed work and add the layers to the ToC. Thanks! If you write it as an answer I'd be happy to mark this as solved. As a note I do seem to suffer from a memory problem with only a few layers added; I'm going to try klewis solution to see if it works/helps with the memory. –  Jan 28 '16 at 23:17

1 Answers1

3

When working with tools, both models and scripts, the output is only added to the ToC if the particular output is a parameter. Parameters control interaction between the tool and the map.

In a script, use something like the following to set the output

arcpy.MakeFeatureLayer_management(layer, temp, where_clause)
arcpy.SetParameterAsText(0, temp)

This Setting script tool parameters help explains parameters in more detail

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
KHibma
  • 16,786
  • 1
  • 31
  • 55