Write Name and Geometry Type of Feature Classes in an Enterprise Geodatabase Connection

4080
6
Jump to solution
10-27-2017 10:34 AM
JayantaPoddar
MVP Esteemed Contributor

Hi Guys,

I need a script which would write the name and geometry (Point,Line,Polygon) of the Feature Classes from an Ent. GDB to a CSV file.

So far, I am able to write the names successfully using the below script. 

import arcpy 
from arcpy import env 
import os 

# Set the workspace for the ListFeatureClass function 

from arcpy import env 
import os 

# Set the workspace for the ListFeatureClass function 
env.workspace = r"Path\xyz.sde"
# Use the ListFeatureClasses function to return a list of all fc's in the sde gdb: 
fcList = arcpy.ListFeatureClasses() 

# Write the name of the current fc in csv file:
txtFile = open(r"Path\xyz.csv","w")
for fc in fcList:
    print fc

    # Write messages to a csv File
    txtFile.write(fc)
    txtFile.write (os.linesep)

#close csv file
txtFile.close()

print "done"
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

How do I write the Geometry Type?

Also suggestions are welcome to further optimize the above script.

Thanks!!!



Think Location
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor

Or just write the desc.featureType value to your output:

for fc in fcList:

    desc = arcpy.Describe(fc)

    # Write messages to a csv File
    txtFile.write(fc + " " + desc.featureType)
    txtFile.write (os.linesep)

#close csv file
txtFile.close()

print "done"
‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

6 Replies
JamesCrandall
MVP Frequent Contributor

Looks like arcy.Describe() is what you will need.  http://pro.arcgis.com/en/pro-app/arcpy/functions/featureclass-properties.htm

JayantaPoddar
MVP Esteemed Contributor

Thanks James.

How can I use arcpy.Describe() for all the FCs in a Geodatabase? And ultimately write them next to the name of each FC in CSV.



Think Location
0 Kudos
JamesCrandall
MVP Frequent Contributor

One possibility is to simply set a variable so that you can use it to write the featureType to your output file:

for fc in fcList:
    desc = arcpy.Describe(fc)
    featType = desc.featureType‍‍‍‍‍‍
0 Kudos
JamesCrandall
MVP Frequent Contributor

Or just write the desc.featureType value to your output:

for fc in fcList:

    desc = arcpy.Describe(fc)

    # Write messages to a csv File
    txtFile.write(fc + " " + desc.featureType)
    txtFile.write (os.linesep)

#close csv file
txtFile.close()

print "done"
‍‍‍‍‍‍‍‍‍‍‍‍
JayantaPoddar
MVP Esteemed Contributor

Perfect.

I just replaced Line. No. 6 in your script with the following, as per my requirement

txtFile.write(fc + "," + desc.shapeType)


Think Location
0 Kudos
MicahBabinski
Occasional Contributor III

Hi Jayanta,

Here's a couple enhancements I'd make:

First, list the feature classes using Walk—Help | ArcGIS for Desktop. List Feature Classes is great but only lists the FCs at the root level of your geodatabase. If you have feature datasets containing additional feature classes, they will be missed by list feature classes. Here's how to use Walk for your script:

workspace = r"Path\xyz.sde"
fcs = []

walk = arcpy.da.Walk(workspace, datatype="FeatureClass")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        fcs.append(os.path.join(dirpath, filename))

Then you can print (or do whatever you like with) the geometry types like this:

for fc in fcs:
    geometryType = arcpy.Describe(fc).shapeType
    print(geometryType)

As for writing to the CSV, have a look at Using the CSV module in Python. I've never used it but it looks pretty straightforward.

Micah