Creating A New Feature Class From List Output

757
7
Jump to solution
01-31-2012 01:56 AM
TommyBurk
New Contributor II
I have a bit of python code that compares consecutive row values in a field. If the first feature's field value is 1 and the second feature's field value is 2, it prints "Different." If they are both 1, it prints "Same." I want to take the features that read "Different" and shoot those out to a new feature class. I know there's a way to do this in python, but I can't seem to come up with it. My list script is as follows:

IdList[]
rows = arcpy.SearchCursor("INPUT FEATURE CLASS")

for row in rows:
     IdPlusOne = int(row.FIELD) + int(1)
     IdList.append(IdPlusOne)

del row, rows

i = 0
i1 = 1

for Id in IdList:
     if i1 >= len(IdList):
          break
     elif Id == IdList[i1]:
          arcpy.AddMessage("Same")
          i+=1
          i1+=1

     else:
          arcpy.AddMessage("Different")
          i+=1
          i1+=1


So I would just like to have those rows that get listed as "Different" to be exported to a new feature class. Is this doable? Thanks so much for any assistance.


Tommy Burk
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BruceNielsen
Occasional Contributor III
If I understand your problem correctly, you want to identify all of the features that have an attribute different than the previous feature, and put those in a new featureclass. I'm still using 9.2, so some of the syntax and capitalization might need correcting, but this should accomplish what you need:
inFile = "INPUT FEATURE CLASS" selectionLayer = MakeFeatureLayer("inFile") rows = arcpy.SearchCursor(inFile) last = "" #or maybe 0 if the attribute is an integer instead of a string for row in rows:     if row.FIELD != last:         arcpy.AddMessage("Different")         arcpy.SelectLayerByAttribute(selectionLayer, "ADD_TO_SELECTION", "\"FID\" = '%s'" % row.FID) #FID is some unique identifier for each feature         #change the where clause to "\"FID\" = %i" if FID is an integer     else         arcpy.AddMessage("Same")     last = row.FIELD arcpy.CopyFeatures(selectionLayer, "OUTPUT FEATURE CLASS")

View solution in original post

0 Kudos
7 Replies
TommyBurk
New Contributor II
I have a bit of python code that compares consecutive row values in a field. If the first feature's field value is 1 and the second feature's field value is 2, it prints "Different." If they are both 1, it prints "Same." I want to take the features that read "Different" and shoot those out to a new feature class. I know there's a way to do this in python, but I can't seem to come up with it. My list script is as follows:

IdList[]
rows = arcpy.SearchCursor("INPUT FEATURE CLASS")

for row in rows:
     IdPlusOne = int(row.FIELD) + int(1)
     IdList.append(IdPlusOne)

del row, rows

i = 0
i1 = 1

for Id in IdList:
     if i1 >= len(IdList):
          break
     elif Id == IdList[i1]:
          arcpy.AddMessage("Same")
          i+=1
          i1+=1

     else:
          arcpy.AddMessage("Different")
          i+=1
          i1+=1


So I would just like to have those rows that get listed as "Different" to be exported to a new feature class. Is this doable? Thanks so much for any assistance.


Tommy Burk
0 Kudos
markdenil
Occasional Contributor III
you could add a flag item to the table, and in addition to printing 'different' you would update the flag to an appropriate value (say, 1). You would need to use an Update Cursor instead of a Search Cursor, of course. After setting the flag, just export the rows with the flag = 1
Then drop the flag item

OR

keep the search cursor, but when you find a 'different' row, capture all the attributes (including the Shape) for that record to a python list.
append that list to a master list, so you build a list of lists of records.

you will also build a similar list of the table items, types, and widths, appended to a master list for the table

Create a new feature class, with the same table schema (use the table items list to add items)
feed the master records list to an Insert Cursor, breaking out each item value (and the shape itself) from the nested list.
0 Kudos
RaphaelR
Occasional Contributor II
i´d go with mark´s 1st suggestion.
exchange the search cursor for an update cursor and fill a field so you can differentiate between features to export/not export.
then you could use feature class to feature class with a SQL query on the new field (choose records where flag=1) . description of it can be found there:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001200000020000000
0 Kudos
TommyBurk
New Contributor II
Mark,

This is great. It's exactly what I want to do. I am unfamiliar with how to add/drop a flag item to/from the table. Can you give me a quick example of how that works? Thanks!

Tommy Burk
0 Kudos
BruceNielsen
Occasional Contributor III
If I understand your problem correctly, you want to identify all of the features that have an attribute different than the previous feature, and put those in a new featureclass. I'm still using 9.2, so some of the syntax and capitalization might need correcting, but this should accomplish what you need:
inFile = "INPUT FEATURE CLASS" selectionLayer = MakeFeatureLayer("inFile") rows = arcpy.SearchCursor(inFile) last = "" #or maybe 0 if the attribute is an integer instead of a string for row in rows:     if row.FIELD != last:         arcpy.AddMessage("Different")         arcpy.SelectLayerByAttribute(selectionLayer, "ADD_TO_SELECTION", "\"FID\" = '%s'" % row.FID) #FID is some unique identifier for each feature         #change the where clause to "\"FID\" = %i" if FID is an integer     else         arcpy.AddMessage("Same")     last = row.FIELD arcpy.CopyFeatures(selectionLayer, "OUTPUT FEATURE CLASS")
0 Kudos
TommyBurk
New Contributor II
Bruce,

That did exactly what I needed. Thanks!

Tommy Burk
0 Kudos
markdenil
Occasional Contributor III
Bruce's add to selection loop is one way of doing it, and should work fine, but sometimes if you have a lot of add-to-selections it gets unweildy.
I prefer simpler selections, saved in attibutes or lists, over Bruce's way, but to each his own.

My first suggestion was very simple: use add item to add the flag item (for exampe: FLAG as short_integer), and every time you find the 'different' record, set FLAG = 1 with the Update Cursor.
afterwards, select all the FLAG = 1 records and export

Some people don't like adding temporary items to their tables, which is why I suggested an alternitive.
0 Kudos