0

I want to lable feature class with related table.One of the solution for this goal is writing script in the Expression of label tab ( How To: Label a related table). But i don't want to scripting in Expression.Then it's not as simple as the Esri solution.

My code is working properly but i could not iterate "lblclass.expression" for the row1 and the related table ( row1) is not iterate and just show one value for all labels.How to iterate related table value for labeling ?

import arcpy
table = "table"
listtlabel = []
mxd = arcpy.mapping.MapDocument("CURRENT")
layer = arcpy.mapping.ListLayers(mxd, "Point")[0]
fielddelimi1 = arcpy.AddFieldDelimiters(table, "Code")
sql_exp1 = "{0} = {1}".format(fielddelimi1,309)

if layer.supports("LABELCLASSES"):
    for lblclass in layer.labelClasses:
        with arcpy.da.SearchCursor(table,["Code1","Row"],sql_exp1) as cur1:
                for row1 in cur1:
                    listtlabel.append(row1[0])

                lblclass.expression = '[Code]' + '&'+ str(row1[1])
                str1=','.join(str(e) for e in listtlabel)
                print str1
                with arcpy.da.SearchCursor(layer,"Code2") as cur:
                    for row in cur:
                        if row[0] in listtlabel:
                             lblclass.SQLQuery = '"Code2"' + "IN" + "({0})".format(str1)
lblclass.showClassLabels = True
layer.showLabels = True
arcpy.RefreshActiveView()
del mxd
3

Labeling a layer with the values of stand-alone table (that is associated to the layer with 1-Many relationship). See my accepted answer for this post plus my first response that has links to discussions of the source code.

I will say it again. NEVER use embedded cursors for relating two tables. That is the most inefficient, horribly performing, and memory intensive approach you can take. You need random access to perform relates, which embedded cursors do not support. Dictionaries support random access and solve the problem very elegantly.

  • I red your articlce.It was very interesting but in your solution the codes must copy in the expression editor of label tab but I want to automate labeling in my tool ( python toolbox). I have two input layers to process labeling. Is that possible ? – BBG_GIS Nov 26 '16 at 15:30
  • You would set the labelclass.expression to use my code in you code. It isn't necessary to use separate labelclasses to do the relate Un – Richard Fairhurst Nov 26 '16 at 16:20
  • If the user is providing the related table path and Field list each time in your tool, then you would want to include a file navigator parameter for the table. At 10.3 you can set up a nice field list and Field relationship builder table. Prior to 10.3 it is harder to design an interactive field match parameter. Look for that topic, also look on geonet.esri.com. The Tool can modify the base code – Richard Fairhurst Nov 26 '16 at 16:33
  • The users just providing the paths.I'm using arcgis 10.3. Thank you Richard – BBG_GIS Nov 26 '16 at 16:38
  • If the user is providing the paths you are assuming they will use tables that you already know the relationship key field name and related field they want to use. You may be able to control the table navigator based on an assumed field key, but the tool would be more flexible if the user could choose the related field or fields to list. More validation is needed as you add flexibility. Anyway, python can make the necessary code substitutions in the labelclass.expression – Richard Fairhurst Nov 26 '16 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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