Python
Python is a general-purpose high-level programming language, designed to be easily read.
Python runs on Windows, Linux/Unix, Mac OS X, and has been ported to the Java and .NET virtual machines. [1]
Contents
Python compared to other languages
Python is said to be much shorter than other scripting languages such as Java and C++, but it requires longer run time, which can slow things down in large programs. Python's code is much more flexible as far as syntax goes compared to these "lower-level" languages. Many sources say that Python is 3-5 times shorter than Java and over 5 times shorter than C++.[2] There is no definite say on which program is better. It is mostly a matter of preference and depends largely on the type of programs that are being written. One of the great advantages to Python is that it is free due to its open source license. It is also transferrable from multiple operating systems including Windows, Mac OS X, and Linux/Unix. It is also "ported to the Java and .NET virtual machines."[3]
"Python can be written quickly and maintained easier than Java." However, it is important to keep in mind that, “Programming languages are tools, and different tools are appropriate for different jobs.”[4]
Python in ArcGIS
Python was introduced to ArcGIS with version 9.0. A full ArcGIS installation includes Python, its standard libraries, and the NumPy package. Starting with ArcGIS 9.2, a compatible version of PythonWin is included in the ArcGIS distribution, but must be installed separately.
ESRI recommends using the version of Python (and additional packages) shipped together with the specific version of ArcGIS.[5]
Python is used as the primary ArcGIS scripting language to perform geoprocessing tasks. It is capable of accessing all the ArcToolbox tools as well as the methods on the Geoprocessor Programming Model.
Python support in different versions of ArcGIS
The geoprocessor python object is accessed differently depending on the version of ArcGIS:
ArcGIS 9.0/9.1
These versions of ArcGIS use Python 2.1 with the PythonWin[6] package to access a Geoprocessor using a COM interface:
import win32com # PythonWin gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
The GpDispatch COM interface is available in later versions of ArcGIS, though it is only available to Python if PythonWin is installed. [7] GpDispatch provides access to geoprocessing using any language that supports COM.
ArcGIS 9.2
This version of ArcGIS uses Python 2.4 and brought a major change to how the Geoprocessor was created. The win32com.client is no longer needed, replaced by the Python-native (non-COM) arcgisscripting
module. This both provides better performance and allows platform independence; Python geoprocessing scripts using the arcgisscripting
module are supported on non-Windows versions of ArcGIS Server.
import arcgisscripting gp = arcgisscripting.create()
ArcGIS 9.3/9.3.1
These versions of ArcGIS use Python 2.5.1 and can be used with both the 9.3 version of arcgisscripting
or the original 9.2 version of arcgisscripting
.
import arcgisscripting gp = arcgisscripting.create() # 9.2 (default for ArcGIS 9.2,9.3) gp = arcgisscripting.create(9.3) # 9.3
You can determine the version of the geoprocessor from its ScriptVersion property:
print "GP version: " + str(gp.ScriptVersion)
ArcGIS 10
This version of ArcGIS adds the new arcpy
module. ArcPy is a site-package that builds on (and is a successor to) the arcgisscripting module. Its goal is to create the corner-stone for a useful and productive way to perform data analysis, data conversion, data management, and map automation with Python.
import arcpy
The older versions of the geoprocessor (GpDispatch and the arcgisscripting
module) are still supported to allow for forward compatibility of Python scripts.
Python Map Algebra in 10.0
If a Spatial Analyst license is available, ArcPy can be used to create Map Algebra expressions to perform complex raster processing tasks.[8] For example:
import arcpy arcpy.CheckOutExtension("spatial") from arcpy.sa import * rasElevMeters = Raster("E:\\work\\elevfeet") * 0.3048
Python and GDAL
The GDAL/OGR libraries have python bindings. This means that you can use all of the GDAL/OGR functions and object classes via python. Being open source software, you don't have to worry about license availability, or not having the latest versions.
Installing the GDAL bindings
There are install instructions and tutorials at http://trac.osgeo.org/gdal/wiki/GdalOgrInPython, for both linux and windows systems.
Pre-packaged python+GDAL bundles
There are two packages that include both a Python iPhone photography interpreter and the GDAL/OGR bindings: OSGeo4Win and FWTools.
Python and GDAL are also included on the OSGeo Live DVDs, which can be tried out without the need for any installation.
References
- ↑ www.python.org
- ↑ [1]
- ↑ [2]
- ↑ [3]
- ↑ ESRI KB 31909: FAQ: Can both Python versions 2.1 and 2.4.1 be installed for use with ArcGIS 9.2?
- ↑ PythonWin distribution at sourceforge.net
- ↑ ESRI KB 31910: HowTo: Install PythonWin 2.4.1 and make correct file associations to work with ArcGIS 9.2
- ↑ Arc 10.0 Documentation: What is Map Algebra?
Further Reading
- Python.org
- The Python tutorial
- Introduction to Programming Using Python for new Python users
- Python Help in ArcGIS 10
- Lutz and Ascher, Learning Python, 3d edition (O'Reilly)
- Another Python tutorial with slides.
- Garrard, Chris. Geoprocessing with Python. Manning Publications. ISBN 9781617292149.