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):
So far, I found various similar questions that all point to the arcpy.TilesLabelsToAnnotations_cartography()
tool.
- Labeling features and converting them to annotations with ArcPy?
- Automate converting labels to annotation in ArcMap at multiple scales?
- Exporting and editing annotation feature class using ArcPy?
- Labels to Annotations in ModelBuilder --> this tool looks most promising, but is not available for ArcGIS 10.3...
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")