Select to view content in your preferred language

Save a group layer using arcpy (output from Convert Labels to Annotations tool)

575
3
10-19-2023 06:31 AM
Labels (1)
ASw93
by
Occasional Contributor

I am running the "Convert Labels To Annotation" tool in ArcGIS Pro using the Python window. I need to save the output group layer to it's own lyrx file.

According to the tool documentation, it should be fairly simple: "when working in the Catalog pane, the Python window, or a stand-alone Python script, you can use the Save To Layer File tool to write the output group layer to a layer file." However, I am struggling to understand where the group layer actually is to then be able to save it.

I have following code:

 

for area in os.listdir(outfolder):

    if area == "test":
        # Find the aprx file in each plan
        project = outfolder + "\\" + area + "\\" + area + ".aprx"
        annoLyr = outfolder + "\\" + area + "\\" + area + "_Annos.lyrx"
        print(project)

        print("Opening ArcGIS Project for", area)
        aprx = arcpy.mp.ArcGISProject(project)

        for map in aprx.listMaps():
            if "_10" in map.name:
                print("Running on", map.name)

                for sc in scales:

                    print("Converting all labels to annotation at scale {}".format(sc))

                    #Convert all labels in entire map to Annotation
                    arcpy.cartography.ConvertLabelsToAnnotation(
                    input_map=map, 
                    conversion_scale=sc, 
                    output_geodatabase=aprx.defaultGeodatabase, 
                    anno_suffix="Anno",
                    extent="DEFAULT",
                    generate_unplaced="GENERATE_UNPLACED",
                    require_symbol_id="NO_REQUIRE_ID",
                    feature_linked="STANDARD",
                    auto_create="AUTO_CREATE",
                    update_on_shape_change="SHAPE_UPDATE",
                    output_group_layer="Annotations",
                    which_layers="ALL_LAYERS",
                    single_layer=None,
                    multiple_feature_classes="FEATURE_CLASS_PER_FEATURE_LAYER",
                    merge_label_classes="NO_MERGE_LABEL_CLASS"
                    )
                arcpy.management.SaveToLayerFile("Annotations", annoLyr, "RELATIVE", "CURRENT")
                print("Labels have been converted to annotations.")

 

 

The SaveToLayerFile doesn't work because it cannot find the "Annotations" group layer (my intended output from line 32. 

I also tried listing the layers in the map object but the group layer "Annotations" hasn't been added to the map, so I cannot export it via that route either.

Anyone know where the group layer from line 32 is being output, so I can retrieve it using arcpy?

0 Kudos
3 Replies
AlfredBaldenweck
MVP Regular Contributor

Sorry for that email you got and then the deleted post. I hadn't read it fully.

If you're using the Python window, that makes it easy. (This does not work for scripting tools, only notebooks or python window)

just add this line 

arcpy.env.addOutputsToMap = True

somewhere above the loop.

It will add the annotations layer to your active map. Then, just search your active map for the group layer and there you are.

 

aprx = arcpy.mp.ArcGISProject("CURRENT")

map = aprx.listMaps("SQL")[0]
arcpy.env.addOutputsToMap = True
#Convert all labels in entire map to Annotation
arcpy.cartography.ConvertLabelsToAnnotation(
input_map=map, 
conversion_scale= "1:24000", 
output_geodatabase=aprx.defaultGeodatabase, 
anno_suffix="Anno",
extent="DEFAULT",
generate_unplaced="GENERATE_UNPLACED",
require_symbol_id="NO_REQUIRE_ID",
feature_linked="STANDARD",
auto_create="AUTO_CREATE",
update_on_shape_change="SHAPE_UPDATE",
output_group_layer="Annotations",
which_layers="ALL_LAYERS",
single_layer=None,
multiple_feature_classes="FEATURE_CLASS_PER_FEATURE_LAYER",
merge_label_classes="NO_MERGE_LABEL_CLASS"
)

grouplyr = aprx.activeMap.listLayers("Annotations")[0]
grouplyr.saveACopy(r'W:\Desktop\Desktop\example.lyrx')

 

 

0 Kudos
ASw93
by
Occasional Contributor

Thank you for the response - the addOutputsToMap seemed like a winner but unfortunately it hasn't solved the issue.

I tested the addOutputsToMap then listed the layers in the map but it still didn't work. I also saved the aprx after running it to check if it was there and unfortunately the group layer still isn't created.

I noticed in your example, you are using the "CURRENT" Project file whereas I am not, I am iterating through a directory of them, and I think this is the problem. It seems to work fine if it's the current project.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Okay yeah, it was an issue.

Basically it was getting added to your map, but not to the map from which the annotations came.

So what I did was hard-code in the path of the project I'm running this code in (aprxPrime), as well as your active map.

Another thing to save on an indent level is to use listMaps to filter the maps. The "*" is a wildcard, so in my example I'm looking for any map that contains the string "Geo" in it. If I wanted maps that started with "Geo", I'd use listMaps("*Geo"), instead.

I tried to throw in a line to remove the annotation group layer from your map each time so they don't clog it up, but it seems to be actively resisting that, so you may just have to do it manually?

This should at least give you a start.

arcpy.env.addOutputsToMap = True

# Path to your current project, where your active map is.
aprxPrime =arcpy.mp.ArcGISProject(r"W:\...\LaunchPad.aprx")
activeMP = aprxPrime.activeMap

for project in [r'W:\...\MyProject.aprx']:
    print("Opening ArcGIS Project for", "area")
    aprx = arcpy.mp.ArcGISProject(project)
    
    for map in aprx.listMaps("*Geo*"):
        # if "_10" in map.name:
        print("Running on", map.name)
        scales = ["1:24000"]
        for sc in scales:

            print("Converting all labels to annotation at scale {}".format(sc))

            #Convert all labels in entire map to Annotation
            arcpy.cartography.ConvertLabelsToAnnotation(
            input_map=map, 
            conversion_scale=sc, 
            output_geodatabase=aprx.defaultGeodatabase, 
            anno_suffix="Anno",
            extent="DEFAULT",
            generate_unplaced="GENERATE_UNPLACED",
            require_symbol_id="NO_REQUIRE_ID",
            feature_linked="STANDARD",
            auto_create="AUTO_CREATE",
            update_on_shape_change="SHAPE_UPDATE",
            output_group_layer="Annotations",
            which_layers="ALL_LAYERS",
            single_layer=None,
            multiple_feature_classes="FEATURE_CLASS_PER_FEATURE_LAYER",
            merge_label_classes="NO_MERGE_LABEL_CLASS"
            )
        # Search the active map of your launching project for the 
        # annotations.
        grouplyr = activeMP.listLayers("Annotations")[0]
        grouplyr.saveACopy(r'W:\Desktop\Desktop\example.lyrx')
        print("Labels have been converted to annotations.")

 

0 Kudos