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

Referencing the current map document

When running a geoprocessing script from the ArcGIS Python window or a custom script tool, you will often need to get a reference to the map document currently loaded in ArcMap. This is typically the first step in your script before you perform geoprocessing operations against layers and tables in a map document. In this recipe, you will learn how to reference the current map document from your Python geoprocessing script.

Getting ready

Before you can actually perform any operations on a map document file, you need to get a reference to it in your Python script. This is done by calling the MapDocument() method on the arcpy.mapping module. You can reference either the currently active document or a document at a specific location on disk. To reference the currently active document, you simply supply the keyword CURRENT as a parameter to the MapDocument() function. This gets the currently active document in ArcMap. The following code example shows how a reference to the currently active document is obtained:

mxd = mapping.MapDocument("CURRENT")

Note

You can only use the CURRENT keyword when running a script from the ArcGIS Python window or a custom script tool. If you attempt to use this keyword when running a script from IDLE or any other development environment, it won't have access to the map document file currently loaded in ArcGIS. I should also point out that the CURRENT keyword is not case sensitive. You could just as easily use current.

To reference a map document on a local or remote drive, simply supply the path to the map document as well as the map document name as a parameter to MapDocument(). For example, you would reference the crime.mxd file in the c:\data folder with the following reference: arcpy.mapping.MapDocument("C:/data/crime.mxd").

How to do it…

Follow these steps to learn how to access the currently active map document in ArcMap:

  1. Open c:\ArcpyBook\Ch3\Crime_Ch3.mxd with ArcMap.
  2. Click on the Python window button from the main ArcMap toolbar.
  3. Import the arcpy.mapping module by typing the following into the Python window:
    import arcpy.mapping as mapping
  4. Reference the currently active document (Crime_Ch3.mxd) and assign the reference to a variable by typing the following into the Python Window below the first line of code that you added in the last step:
    mxd = mapping.MapDocument("CURRENT")
  5. Get the title of the map document and print it out to the shell window. When the script executes, the title of the map document will be printed using the Python print statement:
    print mxd.title
  6. Set a new title for map document:
    mxd.title = "Copy of Crime Project"
  7. Save a copy of the map document file with the saveACopy() method.
    mxd.saveACopy("c:/ArcpyBook/Ch3/crime_copy.mxd")
  8. Run the script .
  9. In ArcMap open the crime_copy.mxd file that you just created and select File | Map Document Properties to view the new title you gave to the map document.

How it works

The MapDocument class has a constructor that creates an instance of this class. In object-oriented programming, an instance is also known as an object . The constructor for MapDocument can accept either the CURRENT keyword or a path to a map document file on a local or remote drive. The constructor creates an object and assigns it to the variable mxd. You can then access the properties and methods available on this object using dot notation. In this particular case, we've printed out the title of the map document file using the MapDocument.title property and we also used the MapDocument.saveACopy() method to save a copy of the map document file.

主站蜘蛛池模板: 宜都市| 象州县| 南宫市| 合肥市| 二连浩特市| 大连市| 城口县| 凌云县| 新乐市| 克拉玛依市| 屏东县| 滦平县| 五华县| 锡林浩特市| 昌邑市| 屏东市| 定州市| 建宁县| 鸡泽县| 阿克陶县| 合江县| 阿克陶县| 剑河县| 阿坝县| 上饶县| 车致| 永州市| 开原市| 沽源县| 鄢陵县| 宣化县| 张家口市| 盐源县| 威信县| 伽师县| 乌拉特前旗| 隆安县| 保山市| 原平市| 靖边县| 印江|