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.
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()
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])
PS The table is not related vis Joins and Relates, it is only being related via the script.
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:
strTable = "Z:\Maps\Tunnels\2015 Tunnel Asset Review - Historic Documents\Tunnel_DifficultAccessType_Table2015.xlsx\'Room Definitions$'"
cursor = arcpy.da.SearchCursor(strpTable, "DifficultAccessTypeCode", strWhereClause)
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
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()
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)
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.
There are some examples of label expression using python in the help files
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.