1

I'm updating text elements off of an arcpy.da.SearchCursor script and needed to construct the script to return a response for when the attribute does NOT exist. Here is the snippet of the code THAT WORKS:

fcWO = r"W:\\GIS_Projects\\Impervious_Surfaces_Tables\\SUM_WORKORDER"
field1 = "SURFACE_TYPE"
field2 = "SUM_SQ_FT"

valueB = r"N/A"
valueM = r"N/A"
valueP = r"N/A"
valueS = r"N/A"
valueR = r"N/A"

with arcpy.da.SearchCursor(fcWO, (field1, field2)) as cursor:
    for row in cursor:
        if row[0] == "BUILDING":
            valueB = row[1]
        elif row[0] == "MISC":
            valueM = row[1]
        elif row[0] == "PARKING":
            valueP = row[1]
        elif row[0] == "ROAD":
            valueR = row[1]
        elif row[0] == "SIDEWALK":
            valueS = row[1]

for elm in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT"):
    if elm.name == 'Building':
        elm.text = valueB
    elif elm.name == 'Misc':
        elm.text = valueM
    elif elm.name == 'Parking':
        elm.text = valueP
    elif elm.name == 'Roadway':
        elm.text = valueR
    elif elm.name == 'Sidwalk':
        elm.text = valueS
11
  • I think you should focus your question on a code snippet that simply uses a text element alone on a layout to see if you can move a single value from a table into it. I would use a SearchCursor to read the table and pseudo-dynamic text to display it.
    – PolyGeo
    Jun 22 '16 at 20:10
  • any reason for not using "view > report" ? Editing text element with Python will require a lot of "hard coding" ta achieve a nice layout.
    – radouxju
    Jun 24 '16 at 6:13
  • 1
    @radouxju , Yes, you are correct. However, I'm trying to make something that other "less savvy" users can operate. I'm trying to eliminate as much "free-thought" as possible. :-) If all they have to do is enter in two values, and press one button, it will greatly reduces the potential for errors that currently exists in our workflow. Jun 24 '16 at 15:22
  • I think you should be using elif instead of else? What does valueB = '{:,.0f}'.format((row[1])) do?
    – GISHuman
    Jun 28 '16 at 17:31
  • @GISKid , I tried that and I couldn't quite get it to work. valueB should be the formatted number that is in row[1] of where row[0] == "BUILDING". Am I making sense? The end result is a pdf that has square footage of impervious surfaces. Instead of copying and pasting, I would like the script to calculate and place these values in certain spots on the layout (the text elements). Jun 28 '16 at 17:46
1

To continue with your code sample:

import arcpy

mxd = arcpy.mapping.MapDocument("Current")
fc = r"W:\\GIS_Projects\\Impervious_Surfaces_Tables\\TOTAL"
field1 = "SURFACE_TYPE"
field2 = "SUM_SQ_FT"
value = ""

with arcpy.da.SearchCursor(fc, [field1, field2]) as cursor:
     for row in cursor:
         if row[0] == "BUILDING":
              value = str(row[1])

for elm in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT"):
     if elm.name == 'Total':
          elm.text = value

arcpy.RefreshActiveView()

THIS WORKS!:

import arcpy

mxd = arcpy.mapping.MapDocument("Current")
fc = r"W:\\GIS_Projects\\Impervious_Surfaces_Tables\\TOTAL"
field1 = "SURFACE_TYPE"
field2 = "SUM_SQ_FT"

valueB = r"N/A"
valueM = r"N/A"
valueP = r"N/A"
valueS = r"N/A"
valueR = r"N/A"

with arcpy.da.SearchCursor(fc, (field1, field2)) as cursor:
     for row in cursor:
         if row[0] == "BUILDING":
              valueB = str(row[1]
         elif row[0] == "MISC":
              valueM = str(row[1]
         elif row[0] == "PARKING":
              valueP = str(row[1]
         elif row[0] == "ROAD":
              valueR = str(row[1]
         elif row[0] == "SIDEWALK":
              valueS = str(row[1]

for elm in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT"):
    if elm.name == 'Building':
        elm.text = valueB
    elif elm.name == 'Misc':
        elm.text = valueM
    elif elm.name == 'Parking':
        elm.text = valueP
    elif elm.name == 'Roadway':
        elm.text = valueR
    elif elm.name == 'Sidewalk':
        elm.text = valueS

arcpy.RefreshActiveView()

If you print the map after you refresh it I would suggest to set all the text elements back to "N/A". It would be the same code as above, just with

elm.text = r"N/A"

9
  • I attempted the above code. I tweaked it to match my table and element name. However, when I run it, I receive this error: Runtime error Traceback (most recent call last): File "<string>", line 6, in <module> RuntimeError: An invalid SQL statement was used. Jun 24 '16 at 16:24
  • @Layne LeBleu: Sorry I am not that familiar wirth the da.cursors. The fields need to be in square brackets (line 9 / old script line 6). And there is no getvalue and setvalue. I changed the script accordingly and tested it. It works now.
    – Alex
    Jun 27 '16 at 6:40
  • no worries! I JUST figured that out and came here to edit my comment. However, do I need to reset the cursor after it names each element? When I run the script, it will give the value correctly for the first two, and then the final entry. Jun 27 '16 at 14:47
  • It depends on what you really want to do. If you want to work with differnet conditions, I would suggest to work with one cursor only. If you want to have a dummy variable, or an empty text you need to specify that. I added an alternative script for that to my answer. Is that what you meant?
    – Alex
    Jun 28 '16 at 6:32
  • We are SO close! I updated the code in the question. It has has everything working EXCEPT that "BUILDING" AND "PARKING" are not working correctly. The only value that should return a "N/A" should be "ROADWAY" which is working properly. What am I doing wrong? Jun 28 '16 at 16:53

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.