Restricting the list of feature classes returned with a wildcard

By default, the ListFeatureClasses() function will return all of the feature classes in a workspace. You will often want to restrict this list in some way. Three optional parameters can be passed into ListFeatureClasses() to restrict the feature classes returned. All are optional. The first parameter is a wildcard used to restrict the returned list based on some combination of characters. Other parameters that can be used to restrict the list to include a datatype and a feature dataset.

Getting ready

The list of feature classes returned by the ListFeatureClasses() function can be restricted through the use of a wildcard passed in 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 the letter B. To accomplish this, you use a combination of an asterisk with any number of characters. The following code example shows the use of a wildcard to restrict the contents of a list:

fcs = arcpy.ListFeatureClasses("B*")

In this recipe, you will learn how to restrict the list of feature classes returned through the use of a wildcard.

How to do it…

Follow these steps to learn how to restrict the list of feature classes returned by the ListFeatureClasses() function through the use of a wildcard that is passed in as the first parameter:

  1. Open IDLE and the c:ArcpyBookCh10ListFeatureClasses.py script.
  2. Add a wildcard that restricts the list of feature classes returned to only those feature classes that start with the letter C:
    fcs = arcpy.ListFeatureClasses("C*")
  3. Save and run the script to see the following output:
    Crimes2009
    CityBoundaries
    CrimesBySchoolDistrict
    

How it works…

The ListFeatureClasses() function can accept three optional parameters including a wildcard that will restrict the list of feature classes by a name. In this case, we used the wildcard character (*) to restrict the list of feature classes returned to only those that begin with the letter C.

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

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