Working with the ArcPy list functions

Getting a list of data is often the first step in a multistep geoprocessing operation. ArcPy provides many list functions that you can use to gather lists of information, whether they are feature classes, tables, workspaces, and so on. After gathering a list of data, you will often perform geoprocessing operations against the items in the list. For example, you might want to add a new field to all the feature classes in a file geodatabase. To do this, you'd first need to get a list of all the feature classes in the workspace. In this recipe, you'll learn how to use the list functions in ArcPy by working with the ListFeatureClasses() function. All the ArcPy list functions work in the same fashion.

Getting ready

ArcPy provides functions to get lists of fields, indexes, datasets, feature classes, files, rasters, tables, and more. All the list functions perform the same type of basic operations. The ListFeatureClasses() function can be used to generate a list of all feature classes in a workspace. The ListFeatureClasses() function has three optional arguments that can be passed into the function that will serve to limit the returned list. The first optional argument is a wildcard that can be used to limit the feature classes that are returned based on a name, and the second optional argument can be used to limit the feature classes that are returned based on a data type (such as point, line, polygon, and so on). The third optional parameter limits the returned feature classes by a feature dataset. In this recipe, you will learn how to use the ListFeatureClasses() function to return a list of feature classes. You'll also learn how to restrict the list that is returned.

How to do it…

Follow these steps to learn how to use the ListFeatureClasses() function to retrieve a list of the feature classes in a workspace:

  1. Open IDLE and create a new script window.
  2. Save the script as C:ArcpyBookCh9ListFeatureClasses.py.
  3. Import the arcpy module:
    import arcpy
  4. Set the workspace:
    arcpy.env.workspace = "C:/ArcpyBook/data/CityOfSanAntonio.gdb"

    Note

    You should always remember to set the workspace using the environment settings before calling any list function in a script developed with IDLE or any other Python development environment. If this isn't done, the list function would not know which dataset the list should be pulled from. If the script is run inside ArcMap, it returns the feature classes from the default geodatabase if you don't set the workspace.

  5. Call the ListFeatureClasses() function and assign the results to a variable called fcList:
    fcList = arcpy.ListFeatureClasses()
  6. Loop through each of the feature classes in fcList and print them to the screen:
    for fc in fcList:
        print(fc)
  7. You can check your work by examining the C:ArcpyBookcodeCh9ListFeatureClasses_Step1.py solution file.
  8. Save and run the script. You should see this output.
    Crimes2009
    CityBoundaries
    CrimesBySchoolDistrict
    SchoolDistricts
    BexarCountyBoundaries
    Texas_Counties_LowRes
    
  9. The list of feature classes returned by the ListFeatureClasses() function can be restricted through the use of a wildcard passed as the first parameter. The wildcard is used to restrict the contents of your list based on a name. For example, you may want to return only a list of feature classes that start with C. To accomplish this, you can use an asterisk along with a combination of characters. Update the ListFeatureClasses() function to include a wildcard that will find all feature classes that begin with an uppercase C and also have any number of characters:
    fcList = arcpy.ListFeatureClasses("C*")
  10. You can check your work by examining the C:ArcpyBookcodeCh9ListFeatureClasses_Step2.py solution file.
  11. Save and run the script to see this output:
    Crimes2009
    CityBoundaries
    CrimesBySchoolDistrict
    
  12. In addition to using a wildcard to restrict the list returned by the ListFeatureClasses() function, a type restriction can also be applied, either in conjunction with the wildcard or by itself. For example, you could restrict the list of feature classes that are returned to contain only feature classes that begin with C and have a polygon data type. Update the ListFeatureClasses() function to include a wildcard that will find all feature classes that begin with an uppercase C and have a polygon data type:
    fcs = arcpy.ListFeatureClasses("C*", "Polygon")
  13. You can check your work by examining the C:ArcpyBookcodeCh9ListFeatureClasses_Step3.py solution file.
  14. Save and run the script. You will see the following output:
    CityBoundaries
    CrimesBySchoolDistrict
    

How it works…

Before calling any list functions, you will need to set the workspace environment setting that sets the current workspace from which you will generate the list. The ListFeatureClasses() function can accept three optional parameters, which will limit the feature classes that are returned. The three optional parameters include a wild card, feature type, and feature dataset. In this recipe, we've applied two of the optional parameters including a wildcard and a feature type. Most of the other list functions work the same way. The parameter types will vary, but how you call the functions will essentially be the same.

There's more…

Instead of returning a list of feature classes in a workspace, you may need to get a list of tables. The ListTables() function returns a list of standalone tables in a workspace. This list can be filtered by name or table type. Table types can include dBase, INFO, and ALL. All values in the list are of the string data type and contain table names. Other list functions include ListFields(), ListRasters(), ListWorkspaces(), ListIndexes(), ListDatasets(), ListFiles(), and ListVersions().

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

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