7 Tips for Successfully Using Python with Esri or Other Mapping Software

January 29, 2019

Using Python With EsriAutomating repetitive or time-consuming tasks saves time – and often frustration – for GIS professionals. A scripting language like Python is an ideal solution for automation and also provides efficiencies around stringing together multiple tools to complete tasks. If you’re new to Python or new to using it with Esri or other mapping software, use these tips to get the most out of it:

1. Before jumping in, learn the basics of Python. To effectively use Python with a software like ArcGIS, educate yourself about Python syntax, commands, built-in functionalities, data types, loops, print statements and the like. Having a clear understanding of Python basics will help you in the long run.

2. Learn how to comment your code. The more comments the better; the extra effort will make a big difference when you or someone else needs to go back and edit the code.

3. Use print statements to follow your scripts as they run. When first writing a script, it is helpful to use print statements to see if certain parts are running properly. For example, if you have a loop in the script and want to make sure the script is going into the loop and running that code, put a temporary print statement inside the loop and run the script. If the print statement prints to the screen you know the code is written correctly.

4. Use Model Builder in ArcGIS to string together multiple tools. Doing so allows you to export the model as a Python script. This is particularly helpful if you’re uncertain of how to write some code since you can more easily visualize the order of multiple tools in Model Builder, export it as a Python script and then copy the code into another script. 

5. Know the differences in syntax between Python 2.x and Python 3.x. Esri ArcGIS Pro and the current release of QGIS both support only Python 3.x. Scripts written for Python 2.x will not run with Python 3.x due to certain syntax changes such as:

    1. Print statements:
      1. Python 2.x could be written print “Hello World!”
      2. Python 3.x needs to be written print(“Hello World!”)
    2. “Not equal” operator:
      1. Python 2.x could be written if x <> 1:
      2. Python 3.x needs to be written if x != 1:
6. Learn how to write Esri ArcGIS SQL statements correctly in Python. Here are a few relatively basic SQL queries for selecting attributes from a shapefile using Python. The queries appear in an expression such as arcpy.SelectLayerByAttribute_management("my_shapefile","NEW_SELECTION",qry). Make sure to pay attention to the single/double quotes to ensure successful outcomes:

# Selects records where the NAME field is either Appleton or Neenah
qry = "NAME = 'Appleton' or NAME = 'Neenah'"

# Uses a variable (city) to select records where the NAME field equals the city variable (Neenah)
city = “Neenah”
qry = "NAME ='"+city+"'"

# Combines a variable and the “LIKE” command to select records that start with “Green” in the NAME field
city = “Green”
qry = "NAME LIKE '"+city+"%'"

# Selects records with a hard-coded numeric
qry = "ID_NUM = 1750"

# Selects records based on numeric variables
x = 1750
qry = "ID_NUM = "+str(x)+""

# Selects records with two equal fields
qry = '"L_POSTCODE" = "R_POSTCODE"'

7. Use a cursor to find/insert or update values in a table, read existing geometries, and/or write new geometries. There are three types of cursors: SearchCursor, InsertCursor and UpdateCursor. Here’s an example of what an UpdateCursor looks like: 

import arcpy
# Set the table name to process
table = "Streets.shp"

# Create the UpdateCursor and capture the values from fields RoadClass, RoadType and ID.
rows = arcpy.da.UpdateCursor(table,["RoadClass","RoadType","ID"])
for row in rows:
        # If RoadClass == 1, update RoadType to "Interstate"
        if  row[0] == 1:
                 print("Updating ID "+str(row[2])+" to Interstate...")
                 # Change the value in RoadType to "Interstate"
                 row[1] = "Interstate"
        # Execute the update
        rows.updateRow(row)

Using Python with Esri or other mapping software empowers you to manage project time and costs through the efficient completion of certain tasks. At ADCi, we have practical experience with Python as we use it every day to customize the map data we deliver to our customers. We’re happy to share our insights about the scripting language and discuss the other ways ADCi can be a value-add for your projects. Contact us today to learn more!

Contacts ADCi

Subscribe to ADCi's Blog