Important Python Modules for GIS Analysis
Modules, or code libraries that can be called by a script to increase its programming potential, are either built into Python or are created by third parties and added later to Python. Most of these are written in Python, but a number of them are also written in other programming languages and then wrapped in Python to make them available within Python scripts. Modules are also used to make other programs available to Python, such as the tools built in Microsoft Word.
The ArcPy module
The ArcPy module is both a wrapper module used to interact with the ArcGIS tools, which are then executed by ArcGIS in its internal code format, and a code base that allows for additional control of geospatial analyses and map production. ArcPy is used to control the tools in ArcToolbox, but the tools have not been rewritten in Python; instead, we are able to use the ArcGIS tools using ArcPy. ArcPy also gives us the ability to control ArcGIS Map Documents( MXDs) and the objects that MXDs include: legends, titles, images, layers, and the map view itself. ArcPy also has tools that are not available in ArcToolbox. The most powerful of these are the data cursors, especially the new Data Analysis Cursors that create a more Pythonic interface with GIS data. The data cursors, covered extensively in Chapters 5, ArcPy Cursors: Search, Insert and Update and Chapter 6, Working with ArcPy Geometry Objects are very useful to extract rows of data from data sources for analysis.
The ability to control geospatial analyses using ArcPy allows for the integration of ArcGIS tools into workflows that contain other powerful Python modules. Python's glue language abilities increase the usefulness of ArcGIS by reducing the need to treat geospatial data in a special manner.
The Operating System (OS) module
The OS module, part of the standard library, allows Python to access operating system functionality. A common use of the module is to use the os.path
method to control file paths by dividing them into directory paths (that is, folders) and base paths (that is, files). There is also a useful method, os.walk
, which will walk-through a directory and return all files within the folders and subfolders. The OS module is accessed constantly when performing GIS analysis.
The Python System (SYS) module
The sys module, part of the standard library, refers to the Python installation itself. It has a number of methods that will get information about the version of Python installed, as well as information about the script and any arguments (or parameters) supplied to the script, using the sys.argv
method. The sys.path
method is very useful to append the Python file path; practically, this means that folders containing scripts can be referenced by other scripts to make the functions they contain importable to other scripts.
The XLRD and XLWT modules
The XLRD
and XLWT
modules are used to read and write Excel spreadsheets, respectively. The modules can be very useful to extract data from legacy spreadsheets and convert them into usable data for GIS analysis, or to write analysis results when a geospatial analysis is completed. They are not part of the Python standard library, but are installed along with ArcGIS 10.2 and Python 2.7.
Commonly used built-in functions
There are a number of built-in functions that we will use throughout the book. The main ones are listed as follows:
str
: The string function is used to convert any other type of data into a stringint
: The integer function is used to convert a string or float into an integer. To not create an error, any string passed to the integer function must be a number such as 1.float
: The float function is used to convert a string or an integer into a float, much like the integer function.
Commonly used standard library modules
The following standard library modules must be imported:
datetime
: The datetime module is used to get information about the date and time, and convert string dates into Python dates.math
: The math module is used for higher level math functions that are necessary at times, such as getting a value for Pi or calculating the square of a number.string
: The string module is used for string manipulations.csv
: The CSV module is used to create and edit comma-separated value type files.
Check out https://docs.python.org/2/library for a complete list of the built-in modules in the standard library.