3

I have a model (shown below) in which at the very end of it I run a Python script ('SumFieldInsertNew'). One issue I'm having is that I'm creating this model for an end-user that will have little to no knowledge of most things computer-wise. Ergo, I'm trying to make it as hassle-free and flexible as possible. My question is, I want the user to be able to name the file the script will run itself on, but am unsure of how to do something like this. So essentially, I want to make 'Empty FC' a parameter in which the user can decide what they want their file to be named. Then have the Python script directory code (where it's defining the feature class), know that whatever the user named it is conditional to it being able to run its processes.

enter image description here

My model runs as follows:

  1. It establishes a connection to an Oracle dB

  2. It creates a Query Layer based on the pre-filled 'Query' parameter

  3. It creates an empty feature class (Empty FC) into a pre-filled GdB parameter
  4. It then exectures the 'Copy Features' tool on the Query Layer and outputs the copy into the Empty FC
  5. This newly populated feature class is then given a new field, called 'SUM_Quantity_Solid'

  6. In which lastly, this new field is populated via the Python script 'SUM_FIELD_INSERT_NEW', which (as seen below), SUMs up a field and then inserts the values.

> import arcpy
> 
> # Define the feature class
    fc = r'User_Defined_Path'
> 
> # find the unique 'SEGMENT_LENGTH' values
    Slist = list() for row in arcpy.da.SearchCursor(fc, 'SEGMENT_LENGTH'):
>     # if the value isn't in the list then add it to the list
>     if not row[0] in Slist:
>         Slist.append(row[0])
> 
> for Value in Slist:
>     # definition query to limit the rows in the cursor
>     DefQ = 'SEGMENT_LENGTH = ' + str(Value)
> 
>     # Use a generator expression to populate a list from the 'QUANTITY_SOLID' field
>     b = sum(row[0] for row in arcpy.da.SearchCursor(fc, 'QUANTITY_SOLID'),DefQ)
> 
>     with arcpy.da.UpdateCursor(fc, ['QUANTITY_SOLID_SUM'],DefQ) as cursor:
>         for row in cursor:
>             row[0] = b
>             cursor.updateRow(row)

As referenced, I would like to be able to have

fc = r'User_Defined_Path'

be what the user initially defined the 'EMPTY FC' filename parameter as.

  • Can you just make the Empty FC a model parameter to gather user input? – artwork21 Aug 13 '14 at 18:56
  • @ artwork21 - Correct, this is what I want to be able to do. The only issue I can't think of at the moment is how to "tell" the Python script what the user defined 'EMPTY FC' to be. – JumpInTheFire Aug 13 '14 at 19:00
1

Instead of calling your python script from the model you would have more control of the code if the tool worked entirely in python. To do this:

  1. Export your model to python script
  2. Create a script tool and define input parameters (similar to model parameters)
  3. Combine the python scripts into one script and re-define the script tool parameters

This will allow you to easily call info (paths) using variables.

  • Great, thank you so much. I have never used a script tool/python toolbox before, So I was unaware of this. Thanks for letting me know! – JumpInTheFire Aug 13 '14 at 19:12
  • I've been going through what you suggested and I'm a bit confused. After exporting my model to python script, I then created a script tool and loaded the script. I made a few of the parameters, but I don't understand how it is that the parameters know which part of the code they are supposed to be referring to? Also, in my model there was a certain query already specified, but it was a parameter and allowed to be altered if need be. When setting an SQL Expression parameter, it doesn't show the previous input query that it's supposed to? Sorry I'm just trying to wrap my head around this. – JumpInTheFire Aug 13 '14 at 20:12
  • You will need to assign each parameter to a different variable then reference the variables within your various geoprocessing arguments. – artwork21 Aug 13 '14 at 20:42
  • Thank you, unfortunately I have no idea how to do that. I'll keep looking into it, hopefully ESRI has something. – JumpInTheFire Aug 13 '14 at 20:56
  • 1
    @JumpInTheFire You're going to want to use arcpy.GetParameterAsText() for any user defined variables. I'd start here: resources.arcgis.com/en/help/main/10.1/index.html#/… – ianbroad Aug 14 '14 at 6:22
1

I use following code to get path of a file.

from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
if filename == '':
     exit(0)
else:
     if filename.split(".")[-1] == "mxd":        #checking file extention 
          return filename
     else:
          do smth

This opens a simple window where you can navigate to your file and then use it's path. Hope this helps)

  • 1
    I think the questions is asking how to get set path within ArcGIS ModelBuilder. – artwork21 Aug 13 '14 at 19:17
  • 1
    @artwork21 I guess, you're right. How about put all that in a separate script and use it as a first item in the model? – Dmitry Aug 13 '14 at 19:23

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.