1

I want to use a "simple" iterator to process all the text files in a directory using my model but ModelBuilder won't let me connect the file to the first step of the model. Once I run the Iterate tool, I can connect it to the "Make XY" but then it only runs with the last output of the Iterate tool?

See attached exported image and python code from the tool.

The error 000732 has "In UNIX, a text file cannot be used as an input to a tool with an input table parameter, such as CopyRows or MakeXYEventLayer" but I am using windows.

enter image description here

I have tried %PATH%/%Name%.txt as well.

# ---------------------------------------------------------------------------
# iterate.py
# Created on: 2012-03-12 16:35:41.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")
arcpy.ImportToolbox("P:/2011/Job_237_BushfireHazard_Datasets/Working/post_erratum/ProcessingModels.tbx")


# Local variables:
working = "P:\\2012\\Job_044_DM_Radio_Propogation\\Working\\FinalPropogation\\TRC_Lamb\\Lamb_ModifiedLocation\\working"
v_InFile_ = "%InFile%"
File_txt = "P:\\2012\\Job_044_DM_Radio_Propogation\\Working\\FinalPropogation\\TRC_Lamb\\Lamb_ModifiedLocation\\working\\File.txt"
File2 = "File.txt"
XY_Feature_Class = "%Path%\\%GDB%\\%Name%_pts"
cb_VHN007_1_pts = "%Path%\\%GDB%\\%Name%_pts"
Source = "%Path%\\%GDB%\\%Name%_pts"
Source_Added = "%Path%\\%GDB%\\%Name%_pts"
v_Signal_Strength = "%Path%\\%GDB%\\%Name%_pts"
Signal_Strength_Added_Pts = "%Path%\\%GDB%\\%Name%_pts"
Buffered_tower_range = "%Path%\\%GDB%\\%Name%_buf"
Joined = "%Path%\\%GDB%\\%Name%_join"
Signal_Strength_Added__2_ = "%Path%\\%GDB%\\%Name%_join"
Buffered_tower_range__3_ = "%Path%\\%GDB%\\%Name%_dowgraded"
Layer = "Layer"

# Process: ParseWorkspace
arcpy.ParsePath_mb(working, "FILE")

# Process: Iterate Files
arcpy.IterateFiles_mb(working, "trc_lamb_modifiedLocation_*", "txt", "NOT_RECURSIVE")

# Process: ParseFile
arcpy.ParsePath_mb(File_txt, "FILE")

# Process: ParseName
arcpy.gp.toolbox = "P:/2011/Job_237_BushfireHazard_Datasets/Working/post_erratum/ProcessingModels.tbx";
# Warning: the toolbox P:/2011/Job_237_BushfireHazard_Datasets/Working/post_erratum/ProcessingModels.tbx DOES NOT have an alias. 
# Please assign this toolbox an alias to avoid tool name collisions
# And replace arcpy.gp.ParseName(...) with arcpy.ParseName_ALIAS(...)
arcpy.gp.ParseName(File2)

# Process: Parse Path
arcpy.ParsePath_mb(File_txt, "PATH")

# Process: Copy Features
arcpy.CopyFeatures_management("", XY_Feature_Class, "", "0", "0", "0")

# Process: Delete Field
arcpy.DeleteField_management(XY_Feature_Class, "Best_Unit")

