python - label from a related table

5435
9
Jump to solution
07-24-2014 01:04 PM
deleted-user-MS0lE1F_0-sy
Occasional Contributor

Anyone know how to label from a related table in python?

I figured out how to using VBScript form here: https://gisnuts.com/terra/blog/2012/05/16/label-features-with-attributes-from-related-records

Cannot figure it out in python.

If it can be done in VBScript I am hoping that means it can be accomplished in Python.

0 Kudos
1 Solution

Accepted Solutions
ThomasStanley-Jones
New Contributor III

This is probably what you're looking for:

def findLabel(relatefieldoflayer):

       strWhereClause = '"&RelateFieldOfTable&" = \'{0}\''.format(relatefieldoflayer)

       strpTable = &PathToRelatedTable&

       cursor = arcpy.da.SearchCursor(strpTable, &LableField&, strWhereClause)

       result = ""

       for row in cursor:

            strLabel = str(row[0])

            result += strLabel + "\n"

       return result.rstrip()

View solution in original post

9 Replies
DanPatterson_Retired
MVP Emeritus

There are some examples of label expression using python in the help files

0 Kudos
deleted-user-MS0lE1F_0-sy
Occasional Contributor

Dan,

    Thank you for taking time to review and respond.  Yes, you are correct, it will take using an advanced expression.  If you click on my link you will see an example of it in VBScript.  What I cannot figure out is how to write that same advance expression in python.

0 Kudos
ThomasStanley-Jones
New Contributor III

This is probably what you're looking for:

def findLabel(relatefieldoflayer):

       strWhereClause = '"&RelateFieldOfTable&" = \'{0}\''.format(relatefieldoflayer)

       strpTable = &PathToRelatedTable&

       cursor = arcpy.da.SearchCursor(strpTable, &LableField&, strWhereClause)

       result = ""

       for row in cursor:

            strLabel = str(row[0])

            result += strLabel + "\n"

       return result.rstrip()

deleted-user-MS0lE1F_0-sy
Occasional Contributor

That is it.  Thank you!  Just one minor thing in case someone else is looking to do this, the f in FindLabel should be a capital F.   Also when filling in the spots relatefieldoflayer and PathToRelatedTable don't forget your " " and [ ]s!

def FindLabel( [relatefieldoflayer] 😞

       strWhereClause = '"RelateFieldOfTable" = \'{0}\''.format([relatefieldoflayer])

       strpTable = "PathToRelatedTable"

       cursor = arcpy.da.SearchCursor(strpTable, "LableField", strWhereClause)

       result = ""

       for row in cursor:

            strLabel = str(row[0])

            result += strLabel + "\n"

       return result.rstrip()

JacquelineAlessi
Occasional Contributor II

So Im a way less advanced (fly by my seat) python scripter. I used the code sample above and am recieving the following error message:

Error 0 on line 0.

Error Running expression: FindLabel()

Traceback (most recent call last):

file "<expression>", line 1, in <module>

NameError: name 'FindLabel' is not defined

The following is my code, Im trying to pull the attributes from an MSExcel sheet:

def FindLabel (  😞

  return def FindLabel ([OID]):

  strWhereClause = "TunnelRoomOID" = \'{0}\".format([OID])

  strTable = "Z:\Maps\Tunnels\2015 Tunnel Asset Review - Historic Documents\Tunnel_DifficultAccessType_Table2015.xlsx\'Room Definitions$'"

  cursor = arcpy.da.SearchCursor(strpTable, "DifficultAccessTypeCode", strWhereClause)

  result = ""

  for row in cursor:

  strLabel = str(row[0])

  result += strLabel + "\n"

  return result.rstrip()

I also attemted to use the VB example above, it was verified as correct but didnt return a label sample. Not sure what Im doing wrong

0 Kudos
JacquelineAlessi
Occasional Contributor II

PS The table is not related vis Joins and Relates, it is only being related via the script.

0 Kudos
deleted-user-MS0lE1F_0-sy
Occasional Contributor

Warning, I too am not very advanced.  Also, I used this in the field calculator, not inside one of my scripts.

Also, I think there are different rules on when to use " "  or  [ ] depending on the type of file.  Where I am calling for a feature class in a database you are using an xlsx table.  ?? I hope this helps some  ??

Try this.

def FindLabel ([OID]):

strWhereClause = "OID" = \'{0}\".format([OID])

Instead of this:

def FindLabel (  😞

  return def FindLabel ([OID]):

strWhereClause = "TunnelRoomOID" = \'{0}\".format([OID])

JacquelineAlessi
Occasional Contributor II

It actually didnt make any difference in regards to the error that Im recieving I still get a 'FindLabel' is not defined.

0 Kudos
TimMinter
Occasional Contributor III

matthew driscoll‌ & Thomas Stanley-Jones

Gentlemen, thank you both for your posts.  You've pushed me along toward that dubious nirvana of labeling features with relates.  I'm Python-deficient at present, and have a need to label a point feature based on a table that is two relates away:  Facility (point) has Space (table) has Tenant (table).  Your code fired right off after I shortened my table path, so I have Facility (point) has Space (table) no worries.

Is there an awesome Python reference that you know of that has examples of traversing multiple relates?

thanks again,

tim

Edit: I'm stunned.  I believe I just hacked my way into some nested cursors that did the trick.  I'm quite positive it's not optimized, but it works.  Now I'm just into the joy of retrieving domain descriptions so I'm not labeling my points with useful info like "3, 8, 2, 6" 

So, thanks for the kick start, guys.  I hope you're off enjoying your weekends early.  On that note, I'd best lean back on some laurels and maybe slide on out of here.

def FindLabel( [GlobalID] ):

  strSpaceTable = "C:\Facility.gdb\Space"

  strTenantTable = "C:\Facility.gdb\Tenant"

  strSpaceWhereClause = '"FacilityID" = \'{0}\''.format([GlobalID])

  spacecursor = arcpy.da.SearchCursor(strSpaceTable, "GlobalID", strSpaceWhereClause)

  for row in spacecursor:

    spaceGlobalID = str(row[0])

    strTenantWhereClause = '"SpaceID" = \'{0}\''.format(spaceGlobalID)

    tenantcursor = arcpy.da.SearchCursor(strTenantTable, "Org3Acronym", strTenantWhereClause)

    tenantresult = ""

    for row in tenantcursor:

      strLabel = str(row[0])

      tenantresult += strLabel + "\n"

    return tenantresult

  return tenantresult.rstrip()