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 have written a python script that is supposed to basically loop through selected records of a feature class. Here's a sample of what a table looks like:

OBID   STID FL  TL      
1   1   0   9       
2   1   8   19      
3   2   3   23      
4   3   0   0       
5   1   21  29      
6   3   0   11      
7   1   27  31      
8   3   13  19

Here's what I'm trying to do:

  1. Create a SearchCursor (for main file) and InsertCursor (for new file)

  2. Sort all records by STID, FL and TL in order with the SearchCursor

  3. Record TL for the first row and move onto next row

  4. Record STID and FL in a variable

  5. Compare current FL to previous TL

  6. If FL<=TL, export both rows to new table with InsertCursor

  7. Continue until current STID is equal to previous STID. If not, go back to step 3 (ie, repeat the process for new STID)

And here's the code I wrote which is not working:

import arcpy
arcpy.env.workspace = "c:\\temp\\test.mdb"
abc = arcpy.SearchCursor("road","","","STID;FL;TL","STID A;FL A;TL A")
first = 1
for row in abc:
     if first != 1:
         spid = row.getValue("STID")
         frl = row.getValue("FL")
             if spid == prev:
                 if frl <= trl:
                    print spid, frl
                    prev = row.getValue("STID")
                    trl = row.getValue("TL")
             else:
                  prev = row.getValue("STID")
                  trl = row.getValue("TL")
      else:
         prev = row.getValue("STID")
         trl = row.getValue("TL")
         first = 0
del row
del abc
share|improve this question
    
Could you be a little more clear on the question? Did you want help actually writing the routine? Or because the code is throwing an error (which I assumed below, but now that I re-read the question, may not be the case)? –  om_henners Jul 13 '11 at 3:38
    
Also, if the code is throwing an error, could you post what it was? –  om_henners Jul 13 '11 at 3:40

2 Answers 2

The logic behind what you're doing is somewhat unclear, but it sounds like you're trying to sort a bunch of records on a few different fields and then go through them one at a time trying to compare each record with some previous record based on the STID field and if neighboring records don't have the same STID field, then you move to the next until they match.

I think the trouble you're getting into, and this is a shot in the dark, is that search cursors don't support looping backwards. So if you're looking for STID-x and you iterate through your search cursor rows looking for STID-y == STID-x, you may end up at the end of your cursor object before you've done your complete analysis on each record. You can add some print statements to see what's going on for each iteration in your search cursor to help pinpoint where your script is going wrong.

Also, based on the fields and data you supplied above, it looks like you want to be comparing the OBID field to the STID field. If that's the case you need to re-look at your sorting and your getValue statements.

EDIT:

Ok, I got this to work. It looks like your problem was caused because you don't have a 'proper' Object ID for your rows. Not quite sure why this works, but I added a field called FID in Access and set it's type to 'AutoNumber' which stores a unique value for each record (even though your OBID field is unique already). Maybe someone else can enlighten us both. After doing that, I added logic to scrape your records from the 'roads' table into a new table called 'output' which is either created from scratch or cleared of any pre-existing data each time you run this script.

import arcpy
ws = 'C:/temp/test.mdb'
arcpy.env.workspace = ws
arcpy.env.overwriteOutput = True
rows = arcpy.SearchCursor('road','','','STID;FL;TL','STID A; FL A; TL A')

#-- create new table to hold results using 'road' table as template
if arcpy.Exists('output'):
    arcpy.DeleteRows_management('output') #-- clear contents of output table
else:
    arcpy.CreateTable_management(ws,'output','road')
    arcpy.AddField_management('output','originalFID','LONG')

irows = arcpy.InsertCursor('output')  #-- create insert cursor

first = True
for row in rows:
    if first:
        first = False
    else:
        fid = row.FID
        stid = row.STID
        fl = row.FL
        tl = row.TL
        if stid == prev_stid and fl <= prev_tl:
            #-- add previous record to new table
            irow = irows.newRow()
            irow.originalFID = prev_fid
            irow.STID = prev_stid
            irow.TL = prev_tl
            irow.FL = prev_fl
            irows.insertRow(irow)
            del irow

            #-- now let's add the current record
            irow = irows.newRow()
            irow.originalFID = fid
            irow.STID = stid
            irow.TL = tl
            irow.FL = fl
            irows.insertRow(irow)
            del irow

            print stid,fl

    #-- let's store all information from this row
    prev_fid = row.FID
    prev_stid = row.STID
    prev_tl = row.TL
    prev_fl = row.FL

del rows,irows
share|improve this answer
    
@Sam Editing an answer is not an acceptable way to provide comments. I believe you are able to comment on answers to your own question. If not, you definitely can edit your question itself, provided this is an improvement to the question and not just a way to carry out a conversation. –  whuber Jul 13 '11 at 15:56
    
For the record, so it does not get lost, here is the comment Sam would like to make: "Jason, The logic I used was that once the table is sorted using searchcursor, all that the code has to do is go to the next row and then compare like enumerated rows . But again, I'm new to all this and it might not make any sense. Can you suggest me how I should go about this? Thanks." –  whuber Jul 13 '11 at 15:57
    
@Sam, without seeing your data I'm having trouble coming up with any solution for you here, though I posted an edit to my answer above to start off with. –  Jason Jul 13 '11 at 21:03
    
An excerpt of the data appears at the beginning of the question. +1 for your effort and for cleaning up the code, though. –  whuber Jul 13 '11 at 21:05
    
Ah yes, my bad. See updated code... –  Jason Jul 13 '11 at 22:10

Well, I copied your script and the test data. The only error I could see was in the second line, where you set the workspace - backslash is an escape character in Python.

There are four reasonably common alternatives. Double backslash (escapes the escape):

arcpy.env.workspace = "c:\\temp\\test.mdb"

Raw strings (prefix r):

arcpy.env.workspace = r"c:\temp\test.mdb"

Triple quotes:

arcpy.env.workspace = """c:\temp\test.mdb"""

And forward slashes:

arcpy.env.workspace = "c:/temp/test.mdb"

Choose one that works for you. And there are more.

share|improve this answer
    
om_henners, The code doesn't give any error messages when I run it. It runs but gives unexpected result: 3 0.0 I am not sure how or why. And python recognizes 2 backslashes for path. I don't think that is a problem. Its the loop. It would be awesome if you or anybody can help me figure this out. Thanks. –  Sam Jul 13 '11 at 13:22

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.