List Function
There are several different types of list functions and some of them will be explored in this section.
List Feature Class
In Figure 10, the members of a folder will be displayed. The folder is named Vince and located on the author’s Y drive. The first two lines of code set the environment workspace to the folder location. It is important to note the direction of the slashes for the pathway to the folder.
|
The third line sets the value of the variable ‘feature’ to the members in the workspace by default, since there are no values placed in the parentheses. The default is a list of all members of the folder. Parameters could be placed in the parentheses, which could look for values of a single
state or only a specific type of file such as a point file.
state or only a specific type of file such as a point file.
In the example, shown in Figure 11, the members of a geodatabase will be explored as well as searching for only specific members of the geodatabase. This is not a relational database that is being searched.
- It is important to note in the setting of the workspace that the slashes are going in the opposite direction then those in the previous example (the pathway would be different for the learner).
- In the second line of code that a wildcard search has been added between the parentheses. The asterisk shows that as long as IN is located in the search a result will be returned. IN represents the state of Indiana. Therefore, if a good naming convention was used within the geodatabase, the returned results will be all files that are related to IN in the geodatabase. Note the geodatabase has not been properly named since its name is KY for Kentucky (but does contain items from Indiana).
- The variable feature, which is the result of the command, and then printed
- The results are shown in gray. In Figure 11 the results line has been clipped for display purposes.
List Fields
ListFields is a new function, but has similarities to the ListFeatureClasses, except it is dealing with the fields inside of a single file. For example; if the fields names contained within a shapefile are needed, typically the user would need to load the shapefile into ArcMap, then open the attribute table to discover the names. Other parameters can also be returned using this command, but for this example only the field names will be returned. In this example a For statement will be used so that all fields contained within the layer are displayed. The output is shown in a gray color in Figure 12.
|
- The first line shown in Figure 13 is to set the environment just as was done in Figure 10. Since the shapefile to be explored is located in a folder this code is used. If the shapefile had been in a geodatabase, the pathway would be written differently.
- The next functional line of code is creating a list variable (fieldName) based on the contents of the College shapefile, which is contained in the gis data folder.
|
Conclusion
It is important to keep in mind that the Python Window in ArcMap works similarly to the immediate mode in an IDE. The Python script window will give hints as commands are being typed on the format and the command names. The files used in ArcMap, in general, can be loaded, but this is not a requirement. Realize that some operations cannot be completed on an opened file due to locks installed by the software. A lock is a file protection so that the file cannot be modified while opened, thus two users cannot be editing at the same time, and generally, this can be done only with a relational database. The database operations discussed are for personal geodatabases and might function differently if used with a relational database. The separating slashes in pathways in the environment statement are different for folders and for geodatabases.