官术网_书友最值得收藏!

Accessing ArcPy with Python

Before you can take advantage of all the geoprocessing functionality provided by ArcPy, you must first import the package into your script. This will always be the first line of code in every geoprocessing script that you write.

Getting ready

ArcPy is a Python site package that is part of the ArcGIS 10 release, and fully encompasses the functionality provided with the arcgis scripting module at ArcGIS 9.2, which further enhances its capabilities. With ArcPy you have access to the geoprocessing tools, extensions, functions, and classes for working with ESRI GIS data. ArcPy provides code-completion and integrated documentation for the modules, classes, and functions. ArcPy can also be integrated with other Python modules to widen the scope of its capabilities. All ArcGIS geoprocessing scripts that you write with Python must first provide a reference to ArcPy.

How to do it…

Follow these steps to import the arcpy site package into the ArcGIS Python window:

  1. Open the c:\ArcpyBook\Ch2\Crime_Ch2.mxd file with ArcMap.
  2. Click on the Python window button to display a shell window where you can write the Python code.
  3. In the Python window, import the arcpy package and press the Enter key on your keyboard. After each statement that you enter in the Python window, you will press the Enter key. You will include this line of code in every single script that you write, so get used to it! This import statement is what gives you access to all the functionality provided by ArcPy.

    Note

    Technically, you don't have to include the import arcpy statement when working inside the ArcMap Python window. It's inherent to this window. However, it is required when creating standalone scripts in IDLE, PythonWin, or any other integrated development environment. It's also a good habit to get into as many of your scripts will ultimately be run as standalone scripts.

    How to do it…
  4. ArcPy also provides code-completion functionality that makes your life as a programmer much easier. On the second line, begin by typing arcpy and then a dot. ArcPy is an object-oriented package, which means that you access the properties and methods of an object using the dot notation. Notice that a drop-down list of available items is provided. These are the tools, functions, classes, and extensions that are available on this particular object. All objects have their own associated items, so the list of items presented will differ depending on the object that you have currently selected:
    How to do it…
  5. This is an auto-filtering list, so as you begin typing the name of the tool, function, class, or extension, the list will be filtered by what you have typed:
    How to do it…
  6. You can choose to have the Python window auto-complete the text for you by selecting an item from the list using your mouse or by using the arrow keys to highlight your choice, and then using the Tab key to enter the command. This auto-completion feature makes you a faster, more efficient programmer. Not only is it easy to use, but it also dramatically cuts down the number of typos in your code.

How it works…

Once the ArcPy module has been imported, you get access to the geoprocessing tools, extensions, functions, and classes for working with ESRI GIS data. One of the most important aspects of ArcPy is that it provides access to all of the geoprocessing tools available, based on the license level of ArcGIS Desktop currently in use. The tools available to your script will vary depending upon whether you are using the ArcGIS Basic, Standard, or Advanced license level, with the Basic level providing the fewest tools and the Advanced level providing a complete set of tools.

主站蜘蛛池模板: 门源| 探索| 临安市| 正蓝旗| 福建省| 昌邑市| 抚松县| 同江市| 玛多县| 竹北市| 伊宁市| 璧山县| 义马市| 凉城县| 泰兴市| 新乐市| 安仁县| 苏尼特右旗| 浦东新区| 曲松县| 厦门市| 谷城县| 顺平县| 卢氏县| 渝中区| 万全县| 新建县| 盐源县| 屯留县| 大新县| 额敏县| 庆城县| 台东市| 金秀| 任丘市| 藁城市| 大荔县| 鄂尔多斯市| 资溪县| 垣曲县| 玉门市|