Select to view content in your preferred language

How do I delete a feature from a ArcGIS for Server Feature server using arcpy?

570
0
05-18-2018 11:02 AM
ShawnKeizer1
Emerging Contributor

I have a feature service created using ArcGIS for Server and using python i want to select feature(s) based on OBJECTID and delete them. The user will be selecting the features to delete in Arcmap. I know i am able to create a local copy, delete them manualy and sync back to the service but i want to do all the lifting via python. Any help would be appreciated.

Below is what i have so far the section "Choose orginal features to delete" is what i can not get working, the rest of the code works fine.

import arcpy
import os
from arcpy import env
env.overwriteOutput = True

# Variables...
in_datavarPOI = r'U:\Tools\SDE_Connection_Files\RedDeerNorthSaskRegion.GDB@genesis-sde.sde\RedDeerNorthSaskRegion.GDB.POI'
in_datavarTKS = r'U:\Tools\SDE_Connection_Files\RedDeerNorthSaskRegion.GDB@genesis-sde.sde\RedDeerNorthSaskRegion.GDB.Tracks'
inLyrPOI = 'AEP Field Data Collection/Points of Interest'
inLyrTKS = 'AEP Field Data Collection/Tracks'
sdeconnection = r'U:\Tools\SDE_Connection_Files\RedDeerNorthSaskRegion.GDB@genesis-sde.sde'
expChce = arcpy.GetParameterAsText(0)
out_location = arcpy.GetParameterAsText(1)
out_name = arcpy.GetParameterAsText(2)
delFeat = arcpy.GetParameterAsText(3)

# Create a FGDB to store copies data...
arcpy.AddMessage('\n... Creating new File Geodatabase')
arcpy.CreateFileGDB_management(out_location, out_name)

# Choice of data to export
if expChce == 'Points of Interest':
inData = in_datavarPOI
inLyr = inLyrPOI
else:
inData = in_datavarTKS
inLyr = inLyrTKS

# Copy features and attachments to new GDB...
arcpy.AddMessage('\n... Coping features and attachments to the new File Geodatabase')
arcpy.Copy_management(in_data=inData,
out_data=os.path.join(out_location + os.sep + out_name +".gdb", out_name))

# Add New Featureclass to map and symbolize
arcpy.AddMessage('\n... Adding Copied features to the current map')
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
layer = arcpy.mapping.Layer(os.path.join(out_location + os.sep + out_name +".gdb", out_name))
arcpy.mapping.AddLayer(df, layer, "TOP")

# List selected data...
selFeat = []
with arcpy.da.SearchCursor(inLyr, 'OBJECTID') as cursor:
for row in cursor:
selFeat.append(row[0])
del cursor

# Delete features that were not selected...
sql = ''
for sel in selFeat:
sql += 'OBJECTID = ' + str(sel) + ' OR '
arcpy.SelectLayerByAttribute_management(out_name, selection_type='NEW_SELECTION', where_clause=sql[:-4])
arcpy.SelectLayerByAttribute_management(out_name, selection_type='SWITCH_SELECTION')
arcpy.DeleteFeatures_management(out_name)

# Choose to delete orginal features
if delFeat == 'true':
#Start editing
print "Initiating editing"
edit = arcpy.da.Editor(sdeconnection)
edit.startEditing()
edit.startOperation()

#Test Cursor
print "Testing cursor"
Cursor = arcpy.da.UpdateCursor (inData, ["ObjectID"], sql[:-4])
for row in Cursor:
print row[0]
del row
del Cursor

#Stop/save edits
edit.stopOperation()
print "Stopping editing"
edit.stopEditing("True")
else:
arcpy.SelectLayerByAttribute_management(in_layer_or_view=inLyr, selection_type='CLEAR_SELECTION')

arcpy.AddMessage('\n... Finished coping features :)\n')

0 Kudos
0 Replies