1

I'm writing my first python toolbox. I know there's a way to do this in batch processing, but for proof of concept I want to write this pyt tool to call the Clip tool and add the resulting featureclasses to the map.

According to the documentation, here is how you're supposed to call the Clip tool:

arcpy.Clip_analysis('input_feature', 'clip_feature', 'output_feature')

The parameters I have defined for my tool are two "Feature Layer"s for the user to select, and then a Derived parameter that is a Feature Layer and an Output.

After parameters are defined, the only part of the code that does anything is the execute function. I just want to call Clip_analysis with the names of the feature layers that the user defined, and then output to the derived output feature layer.

It seems that calling Clip_analysis with these parameters will output to the gdb I am working in, but it will not show up in the current layer in arcmap. How do I solve this?

  • What's the rest of your .pyt file look like? – Paul Oct 8 '15 at 18:29
2

You will want to use AddLayer in ArcPy. Something along the lines of this code should do what you're asking:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
arcpy.Clip_analysis('input_feature', 'clip_feature', 'output_feature')
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
  • Thanks for the response. I have some followup questions and issues— This seems like the kind of thing we should have to do for a stand-alone geoprocessing script. It's opening the map document that we already have open. Remember, we're doing this in a tool. How can I reference the current map document and dataframe without asking the user to specify them? It seems to defeat the purpose of writing a tool in the first place if we don't have access to any information about our current session of Arcmap. When you run the Clip tool by itself, it simply outputs a layer and adds it to the map. – user1952534 Oct 8 '15 at 21:10
  • I just tried it out and it doesn't add the output layer to the dataframe. \n This seems overly complicated. Can I make the layer created by Clip_analysis be a derived output feature for this tool? That is basically all I want. – user1952534 Oct 8 '15 at 21:43
  • I am not super familar with .pyt tools. I mainly do stand alone scripts and such. For the input of multiple features into your tool, this post should lend insight into what you want. Their examples don't add the layer to your map at the end, but you can call the AddLayer() function after each clip. You can then hard code the mxd and df variables with mxd as Current and the dataframe at the [0] position. Thus you don't have to have the user input mxd or dataframe information. Hope that helps. – Stooky Oct 9 '15 at 16:02
0

The asker provided this answer within their question, so it has been cut/paste to here:

I figured out what the problem was. I was letting it run as a background program so it had no idea about the current workspace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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