Python in ArcGIS

Recently I have got a chance to dive into Python development, particularly into the ArcPy site package. I have started learning more about Python and how it can be used within the ArcGIS platform one year ago and use it now and then mostly for the cases when the geoprocessing workflow complexity becomes too high and using ModelBuilder is no longer practical. I do like the Iterators introduced in ArcGIS 10.0 and I have shown how one can take advantage of them in some previous posts. No doubt, they fill the gap between Python and ModelBuilder visual modelling to some extent. However, nowadays I tend to start building my geoprocessing workflows using Python directly from the beginning.

This is partly because it became easier for me now to see the workflow from the code rather than visually in ModelBuilder with numerous objects and the relationships. Another thing is that it is so much easier to build up the Python script functionality by using external modules such as zipping, os, logging, math and many more. Copying and pasting chunks of code into the PyScripter (my favorite IDE so far) is so much easier than dealing with creating first script tools in a toolbox and then adding the tool to the model and setting up all the conditions and connections. Dealing with passing the values from a model to a script can be also quite often cumbersome comparing to doing that in the script source code.

Another thing is that programming with Python is quite a valuable skill for a GIS professional as was mentioned by Bill Dollins in his blog post. I agree with him and do try to stay comfortable with programming Python for ArcGIS. By the way, if you just have started thinking of learning Python programming because you want to automate some parts of your current business workflows, check the Python Scripting for ArcGIS (10.1) book. I have gone through this one in February and can recommend it very high. This book should get you up and running fairly quickly. Another good book to look at is A Python Primer for ArcGIS (10.0) – it was written for 10.0 and does not cover a couple of things introduced in 10.1 (like Data Access module cursors) yet it is still worth checking. I have read through this one last year too and liked it very much.

A couple of weeks ago I have developed a Python add-in in ArcGIS 10.1 for managing some GIS logic within ArcMap which was is not available out-of-the-box. I am talking about the ability to define the geometry of the feature with the NULL shape in a feature class – this was supposed to get done by user while selecting a row in the attribute table (which represents a feature) and then click on the map to set the XY values for the point feature geometry. If you are not ArcObjects guru and don’t plan to heavily use it yet still need to get a “button” in ArcGIS that user would be able to interact with, you should definitely check the Python add-ins that were introduced in 10.1. It is also a great way to basically let your current GP script tools to run from the ArcGIS GUI and, if applicable, respond to user actions.

Anyways, the business logic steps are as below:
1. User selects a feature in the attribute table of the feature class created from the text file (here inserting null geometries for coordinates would work fine).
2. User chooses what location this feature will have by double-clicking this location on the map. This fires up several things.
2a. The add-in’s onMouseDownMap(self, x, y, button, shift) function gets XY coordinates of where the user clicks (in fact, only the first click of the mouse is required here).
2b. The add-in’s onDblClick(self) function starts running. The logic checks with the Get Count GP tool if there is exactly one feature selected. Here is where the arcpy.da.UpdateCursor comes into play. I am updating the geometry of the selected feature based on the XY values received earlier in another function.
3. Lastly, refreshing active view.

Happy Pythoning!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s