0

I'm trying to create a simple search tool so that my colleagues can easily query a database. My first version is like this.

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
SearchTerm = arcpy.GetParameterAsText(0)
Fuzzy = arcpy.GetParameterAsText(1)

if Fuzzy == False:
     NewLayer = arcpy.MakeFeatureLayer_management(r"W:\Source.lyr","Search Results (" + SearchTerm + ")","PROC_NAME LIKE '%" + SearchTerm + "%'")
else:
    NewLayer = arcpy.MakeFeatureLayer_management(r"W:\Source.lyr","Search Results (" + SearchTerm + ")","PROC_NAME = '" + SearchTerm + "'")

This works as expected if I just run the code through the python window (with the parameters hardcoded) but if I run it as a script in toolbox nothing happens. I suppose ArcMap doesnt automatically add the layer you create in a script, so I've added:

arcpy.mapping.AddLayer(df,NewLayer,"TOP")

This doesn't give add a layer to my mxd and gives me the error message: Runtime error <type 'exceptions.AssertionError'>:

Surely this should be pretty simple. Is there a better way of doing it?

1 Answer 1

2

This situation is always confusing. There are two types of 'Layers' in arcpy. The feature layer, which you are creating, isn't the type you can add to a map. The other is the layer generated from the mapping module. This is the one you want. Instead of MakeFeatureLayer, try this:

    NewLayer = arcpy.mapping.Layer(r"W:\Source.lyr")

Then you can perform your arcpy.mapping.AddLayer(df,NewLayer,"TOP").

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.