1

I'm not sure what the best way is to go about this, but here's my situation:

In short, I exported a domain to a table (and I exported the table as a CSV, too, just in case it might be easier to work with), and I need to be able to reference the descriptions from the table to replace text on a map template in a Python script. I have a layer that has an attribute that is the same as the coded value in the domain, and whenever I replace the template text for that object, it needs to be able to see whatever the code is in the attribute table and replace the generic template text with the corresponding description from the table.

I'm probably butchering my explanation here, so here's an example of what I'm looking for. Let's say this is the table:

Code Description

  • A Alpha
  • B Bravo
  • C Charlie
  • D Delta

Within my layer, there's an attribute that corresponds to the "Code" in the table, and I used a SearchCursor to make a variable to choose a single record. If it says "A" in the attribute table, I want a code snippet that can see that and select "Alpha" in my exported table (or a CSV or a list that gets created from the CSV, whatever's the most painless!), so I can use that variable for replacing the generic text on a map. (I already have the text replacement code ready, but I just haven't figured out how to read the table. So, if anyone has ideas, please help!)

[EDIT: 8/4/14] Here is the code that I'm stuck on. The first line works and is simply a SearchCursor that gets me the name of an attribute from a feature class. The second line though doesn't work, which is when I'm trying to get the corresponding attribute from a table exported from a domain.

abbreviation = [row[0] for row in arcpy.da.SearchCursor(fc, field_names="myfieldname")][0]
fullname = arcpy.SearchCursor(r"C:\Users\Administrator\Desktop\CurrentProject\mygeodatabase.gdb\myexporteddomain", """ "Code" = '""" + str(myfieldname) + """'""")
  • Would you be able to edit your question to include a snippet of your code so far that works up to where you are stuck, please? I imagine that this would use a SearchCursor to load your table into a Python dictionary and then locate the text element object, and the string within it to replace by looking up the replacement value in the Python dictionary. – PolyGeo Aug 1 '14 at 23:29
  • I added the code that was asked for. What I currently have simply tries to reference an exported ArcGIS table, but if there's an easier way to do it, I'm not married to going this particular route. – Citizen of Pawnee Aug 4 '14 at 12:37
0

The general code logic would look something like this (for version > 10.0):

mxd = 'C;/Temp/Mymxd.mxd'
with arcpy.da.SearchCursor("myLayer", 'Code') as cursor1:
    for row1 in cursor1:
        with arcpy.da.SearchCursor("myTable", ['Code','Des']) as cursor2:
            for row2 in cursor2:
                if row1[0] == row2[0]:
                    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
                        if elm.name == 'Description': # use element name instead of text
                            elm.text = row2[1]
                            arcpy.RefreshActiveView()

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.