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

Referencing map documents on a disk

In addition to being able to reference the currently active map document file in ArcMap, you can also access map document files that are stored on a local or remote drive by using the MapDocument() constructor. In this recipe, you'll learn how to access these map documents.

Getting ready

As I mentioned earlier, you can also reference a map document file that resides somewhere on your computer or a shared server. This is done simply by providing a path to the file. This is a more versatile way of obtaining a reference to a map document because it can be run outside the ArcGIS Python window. Later, when we will discuss parameters in a script, you'll understand that you can make this path a parameter so that the script is even more versatile, with the ability to input a new path each time it is needed.

How to do it…

Follow these steps to learn how to access a map document stored on a local or remote drive:

  1. Open the IDLE development environment from Start | All Programs | ArcGIS | Python 2.7 | IDLE.
  2. Create a new IDLE script window by navigating to File | New Window from the IDLE shell window.
  3. Import arcpy.mapping:
    import arcpy.mapping as mapping
  4. Reference the copy of the crime map document that you created in the last recipe:
    mxd = mapping.MapDocument("c:/ArcpyBook/Ch2/crime_copy.mxd")
  5. Print the title of the map document:
    print(mxd.title)
  6. Run the script, and you will see the following output:
    Crime Project
    
  7. You can check your work by examining the c:\ArcpyBook\code\Ch2\ReferenceMapDocumentOnDisk.py solution file.

How it works…

The only difference between this recipe and the last one is that we provided a reference to a map document file on a local or remote drive rather than using the CURRENT keyword. This is the recommended way of referencing a map document file unless you know for sure that your geoprocessing script will be run inside ArcGIS, either in the Python window or as a custom script tool.

主站蜘蛛池模板: 仁怀市| 西乌珠穆沁旗| 容城县| 犍为县| 江津市| 偏关县| 紫阳县| 锡林浩特市| 西充县| 大田县| 开远市| 静乐县| 灵宝市| 台山市| 收藏| 巴彦县| 象山县| 金川县| 贺州市| 襄垣县| 泌阳县| 新津县| 丰台区| 万全县| 平谷区| 高阳县| 抚顺市| 锡林浩特市| 通海县| 河池市| 十堰市| 灵宝市| 泽州县| 霞浦县| 凤山市| 云阳县| 东宁县| 八宿县| 武邑县| 澄城县| 东兰县|