Wednesday, May 22, 2024

GIS Programming - an Introduction to Python

A week removed from completing Computer Cartography (GIS4006), my next class in the UWF Online GIS Certification Program is GIS Programming (GIS4102), which covers Python. Python is an open-sourced programming language used for geoprocessing tools in ArcGIS Pro. Python is also object-orientated, which is a programming model that organizes software around data (objects) as opposed to functions and logic.

Python is the is the scripting language of choice for geoprocessing in ArcGIS. A scripting language references the automation of certain functionality within another program. The scripting programming task connects separate, existing components in an effort to perform a new associated task. Python can take manual tasks in GIS and automate them.

Python is an interpreted language and does not need to be compiled. That means it does not need compilation to binary code before it is run. Python can run an entire script (module) or just a snippet of code. The flexibility allows smaller sections of code or even a single line to be tested or checked for proper syntax.

There are multiple Python Editors that make working with Python easier by using a menu-driven interface and tools for organizing and testing scripts. IDLE (Integrated DeveLopment Environment) comes preinstalled with Python and is the default interactive interpreter for working with Python scripts. Another Python Editor is Jupyter Notebook. ArcGIS Notebooks are derived from Jupyter Notebook that allow a user to work with Python directly from within ArcGIS Pro. This Python editor uses narrative text and visualizations, and produce results from analysis in a geographic context.

An example of a flowchart for a Python script

Also introduced in the first module for GIS Programming are flowcharts for programs. A flowchart outlines an algorithm with the use of symbols and shapes. General rules for flowcharts use ovals for the start and end of a program. Rectangles are used for a task process or executing assignment statements.. Diamonds are used for decisions and parallelograms for I/O statements, when statements are printed or information is obtained from the keyboard. Arrows connect the various shapes indicate the direction of the program.

Flowcharts visualize the behavior of a program. An exercise for Lab this week was to create a flowchart illustrating converting 3 radios to degrees and printing the result. This was based upon the formula of degrees = radians * 180/pi.
The resulting python script from the flowchart was:

#This program calculates degrees from radians
radians = 3
pi = 3.14159
degrees = radians * (180/pi)
print (degrees)

Lastly this week we were tasked to derive the meaning of "The Zen of Python" poem embedded within the Python source code. These are 19 aphorisms alluding to general guidelines used by Python developers. An aphorism is defined as short clever observation intended to express a general truth.

I attempted to make sense out of "The Zen of Python" by making correlations to what was in our textbook "Python Scripting for ARCGIS Pro" but was not satisfied with my interpretation. I ended up researching it and found the article "What's the Zen of Python" on the Real Python web site. The author breaks down the poem into sections with examples of what the aphorisms try to convey. Coupling that analysis with some of my own observations from working with PHP scripts for AARoads, I surmised:

The guidelines allude to the simplicity of Python (syntax) and the ease of learning the programming language. Code should be clear and easily understood. More complex code can be subdivided into more manageable components. Avoid overly nesting code with multiple indentations, but not by using overly long and compacted code placed on a single line. Separate longer code into smaller parts that are easier to parse. Patience can offset premature intuition that saves time and frustration. Functionally should not be added until deemed necessary.

No comments:

Post a Comment