Quantcast
Channel: Question and Answer » arcmap
Viewing all articles
Browse latest Browse all 248

Issue with arcpy.MakeFeatureLayer_management in tool

$
0
0

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)

Viewing all articles
Browse latest Browse all 248

Trending Articles