3

I'm trying to automatically generate annotations using arcpy from a set of MXD files with preorganised labels (all settings done, ready to export the labels). I'm using ArcGIS 10.3 with an Advanced Licence. All I'm looking to do is scripting the process of right-clicking the layer selecting an output and exporting the labels to annotations (see screenshot below):

screen_1

screen_2

So far, I found various similar questions that all point to the arcpy.TilesLabelsToAnnotations_cartography() tool.

So I tried using the tool as follows, which for some reason does not end in an error (green checkbox appears after only a few seconds) but simply produces nothing at all...

Where am I going wrong?

Any suggestions for changes?

Are there easier ways of achieving my goal?

Ideally I don't even want a tile for exporting, since I want to convert all labels, no matter of the extent. Sidenote: Some of the resulting annotation feature classes are quite large (300+ MB) when exported manually, I don't know whether this is of any relevance for the tool.

import arcpy
import sys

MXD_folder = r"C:\Users\sutter\Desktop\GIS_temp\stack_test"  
outGDB = r"C:\Users\sutter\Desktop\GIS_temp\stack_test\testOut.gdb"


arcpy.env.workspace = MXD_folder
arcpy.env.scratchworkspace = MXD_folder
arcpy.env.parallelProcessingFactor = "100%"
arcpy.env.overwriteOutput = True

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# 2. Create surrounding polygon:

spatRef = arcpy.SpatialReference(2056) # Swiss LV95
arcpy.CreateFeatureclass_management(MXD_folder, "tempTile", "POLYGON", "", "", "", spatRef)  
tempTile = r"{}\{}.shp".format(MXD_folder, "tempTile")
# Setup Extent:
array = arcpy.Array([arcpy.Point(2600000, 1200000),
                     arcpy.Point(2600000, 1210000),
                     arcpy.Point(2610000, 1210000),
                     arcpy.Point(2610000, 1200000)
                     ])
polygon = arcpy.Polygon(array)
with arcpy.da.InsertCursor(tempTile, ['SHAPE@']) as cursor:
    cursor.insertRow([polygon])
# Create Feature Layer for following tools:
arcpy.MakeFeatureLayer_management(tempTile, "tile")

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# 3. Find all files for processing:
mxdNameList = arcpy.ListFiles("*.mxd")


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# 4. Export to annotations for all MXD files:

for i in range(len(mxdNameList)):
    mxd = r"{}\{}".format(MXD_folder, mxdNameList[i])

    # List Layers in MXD. If less or more than 4, exit. If 4, activate labels
    mxdPy = arcpy.mapping.MapDocument(mxd)
    layers = arcpy.mapping.ListLayers(mxdPy)
    if len(layers) != 4:
        sys.exit()
    else:
        for layer in layers:
            layer.showLabels = True
        arcpy.RefreshActiveView()
        mxdPy.save()
    del mxdPy

    # Get Reference Scale from MXD Name:
    refScale = int(mxdNameList[i].split("_")[-1].split(".")[0])

    # Define Dataframe name:
    df = "Layers"

    # Start conversion of all layers to annotations:
    arcpy.TiledLabelsToAnnotation_cartography(mxd, df, "tile", outGDB, "group", "anno", str(refScale), "", "Id", "", "", "STANDARD", "GENERATE_UNPLACED_ANNOTATION")
2
  • 1
    Sys.exit() looks suspicious. If condition met on 1st mxd nothing happens. Replace with continue?
    – FelixIP
    May 19, 2018 at 23:52
  • yes, ´continue´ might be the better choice.
    – dru87
    May 22, 2018 at 5:55

1 Answer 1

3
+50

I think I found the answer to your question is in this documentation : http://desktop.arcgis.com/fr/arcmap/10.3/tools/defense-mapping-toolbox/convert-labels-to-annotation.htm

# Script variables
mxd = 'C:/Example/Untitled.mxd'
annoFeatures = 'C:/Example/example.gdb/CARTO/MTM50_Anno'

# Run Convert Labels to Annotation tool
arcpy.ConvertLabelsToAnnotation_defense(mxd, annoFeatures)

This tool looks like it does exactly what you want to do. However, it requires the module "Defense Mapping". I don't have it so I could not try it out for you. If you don't have it either, I can't think of a way around it using arcpy, as it seems it is the solution offered by arcpy, and I doubt it provides another one.

1
  • Thanks for your answer T Dog. Neither do I have the Defense Mapping module availbe, therefore your solution remains untested. However I'm still going to accept your answer.
    – dru87
    May 23, 2018 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.