Select to view content in your preferred language

How to obtain a location of SDE feature class using python to edit selected features throuhg notebooks

193
1
06-06-2024 01:28 PM
Markbe_utco
New Contributor III

I am trying to remove the interior vertices from a set of lines that are selected within a map using a notebook.

Everything seems to work with the feature classes are housed within file geodatabase.

Unfortunately the script crashes when features within an SDE feature classes is selected.

How

 

import arcpy
import sys
import os

p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps("Edits")[0]
sel_source = []
for src in m.listLayers():
    if src.getSelectionSet():
        listOfLineOIDs = src.getSelectionSet()
        sel_source.append(src.dataSource)

###  Make sure only one layer is selected and halt script if too many or none are selected
if len(sel_source) > 1:
    print ("********************************")
    print ("******   To many layers   ******")
    print ("********************************")
    sys.exit()

elif len(sel_source) < 1:
    print ("*********************************")
    print ("***   Please select a layer   ***")
    print ("*********************************")
    sys.exit()

xx = 0
for source in sel_source:
    print ("Selected Layer Source >>> "+ sel_source[xx])
    fc = sel_source[xx]
    xx = xx + 1

### Create a where_clause for UpdateCurson to edit only the selected features
whereClause = ""
OIDcounter = len(listOfLineOIDs)
for OID in listOfLineOIDs:
    if OIDcounter > 1:
        whereClause = whereClause + "OBJECTID = " + str(OID) + " OR "
    elif OIDcounter == 1:
        whereClause = whereClause + "OBJECTID = " + str(OID)
    OIDcounter = OIDcounter - 1

### Remove the non-endpoint vertices from the selected lines
with arcpy.da.UpdateCursor(fc,["SHAPE@"],where_clause = whereClause) as cursor:
    for row in cursor:
        #print("x")
        #loop through parts
        for part in row[0]:
            #print("y")
            count = 0
            # loop through verticies
            for pnt in part:
                #print("z")
                count = count + 1
                if count >= 3:
                    arr = row[0].getPart(0)
                    arr.remove(1)
                    newLine = arcpy.Polyline(arr)
                    row[0] = newLine
                    cursor.updateRow(row)

 

I get this error message

RuntimeError: cannot open 'Server=xxxxx,Instance=aaaaa:1111/orcl,Database Platform=Oracle,User=survey,Version=SDE.DEFAULT (Traditional),Authentication Type=Database Authentication,Feature Dataset=SURVEY.DATASET,Dataset=SURVEY.FEATURECLASS_Lines'

 

I'm just not sure how to obtain the proper reference to the SDE Feature Class so the edits can be applied to the selected features.

Tags (3)
0 Kudos
1 Reply
awgeezrick
New Contributor III

Good Morning,

Have you tried to access the SDE FC directly? rather than as an item in the map list layer? When doing SDE work I generally tend to have my workspace env set to the SDE (using sde connection). Connecting via an SDE connection ensures that you have the proper privileges to do what you need to do (i.e. edits, etc.).

Just a thought.

 

Mark

0 Kudos