Select to view content in your preferred language

Joining Python Scripts outputs by using ModelBuilder

5220
6
04-01-2016 03:36 PM
FereshtehAghyan
Deactivated User

Hi everyone,

I am new in python and have just started to write  some simple scripts similar to the following one:

# Import arcpy module

import arcpy, os, sys

#Parameters

L= arcpy.GetParameterAsText(0)

O= arcpy.GetParameterAsText(1)

O_4= arcpy.GetParameterAsText(2)

#Variables

O_4= O

#Select by location

arcpy.SelectLayerByLocation_management(O, "WITHIN", L,"", "NEW_SELECTION", "")

#Select by Attribute

arcpy.SelectLayerByAttribute_management(O_4, "SWITCH_SELECTION", "")

#calculate field

arcpy.CalculateField_management(O, "Feild2", '"Warning"', "VB")

#calculate field

arcpy.CalculateField_management(O_4, "Field2", '"ERROR"', "VB")

When I attempt to join those scripts outputs (by using "Join" tool) in ModelBuilder; I get this error: <<Error:000229: Cannot pen \\psf\Home\Documents\Default.gdb\finaloutput ; Failed to execute (Merge)>>

I have also set my parameters as following:

Display Name         Data Type                     Type                             Direction

L                              Feature Layer               Required                     input

O                             Feature Layer               Required                     input

O_4                         Table                             Required                     output

Do you have any solution for that?

0 Kudos
6 Replies
DarrenWiens2
MVP Honored Contributor

Why do you bother asking for a parameter (O_4), if you're immediately going to overwrite it with "O"?

FereshtehAghyan
Deactivated User

Thats right! I am overwriting second "select by attribute" one by Calculate field , but should I keep it as a Parameter  and  variable (O_4) ?

0 Kudos
WesMiller
Deactivated User

is this the whole script? As Darren says your overwriting O_4 with O and then your overwriting "Warning" with "ERROR". What are each of the scripts trying to do?

FereshtehAghyan
Deactivated User

Yes that is a whole script. In this script, some features are supposed to be selected by "select by attribute" and select by location" then their two attribute table fields are supposed to be populated by"warning" and "error" values.  I will implant this and some other scripts (about 20-30) with similar functions in a model builder and the model builder  will join output of all those scripts.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Fereshteh,

I have several suggestions and comments.  First, I agree with Darren and Wes that you do not need to ask for both "O" and "O_4" if they we be set the the same value right away.  Just ask for "0" and then include you next line if needed, but it may help to have more code (see my tips below).

Q: Since it looks like you are wanting to write it in arcpy/python, is there a reason you are trying to get this to work in ModelBuilder too?  Personally, when I was first learning arcpy and it's syntax, I created a couple ModelBuilder tasks and then converted to python and then fixed the code the way I needed.  But that was not an efficient way for me, since there was so much "noise" I had to fix (but that was with ArcGIS 9.x, so may be better now).

With that said, when using python to select, you typically need to make it into a feature layer first.  See Select Layer By Location—Help | ArcGIS for Desktop  for more explanation.

Tips for future posts.

- when posting code, look thru Posting Code blocks in the new GeoNet

- It takes a while to figure out the geonet structure, but  GeoNet Community Structure​  might help, and if needed you can move your post with Moving Content to the Geoprocessing​   or Model Builder​  or Python​  spaces to get a more focused set of responses.

- with me mentioning the other places with the @ and the location, that will automatically show a reference in those spaces.  That is the way to include other places without having to post it twice. (not that you did htat, but good to remember).

FereshtehAghyan
Deactivated User

Rebecca,

As you, Darren and Wes suggested, I will replace all "O_4" with "O", I will also make feature layers from my source files, hopefully these suggestion solve the issue.

Since I am new in python I prefer to use ModelBuilder for doing "iterate" and "join" tasks. I have similar scripts to this one (about 20 scripts) which select some records in attribute table and their output are supposed to be join together In ModelBuilder.

0 Kudos