dondanvm

Arcpy If Else Statements

Discussion created by dondanvm on Oct 11, 2012
Latest reply on Oct 11, 2012 by VKondratas-esristaff

I was messing around with ArcPY today at work and was having a problem. The script is designed to make a table of the fire hydrant valves that show up in a given data driven page view. We ran into a problem where if there is no fire hydrant in the view than the script breaks. I want to put in an "if" "else" statement to tell it to skip the step that has the error if the select by location finds 0 hydrants. I have listed the script below. The part I am having problems with is highlighted. I do not know how to tell the if statement to find the number of locations. The indenting does not show up, but that is not the problem.
Thanks
Matt

 

#Date: 9/7/2012
#Update Graphic Tables

#This tool runs from a script tool.  Choose a Map Book page number from a pick list
# generated from the script tools validation script.  Click OK and the
# Data Driven Page will update the layer with the appropriate records in the output
# table.

#Note - this script tool uses CURRENT and must be run from within ArcMap.

import arcpy, os, sys

#Reference current MXD - make sure its correct demonstration MXD loaded in ArcMap
mxd = arcpy.mapping.MapDocument("current")

ws = "T:\Working - Geodatabases\RF_Water.gdb"
arcpy.env.workspace = ws
arcpy.env.overwriteOutput = True
table_output = "T:\Working - Geodatabases\RF_Water.gdb\table_output"
tbl = 'table_output'

#Get input parameter
PageName = arcpy.GetParameterAsText(0)

#Reference appropriate data frames
df= arcpy.mapping.ListDataFrames(mxd, "MapBook_Page")[0]
#mxd.activeView = df

sheetDF = arcpy.mapping.ListDataFrames(mxd, "MapBook_Page")[0]
#locatorDF =arcpy.mapping.ListDataFrames(mxd, "Map Locator")[0]

#Reference appropriate layers

indexGrid = arcpy.mapping.ListLayers(mxd, "Page Index Grid", sheetDF)[0]

#Reference layout elements by calling ListLayoutElements only once - get better performance
for elm in arcpy.mapping.ListLayoutElements(mxd):
  if elm.name =="Table1Column1": tab1Col1Txt = elm
  if elm.name =="Table1Column2": tab1Col2Txt = elm
  if elm.name =="Table1Column3": tab1Col3Txt = elm

#Reference the Data Driven Page object
ddp = mxd.dataDrivenPages

#Set the current page to be the one selected in the script tool
arcpy.AddMessage(PageName)

pageID = mxd.dataDrivenPages.getPageIDFromName(str(PageName))
mxd.dataDrivenPages.currentPageID = pageID

#for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
#    mxd.dataDrivenPages.currentPageID = pageNum
#    arcpy.mapping.ExportToPDF(mxd, r"C:\Temp\WaterMap" + str(pageNum) + ".pdf")
#del mxd



#Build selection set
layers = arcpy.mapping.ListLayers(mxd, "Main valves", df)

for lyr in layers:
    extentPolygon = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft,df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),df.spatialReference)
    arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", extentPolygon, "", "NEW_SELECTION")
  
    arcpy.CopyRows_management("Main valves", tbl)                       #############################
    selectioncount = arcpy.CopyRows_management("Main valves", tbl)      #############################
    arcpy.GetCount_management("selection count")                        #############################
                                                                        #############################
selection = arcpy.GetCount_management("selection count")                #############################
if selection == 0:
   #Clear all table text values
  tab1Col1Txt.text = " "; tab1Col2Txt.text = " "; tab1Col3Txt.text = " "
  tab1Col1Value = ""
  tab1Col2Value = ""
  tab1Col3Value = ""

  mxd.activeView = "PAGE_LAYOUT"

  arcpy.mapping.ExportToPDF(mxd, r"C:\Temp\WaterMap" + str(pageID) + ".pdf")
  arcpy.RefreshActiveView()
  mxd.save()
  arcpy.ClearWorkspaceCache_management()
  arcpy.AddMessage("PROCESS COMPLETED")
  arcpy.CopyRows_management("Main valves", tbl)


else:


  Valves_outputrows = arcpy.SearchCursor(tbl,"","","Valve_ID; vDiameter; LOCATION_DATA","Valve_ID")

  #Clear all table text values
  tab1Col1Txt.text = " "; tab1Col2Txt.text = " "; tab1Col3Txt.text = " "
  tab1Col1Value = ""
  tab1Col2Value = ""
  tab1Col3Value = ""


  for row in Valves_outputrows: 
    tab1Col1Value += str(row.Valve_ID) + "\n"
    tab1Col2Value += str(row.vDiameter) + "\n"
    tab1Col3Value += str(row.LOCATION_DATA) + "\n"

  tab1Col1Txt.text = tab1Col1Value
  tab1Col2Txt.text = tab1Col2Value
  tab1Col3Txt.text = tab1Col3Value

  arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
  mxd.activeView = "PAGE_LAYOUT"

  arcpy.mapping.ExportToPDF(mxd, r"C:\Temp\WaterMap" + str(pageID) + ".pdf")
  arcpy.RefreshActiveView()
  mxd.save()
  arcpy.ClearWorkspaceCache_management()
  arcpy.AddMessage("PROCESS COMPLETED")

Outcomes