Writing helper Python functions with arcpy

If you have been working with arcpy for some time now, you might have found at some point that you re-use the same chunks of code in various modules and making changes in one place requires making modifications in other files involved.

At this stage, you should consider wrapping those bits of code into a function. This will make it easier to organize your code and make modifications as your project grows. Another thing with arcpy is that you might finding yourself going through the same procedure when starting working on a project. For instance, you set up a workspace, supply a feature class path, use the arcpy.da cursor to get values from a column and then start doing some analysis based on that. You may of course have multiple projects each of which will require a different setup, but this one is fairly common among many developers.

It is may be a good time at which you would need to compile a site package or just a helper module that will contain the functions and methods you need to use regularly.

An excellent example of compiled package of arcpy helper functions can be found here on github; the package is called arcapi. Caleb and Filip have done a great job putting together dozens of useful functions that nearly every arcpy developer uses daily. Take a look at the amazing work they have done.

So, for instance, you often need a sorted list of unique values within a column in a feature class. You could write a helper function that will return a sorted list and keep in a helper module apart from your project where you focus on implementing the business logic.

Provided this function is stored within a separate module named arcpy_helper.py, it’s just about importing this module and calling

arcpy_helper.select_distinct(“path_to_fc,”input_field_name”)

Much less code, much more productive.

Sometimes it’s not about bringing in a new piece of functionality though, but rather about writing less code.

Compare two function calls:

fields = [field.name for field in arcpy.ListFields(in_fc,field_type=”DOUBLE”)]

and

fields = get_fields(in_fc,”DOUBLE”)

Both of this calls will give you a list of fields of double type found in a feature class or a table. However, the second one is much shorter. If you need to run this piece of code just once, you are OK. But if you call it fairly often throughout the module, you can save a lot of space by using a wrapper function (sometimes referred to as syntactic sugar) that is more concise and can improve the code readability.

However, be cautious when trying to implement a functionality that you think is missing in arcpy, because there is a chance there is a ready-to-use geoprocessing tool for that. For instance, when you need to find out how many times a certain value is found in a column, the Summary Statistics GP tool can be used. When trying to remove the duplicate rows or features, the Delete Identical GP tool can be used.

But in other cases, it might be worth wrapping a chunk of code you think you will need “just for this project” into a helper function. You can collect those and at some point of time you will realize how much time you save by calling them instead searching through the projects’ code looking for a code snippet you need.

I will leave you with a helper function I’ve written for listing out fields where there are no values stored (i.e., all rows have NULL).

 

Advertisements

3 thoughts on “Writing helper Python functions with arcpy

    1. gist.github.com is a good place to keep public or private snippets of code. You can of course just have a file folder with your helpers/utils and import the module(s) when neeeded. Funny enough, it might be sometimes faster to google for code than search for it within the files 🙂

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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