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 am trying to take an existing shapefile and narrow down the attributes using the 'select by attribute' The problem I am trying to solve is: Find parcels that have an acreage >= 100. The code I have is not narrowing down the search or showing up in arcmap. Below is the code I have so far. Any help would be greatly appreciated!

import arcpy

#Get Parcel layer
countyParcels = "C:\\Users\\Krystle\\Desktop\\WCGIS\\Geog485         \\final\\TarrantCoParcels\Parcels2013.shp"
nameField = "TAXPIN"

try:
    #Make a feature layer of parcels2013
    arcpy.MakeFeatureLayer_management(countyParcels, "AllParcelsLayer")

except:
    print "Could not create feature layer"

try:
    #Create a seach cursor
    rows = arcpy.SearchCursor("AllParcelsLayer")
    #Call SearchCursor.next() to read the first row
    rows = rows.next()

    #Start a loop that will exit when there are no more rows available
    while row:
        arcpy.SelectLayerByAttribute_management("AllParcelsLayer", '"CALCULATED" >=100.00')
        #Print vaules in the current row
        print row.getValue(nameField)
        #Call SearchCursor.next() to move to the next row
        row = rows.next()

    #Clean up cursor and row objects
    del row
    del rows
    #Write the selected features to a new featureclass
    arcpy.CopyFeatures_management("AllParcelsLayer", "parcels_100plus")

except:
    print arcpy.GetMessages()
share|improve this question
    
What software suite are you looking to use? I'm assuming arcgis. –  Paul Jul 25 '13 at 20:59
    
Yes arcgis and using python to write script –  user19506 Jul 25 '13 at 21:02
    
Are you able to do what you want within ArcMap? For instance, you can use a tool and then convert to the Python code, as outlined here. –  Paul Jul 25 '13 at 21:07
    
I believe I know what to do, but it's the logistics of narrowing down using certain information. I will try this though. Thanks! –  user19506 Jul 25 '13 at 21:32
1  
Well if you have questions about syntax, you can edit your opening post and we'll go from there. –  Paul Jul 25 '13 at 21:34

2 Answers 2

You don't need a search cursor to do what you want. At the moment, you are running a selection on the entire layer for each row you have. So, if you have 1000 rows, you are making a selection on the entire layer 1000 times.

Try getting rid of your search cursor and the while statement - just use the select tool. I suggest you read the help page on the select tool as what you're trying to do is discussed there.

share|improve this answer

There's a few issues:

row is undefined, which means that your while loop cannot loop over it. You need to define row = rows.next(), see here.

You are missing a parameter for SelectLayerByAttribute(). You need to specify which type of selection you want (new selection, add to current selection, remove from selection).

I think you want your search cursor after you use SelectLayerByAttribute(). As Fetzer mentioned, you are pretty much running the same code the number of times equal to your feature count.

This might point you in the right direction:

import arcpy

countyParcels = "C:\\Users\\Krystle\\Desktop\\WCGIS\\Geog485\\
                 final\\TarrantCoParcels\Parcels2013.shp"
nameField = "TAXPIN"

try:  
    arcpy.MakeFeatureLayer_management(countyParcels, "AllParcelsLayer")
except:
    print "Could not create feature layer"

try:    
    arcpy.SelectLayerByAttribute_management("AllParcelsLayer", 
                                            "NEW_SELECTION", '"CALCULATED" >=100.00')

    rows = arcpy.SearchCursor("AllParcelsLayer")
    row = rows.next()
    while row:        
        print row.getValue(nameField)        
        row = rows.next()

    del row, rows

    arcpy.CopyFeatures_management("AllParcelsLayer", "parcels_100plus")

except:
    print arcpy.GetMessages()
share|improve this answer

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.