Introducing Python - Esri

Exercise 1

Introducing Python

Part 1

Learning the fundamentals of Python

and geoprocessing

Install the exercise data

Exercise data is provided online on the Esri Press book resources website at . Copy the exercise data to a hard drive.. The default path is C:\EsriPress\Python, but you can change this during installation.

Check ArcGIS settings

You will first determine the version of the ArcGIS for Desktop software that is installed on your computer, including any available extensions.

1 Close any ArcGIS for Desktop programs you may have open (the ArcMap application, the ArcCatalog application, and so on).

2 On the taskbar, click the Start button, and then on the Start menu, click All Programs > ArcGIS > ArcGIS Administrator. This brings up the ArcGIS configuration information, which should read something like the figure.

In this example, the version of ArcGIS for Desktop is 10.1, which is the same version used for the exercises in this book. If you are using version 10.1 with a recent service pack, there should be no noticeable difference in how the Python code works. Most if not all of the Python scripting in the book will also work in ArcGIS 10.2. If you are using version 10.0, some of the Python code in the exercises

Python Scripting for ArcGIS Exercise 1: Introducing Python

will not work correctly because additional Python functionality was introduced in 10.1.

If you are using ArcGIS for Desktop 9.3.1 or earlier, the code in the exercises will not work. The ArcPy site package was introduced in version 10.0, and this includes Python functionality that was not available in earlier versions.

Next, you will determine the license level and any available licenses.

3 On the ArcGIS Administrator dialog box, click Desktop > Availability. This brings up a list of the available licenses, which should look something like the figure.

Check ArcGIS settings 2

The first entry shows the product level--that is, ArcGIS for Desktop Basic, ArcGIS for Desktop Standard, or ArcGIS for Desktop Advanced, formerly known as ArcView, ArcEditor, and ArcInfo, respectively. Whatever your product level, the code in the exercises will work correctly.

The following entries show the extensions that are installed and licensed. The only required extension for these exercises is Spatial Analyst, which is used in exercise 9. If you do not have ArcGIS Spatial Analyst installed and licensed, the code in exercise 9 will not work.

Next, you will select one of the options in ArcCatalog to make it easier to recognize file types.

4 Close ArcGIS Administrator.

Note: If you do not have the ArcGIS Spatial Analyst extension, you may want to consider installing the evaluation software available at . trydesktop because it includes ArcGIS Spatial Analyst.

5 On the taskbar, click the Start button, and then, on the Start menu, click All Programs > ArcGIS > ArcCatalog 10.1.

6 On the ArcCatalog menu bar, click Customize > ArcCatalog Options.

Python Scripting for ArcGIS Exercise 1: Introducing Python

7 On the General tab, make sure the "Hide file extensions" check box is cleared.

8 Click OK to close the ArcCatalog Options dialog box.

>>> TIP

To make it easier to work with the exercise data in this book, you can create a connection to the data folder. In the Catalog window, click the Connect to Folder button, browse to C:\EsriPress\Python\Data, and click OK. The shortcut will now appear under Folder Connections in the Catalog tree.

9 Browse to where the data for the exercises is installed--that is, C:\EsriPress\Python\Data. Then open one of the folders that has data files in it--for example, Exercise02.

Notice that the files are now shown with their file extension, such as basin.shp instead of just basin.

10 Close ArcCatalog.

Check the Python version 3

Check the Python version

You will now determine what version of Python is installed on your computer.

1 On the taskbar, click the Start button, and then, on the Start menu, click All Programs > ArcGIS > Python 2.7 > IDLE (Python GUI). This brings up the default Python editor, as shown in the figure.

Python Scripting for ArcGIS Exercise 1: Introducing Python

ArcGIS 10.1 is designed to work with Python 2.7. At the final release of ArcGIS 10.1, the most recent production version of Python 2.7 was 2.7.2, which is the version that is installed as part of a typical ArcGIS 10.1 installation. Versions of Python 2.7 will continue to evolve, but the changes between these versions are so minor that ArcGIS 10.1 is expected to work with all versions of Python 2.7. The code in the exercises will generally also work fine for other 2.x versions of Python, including 2.6. However, when working with ArcGIS for Desktop, it is recommended that you run the version of Python that is installed with ArcGIS for Desktop. The code in the exercises will not work with Python 3.x.

Note: It is possible to have multiple versions of Python on the same Windows computer. If you already have a different version of Python installed in addition to the one that is installed with ArcGIS for Desktop, be sure to use the version that is installed with ArcGIS for Desktop to write and run the code in these exercises.

2 Close IDLE.

Install PythonWin

The exercises in the book use PythonWin as the editor application to work with Python. The following steps walk you through the installation of PythonWin.

When you install PythonWin, it determines the version of Python that is installed on your computer. The installation of ArcGIS 10.1 installs Python version 2.7.2. The PythonWin installation then assumes you have a program folder called Python 2.7--however, the ArcGIS 10.1 installation creates a program folder called ArcGIS\Python 2.7. Therefore, you first need to create a program folder simply called Python 2.7.

1 For Windows XP: On the taskbar, right-click the Start button, and then click Open All Users.

For Windows 7: On the taskbar, click the Start button, and then, on the Start menu, right-click All Programs and click Open All Users.

2 Double-click the Programs folder.

3 Right-click in the Programs folder and click New > Folder. Name the folder Python 2.7.

4 Close the Programs window.

Install PythonWin 4

Python Scripting for ArcGIS Exercise 1: Introducing Python

Now it is time to find the installation files for PythonWin.

Option 1: Download exercise data

? Install the online exercise data that comes with the book. Start Windows, and then click the Start button. Then click My Computer and browse to the drive where you have installed the data.

? Browse to the C:\EsriPress\Python\Data\Exercise01 folder. This folder contains the file pywin32-214.win32-py2.7.exe.

Option 2: Download ArcGIS software

? Download trial software at . Start Windows, and then click the Start button. Click My Computer and browse to the drive where you have installed the data.

? Browse to the folder Desktop > Python. This folder contains the file pywin32-214.win32-py2.7.exe.

Option 3: Download from SourceForge

? Navigate to the following URL: pywin32/files/pywin32/Build%20214.

? Download the file pywin32-214.win32-py2.7.exe to a folder on your computer.

Notice that SourceForge contains many different versions of PythonWin, one for every version of Python. If you obtain the PythonWin installation files from SourceForge, be sure to select the same version as the one that ships with ArcGIS.

Once you have found the correct installation file, you can proceed with the installation.

5 Double-click pywin32-214.win32-py2.7.exe to launch the PythonWin installation.

Install PythonWin 5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download