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()
-
What software suite are you looking to use? I'm assuming arcgis.Paul– Paul2013年07月25日 20:59:41 +00:00Commented Jul 25, 2013 at 20:59
-
Yes arcgis and using python to write scriptuser19506– user195062013年07月25日 21:02:36 +00:00Commented 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.Paul– Paul2013年07月25日 21:07:35 +00:00Commented 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!user19506– user195062013年07月25日 21:32:22 +00:00Commented Jul 25, 2013 at 21:32
-
1Well if you have questions about syntax, you can edit your opening post and we'll go from there.Paul– Paul2013年07月25日 21:34:01 +00:00Commented Jul 25, 2013 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.
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()
Explore related questions
See similar questions with these tags.