Is there a way to use python get the geometries of the feature classes in sde?

2050
17
10-01-2019 07:03 AM
RPGIS
by
Occasional Contributor III

Hi,

So I am working on a script to automate a process for populating certain fields in several feature classes, but in order to do so I need to determine what kind of geometries these feature classes are. Depending on what kind of feature classes they are, the script runs a certain way. The issue I am having with the script is I am not getting the list of geometries for the feature classes. The feature classes are in sde (not sure if that makes a difference) and I keep getting a list, but not a list of the geometries. Any help would be greatly appreciated.

import arcpy

arcpy.env.workspace = r'Database Connections\Storm Water.sde\STORM.GISADMIN.Storm_Network'

fcs = []
descList = []
fclist = arcpy.ListFeatureClasses()
for fc in fclist:
    fcs.append(fc)
for fc in fcs:
    desc = arcpy.Describe(fc)
    descList.append(desc)
0 Kudos
17 Replies
George_Thompson
Esri Frequent Contributor

Looks like a combination of the https://pro.arcgis.com/en/pro-app/arcpy/functions/describe.htm & https://pro.arcgis.com/en/pro-app/arcpy/functions/featureclass-properties.htm

import arcpy

# Create a Describe object from the feature class #

desc = arcpy.Describe("C:/data/arch.dgn/Point")

# Print some feature class properties #

print("Feature Type: " + desc.featureType)

print("Shape Type : " + desc.shapeType)

print("Spatial Index: " + str(desc.hasSpatialIndex))

--- George T.
JoshuaBixby
MVP Esteemed Contributor

You are on the right track.  The code snippet you provided is created a list of Describe objects.  What you need to do is lookup the shapeType property of the object to determine what type of geometry object is stored in the feature class.

Try:

descList.append(desc.shapeType)
0 Kudos
RPGIS
by
Occasional Contributor III

Thanks Josh and George.

I am trying to automate several different tasks and the reason I was trying to determine the geometries of the feature classes is to run a different scenario depending on the type of geometry exists. I haven't worked with sde databases much and I have automated several tasks in the past before but not to this degree.

Also, I have a mixture of feature classes where the feature classes exist both in a database and dataset. I tried researching different options to extract the names of the feature classes but for some reason I get either a partial name or a partial path. Is there a way to specifically get the name of the feature class given the full path of the feature class.

Robert

0 Kudos
MicahBabinski
Occasional Contributor III

I think the examples shown for Walk—Help | ArcGIS for Desktop will get you what you need in terms of full paths. From there, you can use either the name or baseName property of your describe object: Describe object properties—Help | ArcGIS Desktop.

Hope this helps!

Micah

JoshuaBixby
MVP Esteemed Contributor

See Micah Babinski‌'s reply.

0 Kudos
RPGIS
by
Occasional Contributor III

Thanks Micah,

I had some issues with the overall path names since I had feature classes in both the database and the dataset within the database. I was able to figure out a solution but it was kind of lengthy. I tried to shorthand it but that didn't turn out to well. Here is the solution I came up with when I had the full path names. If there is a better way to shorthand this I would definitely be interested.

import arcpy
import os

workspace = r'Database Connections\Connection to dcp-gisapp1tst.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))
        
for fc in fcs:
    geometryType = arcpy.Describe(fc).shapeType
    fcsname = os.path.basename(fc)
    name = os.path.splitext(fcsname)
    x = geometryType
    y = name[1].lstrip('.')
    print '{} is a {}'.format(y, x)
MicahBabinski
Occasional Contributor III

Hey Robert,

The important thing is that it works for what you need it to do. Your script looks good to me. Are you just trying to write it in fewer lines so you have a shorter script? It would be possible to accomplish what you are doing in the 'for' loop at line 13 in the 'for' loop before that one, but there's nothing wrong with the way you've done it!

If you just want to write it in fewer lines, you could do this:

import arcpy, os

workspace = r'Database Connections\Connection to dcp-gisapp1tst.sde'

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

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        fc = os.path.join(dirpath, filename)
        print("{} is a {}".format(fc.split(".")[-1], arcpy.Describe(fc).shapeType)

Micah

RPGIS
by
Occasional Contributor III

Hey Micah,

I was trying to figure out if there are ways to write the same thing with fewer lines. The script worked for what I needed but I wasn't sure if there was a way to write it in a simpler fashion. I know writing a simpler script is not as important as a working script, but knowing that there are ways to simplify what I have already done would make searching through the code a little easier(and streamlined).

Thanks for your help Micah,

Robert

MicahBabinski
Occasional Contributor III

Totally - I get it. I applaud your efforts to simplify your code - it's easier to understand, maintain, and all around better that way.

0 Kudos