Dark theme

Get the model working


Having tested our model, we'll now build it into an addin.


Now, if we had no other code to run, we'd just use pythonaddins.GPToolDialog() to run the model from the addin, as we did last practical. Remember, we used pythonaddins.GPToolDialog() because it was an easy way of invoking a GUI to feed parameters into a tool. If we just wanted to run a tool and could generate the parameters ourselves, we'd use arcpy.toolname_toolbox() or arcpy.toolbox.toolname().

Unfortunately, we do want to run other code: we want to sort the data produced and display it on the screen. However, as we saw in the last practical, if we call pythonaddins.GPToolDialog() the code displays the tool and keeps charging on, not waiting for the results to be produced before running the rest of the lines in the addin. We saw this in the last practical with messageDialogs.

So, what is the solution? Well, we have a few.

Firstly, we could make sure the data was always loaded up with the right names (or identify the data by searching for field in it), and hardwire those into a call to arcpy.toolname_toolbox(). This is undoubtedly the clearest, but also the least flexible way of doing things.

Secondly, we could generate the paramaters using a whole series of calls to pythonaddins.OpenDialog() and then run the tool using arcpy.toolname_toolbox(). This is probably the clearest coded way to do it with user input, but for large sets of parameters generates a very tedious user experience of endless dialogs.

Thirdly, we could get our Button to run the model, and then write an Application Extension that looks out for changes to the list of displayed data, processing the new file when it is added. This is undoubtably the right way to do this, but involves a fair amount of checking you have the right datafile, just incase someone loads up something similar – you don't want to process someone's arbitary file.

The final way is the nice slight of hand first discovered, as far as I can see by "Chris" on QueryOverflow, which is to build a parametertised tool that you run with pythonaddins.GPToolDialog() to display the GUI, but then get that tool to do all your heavy lifting, including running the model with arcpy.toolname_toolbox(). This is the way we'll do it, in part because it practices skills we've built up so far in the practicals, but also because it generates a nice GUI. The downside is that you need to run the code inside an ordinary (non-addin) script tool. You might imagine that this would be a problem, as it would stop us accessing other tools and using pythonaddins. As it happens, all scripts can access the library, though this isn't what the documentation says.

So, first up, add make a new script called TraffordModelScript.py in M:\GEOG5790M\practical4. Add it to the Models toolbox, and set up its parameters to be:

Burglaries: Feature Layer
Distance: Linear value
Buildings: Feature Layer

Note that we haven't specified the output layer – we're going to hardwire that in.

Now in the script, add:

import arcpy

arcpy.env.workspace = "m:/GEOG5790M/practical4/"

if arcpy.Exists("crime.shp"):
    arcpy.Delete_management ("crime.shp")

Burglaries = arcpy.GetParameterAsText(0)
Distance = arcpy.GetParameterAsText(1)
Buildings = arcpy.GetParameterAsText(2)
Out = "crime.shp"

arcpy.ImportToolbox("m:/GEOG5790M/practical1/Models.tbx", "models")
arcpy.TraffordModel_models(Burglaries, Distance, Buildings, Out)

Note that we both hardwire in "crime.shp" and delete any older copies so we can run the tool multiple times easily. Note that "crime.shp" probably won't be added the the map, but you can find it in ArcCatalog.

Now, finally, go back to the addin RiskButton class, and call the script we've just added by including in onClick:

pythonaddins.GPToolDialog("m:/GEOG5790M/practical1/Models.tbx", "TraffordModelScript")

Check the addin runs and the model works. One of the nice things about doing it this way is that there is minimal code in the addin, which avoids long periods of rebooting Arc. As you debug the script you'll find that not only will it pick up changes you make without rebooting Arc, but it will also sometimes leave the script parameter dialog ready to run again if it doesn't work. This make debugging a fast process.

One you've got that working, move onto the next part, where we'll sort the data in crime.shp.

 


  1. This page
  2. Build the model
  3. This page
  4. Sort the table <-- next
  5. Display the data