Retrieving features from a feature class with SearchCursor

There are many occasions when you need to retrieve rows from a table or feature class for read-only purposes. For example, you might want to generate a list of all land parcels in a city with a value greater than $100,000. In this case, you don't have any need to edit the data. Your needs are met simply by generating a list of rows that meet some sort of criteria. A SearchCursor object contains a read-only copy of rows from a table or feature class. These objects can also be filtered through the use of a where clause so that only a subset of the dataset is returned.

Getting ready

The SearchCursor() function is used to return a SearchCursor object. This object can only be used to iterate a set of rows returned for read-only purposes. No insertions, deletions, or updates can occur through this object. An optional where clause can be set to limit the rows returned. In this recipe, you will learn how to create a basic SearchCursor object on a feature class through the use of the SearchCursor() function.

The SearchCursor object contains a fields property along with the next() and reset() methods. The fields property is a read-only structure in the form of a Python tuple, containing the fields requested from the feature class or table. You are going to hear the term tuple a lot in conjunction with cursors. If you haven't covered this topic before, tuples are a Python structure to store a sequence of data similar to Python lists. However, there are some important differences between Python tuples and lists. Tuples are defined as a sequence of values inside parentheses, while lists are defined as a sequence of values inside brackets. Unlike lists, tuples can't grow and shrink, which can be a very good thing in some cases when you want data values to occupy a specific position each time. This is the case with cursor objects that use tuples to store data from fields in tables and feature classes.

How to do it…

Follow these steps to learn how to retrieve rows from a table or feature class inside a SearchCursor object:

  1. Open IDLE and create a new script window.
  2. Save the script as C:ArcpyBookCh8SearchCursor.py.
  3. Import the arcpy.da module:
    import arcpy.da
  4. Set the workspace:
    arcpy.env.workspace = "c:/ArcpyBook/Ch8"
  5. Use a Python with statement to create a cursor:
    with arcpy.da.SearchCursor("Schools.shp",("Facility","Name")) as cursor:
  6. Loop through each row in SearchCursor and print the name of the school. Make sure you indent the for loop inside the with block:
    for row in sorted(cursor):
      print("School name: " + row[1])
  7. The entire script should appear as follows:
    import arcpy.da
    arcpy.env.workspace = "c:/ArcpyBook/Ch8"
    with arcpy.da.SearchCursor("Schools.shp",("Facility","Name")) as cursor:
        for row in sorted(cursor):
            print("School name: " + row[1])
  8. Save the script.
  9. You can check your work by examining the C:ArcpyBookcodeCh8SearchCursor_Step1.py solution file.
  10. Run the script. You should see the following output:
    School name: ALLAN
    School name: ALLISON
    School name: ANDREWS
    School name: BARANOFF
    School name: BARRINGTON
    School name: BARTON CREEK
    School name: BARTON HILLS
    School name: BATY
    School name: BECKER
    School name: BEE CAVE
    

How it works…

The with statement used with the SearchCursor() function will create, open, and close the cursor. So, you no longer have to be concerned with explicitly releasing the lock on the cursor as you did prior to ArcGIS 10.1. The first parameter passed into the SearchCursor() function is a feature class, represented by the Schools.shp file. The second parameter is a Python tuple containing a list of fields that we want returned in the cursor. For performance reasons, it is a best practice to limit the fields returned in the cursor to only those that you need to complete the task. Here, we've specified that only the Facility and Name fields should be returned. The SearchCursor object is stored in a variable called cursor.

Inside the with block, we use a Python for loop to loop through each school returned. We also use the Python sorted() function to sort the contents of the cursor. To access the values from a field on the row, simply use the index number of the field you want to return. In this case, we want to return the contents of the Name column, which will be the 1 index number, since it is the second item in the tuple of field names that are returned.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset