Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm looping through a raster list, to export cell counts (from "Count" field in the table) to a text file, I'm only interested in cases where the field "Value" = 1, so I have included this in my search cursor (inR is from the raster list):

Rows = arcpy.SearchCursor(inR, '"Value" = 1', "", "", "")

However, some rasters in the list do not have value = 1 in the table, in this case, I want to return 0 for this count. I've tried to include this in the following loop through the search cursor

for row in Rows:
    countR = row.getValue('Count')
    if "countR" not in locals():
        countR = 0

#append count to list to export later
value_list.append(countR)
del(countR)

When the search cursor is "empty", the get.Value line does not return any value and the object is not created, however, the following line does not work, the idea is that if countR does not exist, then it will be given the value of 0 for this raster in the list (inR).

Edit: this is where I went wrong, the above is not correct, when the search cursor is empty (like an empty list) , there is no loop and none of the statements within that loop take place.

Any help would be appreciated, or alternatives. Thanks

share|improve this question
    
If I'm reading this right, your cursor is already querying out the rows with a Value of anything but 1. If you wanted to retrieve non-1 value rows, you would have to change the search query in your cursor creation. –  Nathanus Mar 29 '11 at 20:09
    
I don't want to retrieve other value rows, just assign a count of zero, if the search cursor is effectively empty. There may be a totally different way of doing this, and better, but this has worked, except that before adding the lines to check whether countR was created, I was repeating values in my list when the raster did not have a value = 1, i.e it was taking the previous raster's count value, which was still "in memory" –  CCID Mar 29 '11 at 20:13
1  
What I mean to say is, because your 0 is assigned inside the Cursor object that only contains raster rows with Value = 1, your if statement will never actually take effect. You would have to create a separate cursor for Value != 1 (or the query equivalent) and then fill all Count fields with 0. –  Nathanus Mar 29 '11 at 20:20
    
when you say you want 'cell counts' do you later need a count of the number of 1s or 0s? If so it might be easy to do it within the loop also. –  djq Mar 29 '11 at 20:32
    
@Nathanus, yes, you're right, the if statement doesn't take effect, because it's empty, this is why it doesn't work. Having said it was empty in the question I didn't figure through that a loop wouldn't work in these cases.. –  CCID Mar 29 '11 at 20:40
add comment

1 Answer

up vote 4 down vote accepted

Why not:

countR = 0

for row in Rows:
    countR += row.getValue('Count')

value_list.append(countR)
share|improve this answer
    
This method will not return 0 or 1, which is what the OP is asking for (I think). I don't think it is an actual count of the rows if I understand correctly. –  Nathanus Mar 29 '11 at 20:23
    
This does work, I wanted the actual values of the count field in the table (i.e. the area/no. of cells of the raster with value = 1), maybe the question wasn't so clear. –  CCID Mar 29 '11 at 20:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.