How can I extract just polygons from a file geodatabase

2320
4
11-13-2017 04:42 PM
BrendaJameson
New Contributor II

I have a file geodatabase with over 200 feature classes. I want to extract all the feature classes (including those in feature datasets) that are polygons and save/append them to a separate shapefile. Also, is there anyway to put the filename of the each of the feature classes in a field name called "Label"? For instance, if one of the files that is getting copied to the polygon shapefile is called "FredsPlace", when that gets appended to the shapefile, its label field is filled in with its name "FredsPlace".

Is there a tool in ArcGIS 10.5 that already does this or am I going to have to write a python script to do this? If so, what approach would you recommend I take to accomplish this.

Many thanks in advance.

Tags (2)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

In scripting, you can use ListFeatureClasses and specify the feature type as a filter.

BrendaJameson
New Contributor II

I ended up using arcpy.da.Walk to loop through the geodatabase but ListFeatureClasses would have worked also. I was unble to figure out how to programmatically put the files names into a field that resided within a feature dataset but the feature classes that were not inside a geodatabase dataset were populated using the following toolbox:

AddFileName toolbox

JoshuaBixby
MVP Esteemed Contributor

Typically, either Merge—Help | ArcGIS Desktop  or Append—Help | ArcGIS Desktop are used to combine multiple input data sets of the same type into one data set (sometimes Union—Help | ArcGIS Desktop  is used if geometries are to be conflated/planarized).

If you are merging multiple data sets of the same type into a single data set, are all of the attributes from the data sets structured the same, i.e., same column names with same data types?  If so, that greatly simplifies merging them.  If not, then a field mapping of some sort will need to be created.

In addition to ListFeatureClasses, one can also use Walk—Help | ArcGIS Desktop to find and list spatial data sets.

Given what you have described, something will have to be scripted because there is no single tool that does everything you are trying to accomplish.

RandyBurton
MVP Alum

Using ListFeatureClasses to loop through a geodatabase:

import arcpy
import os

gdb = r'C:\Path\To\geodatabase.gdb'
arcpy.env.workspace = gdb
sfDir = 'C:/Temp/' # directory to put shapefiles, subdirectories will be created

for fc in arcpy.ListFeatureClasses("*","Polygon"):
    print "Creating shapefile: {}".format(fc)
    if not os.path.exists('{}{}'.format(sfDir, fc)):
        os.makedirs('{}{}'.format(sfDir, fc))
    arcpy.FeatureClassToShapefile_conversion(fc, '{}{}'.format(sfDir, fc))
    arcpy.AddField_management('{}{}/{}.shp'.format(sfDir,fc,fc),
                              field_name = "LABEL",
                              field_type = "TEXT",
                              field_length = "25" )
    arcpy.CalculateField_management('{}{}/{}.shp'.format(sfDir,fc,fc),
                                    field = "LABEL",
                                    expression = '"{}"'.format(fc) )‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This puts shape files in a subdirectories to keep them grouped. It uses the feature's name for the subdirectory, the shape file name and to populate a "LABEL" field. This assumes that the feature name will be valid for such purposes. It has not been tested with datasets, but a version using ListDatasets as an outer loop could be created if this doesn't pick up all the desired features.

0 Kudos