Setting Parameters for Model Builder

3144
2
12-08-2010 05:55 AM
BryanDeverot
New Contributor
Hello

I've been working on this off and on for the past 3 weeks so I thought I'd try to forum for some answers.  It's not that complicated of a problem. 

I have a model that I've attached to this message.  In this model I have a custom script. The script is called "MergeFolder" and I have copied and pasted it below.  It merges all shapefiles in one folder into a single shapefile.   I am using Arc9.3 and the folder contains all line shapefiles.

What I've been having problems is accepting user input for assigning the input folder and the output merged shapefile.   I know the use of arguments will solve this but how specifically can I do this? How can I change the script to accept user input?   In the model, I changed the script properties to the attachments below (inFolder & outShp).  Are the properties correct?


##
## Merge multiple shapefiles from a single directory.
##

# Import modules and create the geoprocessor object
try:
    # 9.2 and beyond
    import arcgisscripting, sys, os
    gp = arcgisscripting.create()
except:
    # 9.1 and before
    import win32com.client, sys, os
    gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

# Set the current workspace so the list has full paths
gp.workspace = os.getcwd()

# Start a blank list for appending
bList = []

# For each file in the current directory
for file in os.listdir(''):
    # Get only the shapefiles
    if (file.endswith(".shp")):
                                      
        # Append all the files together into one big list      
        filepath = bList.append(file)

        # Hard-code the output merged shapefile name
        shapefile = "Merged.shp"   
       
        # Given a list of shapefiles, separate each by a ";"
        # and put quotes around the whole thing
        def shpList(filepath):
            return '"%s"' % ';'.join(bList)

# Set the variable "mergedlist" to the newly formatted list of shapefiles           
mergedlist = shpList(filepath)
           
try:
    print "\nMerging " + mergedlist + " to get " + shapefile + "...\n"   
    gp.merge_management(mergedlist, shapefile)
    print gp.getMessages()   
except:
    print gp.getMessages()
    print "\n *** ERROR: Shapefiles failed to merge *** \n"
   
print "\nDone."


Thank you
0 Kudos
2 Replies
by Anonymous User
Not applicable
Original User: shitijmehta

The way to set parameters on the script tool is exactly the way you have shown in your attachment. You must also add the same it in your script with something like:

infolder = gp.GetParameterAsText(0)
outputfc = gp.GetParameterAsText(1)

Check this documentation:http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Understanding_script_tool_parameters

Hope this will help.
0 Kudos
BryanDeverot1
New Contributor
Hi Shitij

Thank you for your reply.  I guess the scripting part it was I need help for.  I can add the two GetParameterAsText to the script.  But what other changes would I need?  The overall goal is for this to work in model builder.


##
## Merge multiple shapefiles from a single directory.
##

# Import modules and create the geoprocessor object
try:
# 9.2 and beyond
import arcgisscripting, sys, os
gp = arcgisscripting.create()
except:
# 9.1 and before
import win32com.client, sys, os
gp = win32com.client.Dispatch("esriGeoprocessing.GpDisp atch.1")

infolder = gp.GetParameterAsText(0)
outputfc = gp.GetParameterAsText(1)

# Set the current workspace so the list has full paths
gp.workspace = os.getcwd()

# Start a blank list for appending
bList = []

# For each file in the current directory
for file in os.listdir(''):
# Get only the shapefiles
if (file.endswith(".shp")):

# Append all the files together into one big list 
filepath = bList.append(file)

# Hard-code the output merged shapefile name
shapefile = "Merged.shp" 

# Given a list of shapefiles, separate each by a ";"
# and put quotes around the whole thing
def shpList(filepath):
return '"%s"' % ';'.join(bList)

# Set the variable "mergedlist" to the newly formatted list of shapefiles 
mergedlist = shpList(filepath)

try:
print "\nMerging " + mergedlist + " to get " + shapefile + "...\n" 
gp.merge_management(mergedlist, shapefile)
print gp.getMessages() 
except:
print gp.getMessages()
print "\n *** ERROR: Shapefiles failed to merge *** \n"

print "\nDone."
0 Kudos