Heb je de bedoeling gehad om de ArcMap <\/SPAN>Python window<\/A> uit te proberen, maar weet je gewoon niet hoe te beginnen? Nou, de vijf codefragmenten hieronder zijn een geweldig hulpmiddel om vertrouwd te raken met het Python window en de <\/SPAN>arcpy.mapping module<\/A>. Maak een kopie van een MXD-bestand en verplaats het ergens om deze fragmenten uit te proberen. Je zult snel zien hoe nuttig een paar Python-trucs kunnen zijn voor kaartbeheer taken.<\/SPAN><\/P>Elk fragment hieronder werkt met ArcMap 10.x, een bestaand kaartdocument (MXD-bestand), arcpy.mapping module, en Python 2.x.<\/LI><\/UL>Je hoeft geen Python-syntaxis te kennen om deze fragmenten te gebruiken. Voer gewoon de code in het Python window in en bekijk de resultaten.<\/P>Fragment 1: Controleer Laag Definitie Queries<\/STRONG><\/SPAN><\/P>Stel dat je moet controleren of er lagen in je kaartdocument een definitiequery hebben toegepast, en zo ja, bekijk dan de definitiequeries. Het openen van het dialoogvenster Laageigenschappen voor elke laag is vervelend.<\/P> <\/P>Oplossing: Open in plaats daarvan het Python window en voer onderstaande code uit. De naam en definitiequery van alle kaartlagen worden "afgedrukt" (d.w.z. weergegeven in het Python window).<\/P>
<\/P>Code Uitleg<\/STRONG><\/SPAN><\/P>De eerste regel code maakt een variabele genaamd mxd<\/STRONG><\/SPAN> die verwijst naar het geopende kaartdocument met behulp van het <\/SPAN>CURRENT keyword<\/A>.<\/SPAN><\/LI>De ListLayers<\/A> functie wordt gebruikt om een lijst te maken van alle lagen in het kaartdocument en de for<\/STRONG><\/SPAN> lus wordt gebruikt om door elke laag in de kaart te itereren.<\/LI>De <\/SPAN>Layer class<\/A> heeft veel <\/SPAN>eigenschappen<\/A> die toegankelijk zijn, inclusief de definitiequery voor een laag. Omdat niet alle laagtypen alle eigenschappen ondersteunen, wordt de methode gebruikt om te testen welke eigenschappen een laag ondersteunt. The last line of code prints (displays) the name of each layer and its definition query.
Snippet 2: Fix Broken Links
At some point, you have probably moved map data from one workspace to another. Have you ever seen the dreaded red exclamation marks?

If the path to map layers has changed, with just a few lines of code you can quickly find the old workspace path and replace it with the new workspace path for all layers and tables in your map document at once. Run the following lines of code in the Python window (be sure to update the paths to reflect your old and new workspaces).

Code Breakdown
- The first line of code is the same as the previous example, creating the variable mxd that references the open map document.
- The findAndReplaceWorkspacePaths method is used to search for layers that have the old workspace (first parameter) and update the workspace to the new workspace (second parameter) for all the layers and tables in the map document.
- The RefreshActiveView function is used to see the changes in the table of contents and on the map.
Snippet 3: Identify Layers with a Broken Data Source
Speaking of those red exclamation marks, you can also use arcpy.mapping to print out the layers that have broken links using the code below.

Code Breakdown
- The first line of code creates a variable called mxd that references the open map document, just like the examples above.
- Like the first example, the ListLayers function creates a list of all the layers in the map document.
- Next, the for loop is used to iterate through each layer in the map, printing out the name and isBroken layer properties for each.
Snippet 4: Inventory Layer Data Sources
If you need to know the data source for all the layers in your map, but dont want to open the Layer Properties dialog box for each, simply run the lines of code below in the Python window to print out the names and the workspace path of the layers.

Code Breakdown
- Like all the examples above, the first line of code creates the mxd variable to reference the open map document (are you getting the hang of it?).
- Again, the ListLayers function creates a list of all the layers in the map document that you can iterate through with the for loop.
In this example, you print out the layer properties name and workspacePath for each layer in your map. Because not all layers have a workspace path (for example, web services), the supports method is used.
Snippet 5: Combine PDF Documents
Maybe youre creating a map book or maybe you have a few PDF documents you need to combine (like your expense report and receipts). You can use arcpy.mapping and the PDFDocument class to manipulate PDF documents. Run the code below to combine two PDF documents.

Code Breakdown
- The first line of code creates a variable called pdf that references an existing PDF document.
- The appendPages method is used to add pages from a PDF to the end of the referenced PDF.
- To save the changes, the saveAndClose method is used.
Wil je meer leren?
Als je meer wilt leren over werken met het ArcMap Python window of het maken van zelfstandige Python-scripts, worden deze bronnen aanbevolen:
Dit bericht is bijgedragen door Esri-instructeur @Brittney White.