Select to view content in your preferred language

Modelbuilder Questions: Calculate Field/Add Field Automation

4771
8
06-01-2015 03:57 PM
ColeRoberts
Emerging Contributor

Hello all! I've attached a screenshot of my working model. I'm hoping that I'm not way off on this one but I know for sure that a few sections need to be cleaned up or automated.

Model Overview.PNG

Essentially what I am trying to do is:

  1. Create a grid over a specified geometry that captures demographic information like total population and density. Grids
  2. Calculate means and standard deviation for each demographic variable for use later. Summary Statistics
  3. Calculate the geometric center of the grids and convert the grid to points. Calculate Field/Make XY Event Layer
  4. Add standardization fields for each variable and a zscore field. Add Field
  5. Use means and standard deviations to calculate standardization for each point and ultimately a Zscore. Calculate Field
  6. Use the Zscore to create a hotspot raster. IDW

Here are my questions:

  • How can I use multiple summary statistics as variables in the calculate field tool (sometimes up to 30 variables)?
  • Sometimes only 1 variable will be used when creating a grid but other times 30+ variables will be used. Is there a simple way to add the same number of columns with the Add Field tool as variables specified in the Grid tool?
    • As an example: I've specified population, household income and median age as my 3 variables. I'll need to add an extra standardization column for each and a zscore column. Is there any easier way than copying/pasting the Add Field tool 3 times?
  • Same question about automation goes for calculating the mean/sd and calculating standardization scores and Zscores for each. Iterator?

Any help is greatly appreciated! If any of this is not clear do not hesitate to ask for clarification. I know this is rather complicated to explain but my main issue is how to utilize multiple summary statistic variables to calculate standardization scores and automate field adding/calculating.

0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus

I have moved this to the Modelbuilder place to get a targeted audience.  Geonet Help is for help on using GeoNet

DarrenWiens2
MVP Honored Contributor

I think you've found yourself at the point where abandoning ModelBuilder and learning Python will greatly benefit your workflow, if this is possible in ModelBuilder at all (in fact, it may benefit all future workflows, too). Since you already have a mostly functional model, you can get a head start by exporting your model to a Python script.

Learning Python is a bit of a steep curve, although not as bad as some languages, so you will have to do some work on your own.

As a start, you will likely begin with a list of input layers, whether hard-coded or used as a script tool parameter. From that list, you get the number of items in the list or iterate through each item in the list (for example, you could add/calculate a field each time through the loop):

>>> masterFC = "points"
... fcs = ["First_Lyr","Second_Lyr","Third_Lyr"]
... print len(fcs)
... for fc in fcs:
...     print fc
...     arcpy.AddField_management(masterFC,fc,"TEXT","","",20)
...     arcpy.CalculateField_management(masterFC,fc,"\"" + fc + "\"")
...     
3
First_Lyr
Second_Lyr
Third_Lyr
WesMiller
Deactivated User

Like Darren Wiens says you are at a spot where you would need to move to code to get where you want to go. I would just like to add to what he said with, if this is your first time working with code break it into small pieces. This will make it easier to understand what is happening and helps you keep track of what's working and what's not.

ColeRoberts
Emerging Contributor

Thanks for the responses Darren and Wes!

I was afraid this was the case but I figured it was worth asking regardless. Can either of you suggest a good resources for learning Python scripting? I have some experience but that was a few years ago so I'm basically starting from scratch with this.

Thanks again!

0 Kudos
DarrenWiens2
MVP Honored Contributor

There's no concensus for best way to learn Python, but here are the GeoNet search results - you'll have to find what works best for you (book, online, etc.)

https://community.esri.com/search.jspa?q=learning+python

0 Kudos
RobinWhite
Regular Contributor

One excellent book is published by ESRI, called "Python Scripting for ArcGIS," by Paul Zandberger, ISBN 978-158948-282-1 (first ed. 2013).

cheerio

DanPatterson_Retired
MVP Emeritus

Cole... Modelbuilder works well ... until one starts editing the model and moving things around on screen.  Even though the 'picture' looks good, if you examine the order of your processes when editing the model, you will often find things get out of order, so everything is nicely coloured and linked, until you try to run it again.  I have recommended in the past (see examples on my blog) of using the Results window to begin learning how to construct scripts that work.  If you can run a tool from arctoolbox for anything, and it works, go to the results window and see the process and export it to a python snippet.  You will soon see how to translate "I know how to do it in ArcToolbox" to creating your own script.  Good luck

0 Kudos
JeffWard
Honored Contributor

The best thing about python is the command line.  Often times I get stuck on a piece of code that doesn't work, so I load a variable in the command line, and try my code out on that variable to see the results then fix it in my script.  Very handy.

Jeff Ward
Summit County, Utah