2

My program works but it is not displaying a message of the total sheet count found in attribute table that the search cursor searched through. For Example: It goes through the mxd chosen by user finds the layer with the name Grid in it and then is supposed to count the rows in the field Sheet_ID. End result is to print a message of the count found. My code is below:

import arcpy, os, sys
from os import path as p
from arcpy import mapping as m
mxdList = arcpy.GetParameterAsText(0).split(';')
sheet_count = ()
for mapDoc in mxdList:
 arcpy.AddMessage(mapDoc)
 mxd = arcpy.mapping.MapDocument(mapDoc)
 for lyr in m.ListLayers(mxd, "*, Grid"):
 if lyr.description == "*, Grid":
 max_list = []
 rows = arcpy.SearchCursor(lyr)
 for row in rows:
 max_list.append(row.Sheet_ID)
 sheet_count = max(max_list)
 arcpy.AddMessage('Sheet count: %s'%sheet_count)
 print 'Sheet count: %s'%sheet_count
 else:
 arcpy.AddError('No Layers in %s match data source'%mapDoc) 
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 7, 2013 at 15:25
4
  • Are you running this from ArcGIS or command line? Have you tried running it from the command window in ArcGIS? Commented Mar 7, 2013 at 20:23
  • I am running it in ArcGIS through a script tool with the parameters for the selection of the mxd. Commented Mar 7, 2013 at 21:06
  • 1
    There is a syntax error here. Indentation is part of the syntax in Python and you have forgotten a level of indentation after the line if lyr.description == "*, Grid":. Unless you just messed up with the copy/paste of the code into your question. Commented Mar 8, 2013 at 2:57
  • Also the same line doesn't make much sense to me. Are you sure it's doing what you expect? Have you stepped through this in a debugger? See my answer to your other question. Commented Mar 8, 2013 at 2:58

3 Answers 3

3

Your question states that you are looking for the "total count of items from field". Yet, you use the max statement and claim that this is what works. Unless the max value in that particular field does indeed match the number of rows, your output isn't for what you are looking.

If you just want the number of entries, it's a simple call to Get Count.

int(arcpy.GetCount_management(lyrfile).getOutput(0))

If that's not what you are looking for @gotchula has a post that will solve the issue efficiently, assuming you are running ArcMap 10.1, and have access to the data access module, da.

Other than that, there are syntax errors that others have pointed out. Indentation is critical in Python.

answered Jul 6, 2013 at 4:16
1

Look at replacing this

 'Sheet count: %s'%sheet_count

With this

 'Sheet count: {}'.format(sheet_count)

It is the modern way, and it's much more robust. For doc see http://docs.python.org/2/library/string.html#format-string-syntax

Also, walking through all records, then building a list of all sheet_ID then asking for max is very inefficient.

This section of code

 max_list = []
 rows = arcpy.SearchCursor(lyr)
 for row in rows:
 max_list.append(row.Sheet_ID)
 sheet_count = max(max_list)

replace it with this

 with arcpy.da.SearchCursor(lyr, "Sheet_ID", 
 sql_clause=(None, "ORDER BY Sheet_ID DESC")) as cur:
 for row in cur:
 # just get the first value (largest) and quit
 sheet_count = row[0]
 break
answered May 6, 2013 at 23:31
-2

I believe you need to print the messages that you have added. So you need to call arcpy.GetMessages() then print.

print arcpy.GetMessages()

or you can do this:

# Return the resulting messages as script tool output messages
#
x = 0
while x < arcpy.MessageCount:
 arcpy.AddReturnMessage(x)
 x = x + 1

All of this is in the help here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Writing_messages_in_script_tools/00150000000p000000/

answered Mar 7, 2013 at 21:46
1
  • 1
    If run from a script tool, arcpy.AddMessage is the correct function to use to display messages in the tool status window and the geoprocessing results. Commented Mar 8, 2013 at 3:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.