# Process: Add Field (4)
arcpy.AddField_management(cb_VHN007_1_pts, "Source_Ch", "TEXT", "3", "2", "50", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field (4)
arcpy.CalculateField_management(Source, "Source_Ch", "\"%Name%\"", "PYTHON", "")

# Process: Add Field (2)
arcpy.AddField_management(Source_Added, "SS", "FLOAT", "3", "2", "10", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field (3)
arcpy.CalculateField_management(v_Signal_Strength, "SS", "re_score(!Rx!,f2)", "PYTHON_9.3", "f2='99'\\ndef re_score(my_field,f2):\\n\\n if (my_field*1) < 14:\\n  f2 = '0'\\n elif (my_field*1) < 24:\\n  f2 = '1'\\n elif (my_field*1) >= 24:\\n  f2 = \"2\"\\n \\n return f2")

# Process: Buffer
arcpy.Buffer_analysis(Signal_Strength_Added_Pts, Buffered_tower_range, "30 Meters", "FULL", "ROUND", "NONE", "")

# Process: Spatial Join
arcpy.SpatialJoin_analysis(Signal_Strength_Added_Pts, Buffered_tower_range, Joined, "JOIN_ONE_TO_ONE", "KEEP_ALL", "Latitude \"Latitude\" true true false 8 Double 0 0 ,First,#,%Path%\\%GDB%\\%Name%_pts,Latitude,-1,-1;Longitude \"Longitude\" true true false 8 Double 0 0 ,First,#,%Path%\\%GDB%\\%Name%_pts,Longitude,-1,-1;Rx \"Rx\" true true false 8 Double 0 0 ,First,#,%Path%\\%GDB%\\%Name%_pts,Rx,-1,-1;Source_Ch \"Source_Ch\" true true false 50 Text 0 0 ,First,#,%Path%\\%GDB%\\%Name%_pts,Source_Ch,-1,-1;SS \"SS\" true true false 4 Float 0 0 ,First,#,%Path%\\%GDB%\\%Name%_pts,SS,-1,-1;SS_1 \"SS\" true true false 4 Float 0 0 ,First,#,%Path%\\%GDB%\\%Name%_buf,SS,-1,-1", "WITHIN", "30 Meters", "")

# Process: Add Field (3)
arcpy.AddField_management(Joined, "Downgraded_SS", "FLOAT", "3", "2", "10", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field (2)
arcpy.CalculateField_management(Joined, "Downgraded_SS", "re_score(!SS!, !SS_1!)", "PYTHON", "def re_score(f1,f2):\\n    if f1 < f2:\\n        out1 = f1\\n    else:\\n        out1 = f2\\n    return out1")

# Process: Buffer (3)
arcpy.Buffer_analysis(Signal_Strength_Added__2_, Buffered_tower_range__3_, "30 Meters", "FULL", "ROUND", "LIST", "Downgraded_SS")

# Process: Make XY Event Layer (2)
arcpy.MakeXYEventLayer_management(v_InFile_, "", "", Layer, "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119521E-09;0.001;0.001;IsHighPrecision", "")

1 Answer 1

4

Assuming that your text files are some sort of tables (e.g. CSV having X and Y coordinates in rows with delimited columns), try using 'Iterate Tables' instead of 'Iterate Files'. You will be offered an option of choosing DBF or INFO as the type. Ignore this field. It is optional. You should now be able to connect the output file to the 'Make XY Event Layer' tool.

You have included a heap of Python code in this post. Remember that the 'Export to Python Script' option in Model Builder will not include the iterator. If you want a Python script, then you will have to write this yourself.

6
  • Thanks. Seems to work perfectly. I thought the model export to python will include the iterator but I guess it leaves out the model only tools. Best.
    – GeorgeC
    Commented Mar 13, 2012 at 6:52
  • Glad it works. This is such a frequent 'gotcha' that it is worth always denoting all path strings as raw or doing the global replace hack as a matter of course. Model Builder's export to python function is very limited and doesn't pretend to write nice code. Professional programmers tend to throw their hands up in horror at it, but it does generate most of what you need (excepting iterations) Commented Mar 13, 2012 at 9:20
  • I don't normally use it either but since people in the forum request the code, I just thought I would include it.
    – GeorgeC
    Commented Mar 13, 2012 at 22:54
  • I like Model Builder as a rapid development tool. For a process I want to keep, I'll export to Python and refactor into proper classes/functions, add the iterations and strip out all the 'Local Variables' as these can usually be simplified in some way. The export to Python function is great because I keep forgetting the differences between ArcPy and ArcGisScripting and it saves me having to memorise all the arguments for every Arc function (is that being lazy?). That said, I will sometimes start coding from scratch if I know the functions well as the auto-generated code is a bit nasty :) Commented Mar 14, 2012 at 8:59
  • 1
    This is where you really need to convert to using a Python script. In the script you can have a simple test to see if the output exists BEFORE doing all the work and relying on OverwriteOutput. Next, in your loop bracket the part where you get errors with a Python Try-Except test (docs.python.org/tutorial/errors.html). I would make 'Except' print some statement to stdout to let you know which files failed. Commented Mar 15, 2012 at 9:15

Your Answer

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

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