Select to view content in your preferred language

Working with labels using arcpy

597
4
Jump to solution
01-30-2025 06:00 PM
ColeNelson
Emerging Contributor

I am working on a script/geoprocessing tool where, once a month, my users can export the feature classes they need from an Enterprise Geodatabase to a File Geodatabase.  So they can essentially do their analysis without impacting the source data. 

I aim to create a tool that extracts the feature classes and adds them to the map I did.  Then, I want to symbolize the feature class, which I've also gotten working on.  However, now I am trying to label the features for them, and I can get it to work, but I was trying to do more with it. 

Each protected area polygon has a NAME and a GRADEID.  I want to concatenate those two fields so it reads "Protected Area Name: GRADEID #".  The code I have so far will turn on the labels for either the Protected area name or the Grade ID, but it doesn't return anything when I try to return both.  

Here is a sample of the table with the names and grades.

Screenshot 2025-01-30 185547.png

Here is what my map currently looks like.

Just the GradIDsJust the GradIDs

Finally here is my code so far.

import arcpy
import arcpy.mp as map
arcpy.env.overwriteOutput = True

arcpy.env.workspace = arcpy.GetParameterAsText(0)

arcpy.conversion.FeatureClassToGeodatabase(
    Input_Features=r"C:\PPCAnalysis\PPCAnalyis\ISRB_DATA.sde\DBO.FireStations;C:\PPCAnalysis\PPCAnalyis\ISRB_DATA.sde\DBO.Hydrants;C:\PPCAnalysis\PPCAnalyis\ISRB_DATA.sde\DBO.Idaho;C:\PPCAnalysis\PPCAnalyis\ISRB_DATA.sde\DBO.ProtectedAreas",
    Output_Geodatabase=r"C:\PPCAnalysis\PPCAnalyis\PPC_AnalyisisData.gdb"
)

arcpy.env.workspace = r"C:\PPCAnalysis\PPCAnalyis\PPC_AnalyisisData.gdb"

aprx = map.ArcGISProject("CURRENT")
for m in aprx.listMaps("PPCAnalyisData"):
    m.addDataFromPath(r"C:\PPCAnalysis\PPCAnalyis\PPC_AnalyisisData.gdb\FireStations")
    m.addDataFromPath(r"C:\PPCAnalysis\PPCAnalyis\PPC_AnalyisisData.gdb\Hydrants")
    m.addDataFromPath(r"C:\PPCAnalysis\PPCAnalyis\PPC_AnalyisisData.gdb\Idaho")
    m.addDataFromPath(r"C:\PPCAnalysis\PPCAnalyis\PPC_AnalyisisData.gdb\ProtectedAreas")

for m in aprx.listMaps("PPCAnalyisData"):
    for lyr in m.listLayers("ProtectedAreas"):
        if lyr.isFeatureLayer:
            lyr.transparency = 50
            sym = lyr.symbology
            sym.updateRenderer('UniqueValueRenderer')
            sym.renderer.fields = ["NAME"] 
            lyr.symbology = sym

updatedProtectedAreas = "ProtectedAreas"

m = aprx.listMaps("PPCAnalyisData")[0]

for m in aprx.listMaps("PPCAnalyisData"):
    for lyr in m.listLayers("ProtectedAreas"):
        lblClass = lyr.listLabelClasses()[0]
        print(lblClass.name)
        lblClass.expression = "$feature.GRADID"  
        lyr.showLabels = True

Any suggestions on how I can get the names and the grades to print together would be great. Thank you.

0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

lblClass.expression = '$feature.Area_Name +": " +$feature.GRADEID'

should be something like this, I think?

View solution in original post

0 Kudos
4 Replies
AlfredBaldenweck
MVP Regular Contributor

lblClass.expression = '$feature.Area_Name +": " +$feature.GRADEID'

should be something like this, I think?

0 Kudos
ColeNelson
Emerging Contributor

I tried that, and nothing happened.

0 Kudos
ColeNelson
Emerging Contributor

Whoops I typed in a field name wrong, it's working now😁

 

AlfredBaldenweck
MVP Regular Contributor

Lol glad it worked.

0 Kudos