Python Scripting for ArcGIS Pro by Paul A. Zandbergen

Python Scripting for ArcGIS Pro by Paul A. Zandbergen

Author:Paul A. Zandbergen [Zandbergen, Paul A.]
Language: eng
Format: epub
Tags: Python, Programming languages, Systems analysis and design, Geographic Information Systems, Data mining
Publisher: Esri Press
Published: 2020-04-28T18:29:19+00:00


6.5 Workspaces and listing data

When working with ArcPy’s list functions, it is critical to be aware of the workspace used and what workspaces mean in the context of different spatial data formats. Consider a scenario in which you have a folder that contains both shapefiles and a file geodatabase with multiple feature classes.

Description MyData folder contains a geodatabase called City.gdb, with feature classes bus_routes and bus_stops, and shapefiles called facilities.shp, parks.shp, and roads.shp in the root folder.

Consider what happens when you use the ListFeatureClasses() function on this folder by setting the workspace to MyData:

import arcpy

arcpy.env.workspace = "C:/Testing/MyData"

fcs = arcpy.ListFeatureClasses()

print(fcs)

The result prints as follows:

['facilities.shp', 'parks.shp', 'roads.shp']

The resulting list of feature classes includes only the shapefiles. Why are the feature classes inside the geodatabase not part of the result? The answer lies in a closer look at the use of workspaces. For example, the workspace for the shapefile parks.shp is C:\MyData. However, the workspace for the geodatabase feature class bus_routes is the geodatabase itself—i.e., C:\MyData\City.gdb. To list the geodatabase feature classes, you must specify the geodatabase as the workspace, as follows:

import arcpy

arcpy.env.workspace = "C:/Testing/MyData/City.gdb"

fcs = arcpy.ListFeatureClasses()

print(fcs)

The result prints as follows:

['bus_routes', 'bus_stops']

Elements inside a geodatabase require a different approach from shapefiles and other stand-alone datasets because of the way workspaces is defined. The workspace for an element inside a geodatabase is the geodatabase itself, whereas for stand-alone datasets, the workspace is simply the root folder.

To get a better handle on workspaces, use the arcpy.ListWorkspaces() function. Consider a more complex folder structure, with one subfolder that contains more shapefiles and two file geodatabases.

DescriptionProject folder contains a subfolder called Shapefiles, with shapefiles called facilities.shp, parks.shp, and roads.shp; a geodatabase called City.gdb, with feature classes called bus_routes and bus_stops; a geodatabase called Water.gdb, with feature classes called monitoring, streams, and watersheds; and a shapefile called boundaries.shp in the root folder.

When the workspace in the script is set to C:\Project, the ListFeatureClasses() function returns only the shapefile boundaries.shp. You can use the ListWorkspaces() function to list the workspaces within the set workspace. The general syntax for this function is

arcpy.ListWorkspaces({wild_card}, {workspace_type})

The two parameters for this function are optional. Consider how the ListWorkspaces() function can be used for the more complex folder structure:

import arcpy

arcpy.env.workspace = "C:/Project"

wspaces = arcpy.ListWorkspaces()

print(wspaces)

The result prints as follows:

['C:/Project\\City.gdb', 'C:/Project\\Shapefiles',

'C:/Testing\\Water.gdb']

The result includes the two geodatabases and the folder that contains the shapefiles. Each of these three elements is a separate workspace within the workspace set in the script.

The ListWorkspaces() function recognizes five different types of workspaces:

ACCESS (personal geodatabases)

COVERAGE (coverages)

FILEGDB (file geodatabases)

FOLDER (shapefiles)

SDE (enterprise geodatabase)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.