0

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.

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()
Fezter
22k11 gold badges72 silver badges128 bronze badges
asked Jul 25, 2013 at 20:52
6
  • What software suite are you looking to use? I'm assuming arcgis. Commented Jul 25, 2013 at 20:59
  • Yes arcgis and using python to write script Commented Jul 25, 2013 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. Commented Jul 25, 2013 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! Commented Jul 25, 2013 at 21:32
  • 1
    Well if you have questions about syntax, you can edit your opening post and we'll go from there. Commented Jul 25, 2013 at 21:34

2 Answers 2

4

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.

answered Jul 26, 2013 at 3:18
2

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()
answered Jul 26, 2013 at 3:19

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.