arcpy DeleteFeatures not fully deleting features when run as web tool

775
5
Jump to solution
03-31-2022 02:50 PM
DarrenConly
Occasional Contributor

Hi All,

Here's what I'm trying to do:

  1. A user submits a 1-feature line feature class (`project_fc` in the excerpt below)
  2. In an APRX map, I have a feature class showing a line layer. I want the line in the APRX to be updated to reflect the user's submitted line. To do that, I run DeleteFeatures on the line layer in the APRX, then use arcpy.Append to insert the user's submitted line to the feature class in the APRX. The idea is that I have a map with a nice line symbology, and when the user runs the tool, the line geometry in the map is update to show the line they drew.
  3. After doing the line geometry replacement, export a PNG of the map showing the user's line.

This process works great if I run the tool in Arc Pro 2.9.2, but if I publish the tool and run through Map Viewer, the user's line does not replace the line in the APRX, but instead is appended to the APRX's line feature class--not what I want. I want it to replace.

Code excerpts below illustrate what I'm talking about. All of the arcpy.AddMessage() commands were added so I could see if things were working as I expected.

 

# self.project_fc is the line submitted by the user
# self.proj_line_template_fc is the feature class shown in the APRX map.
# when the tool runs, I want the line in self.proj_line_template_fc to be replaced with the line in self.project_fc

arcpy.DeleteFeatures_management(self.proj_line_template_fc) # delete whatever features were in the display layer
arcpy.AddMessage(f"{arcpy.GetCount_management(self.proj_line_template_fc)[0]} features in line template feature class after running delete features")
            
arcpy.Append_management([self.project_fc], self.proj_line_template_fc, "NO_TEST") # then replace those features with those from user-drawn line

arcpy.AddMessage(f"After appending, now have {arcpy.GetCount_management(self.proj_line_template_fc)[0]} features in the line template feature class")

geoms_project_fc = []
with arcpy.da.SearchCursor(self.project_fc, ['SHAPE@']) as cur:
    for row in cur:
        geoms_project_fc.append(row[0])

geoms_template_fc = []
with arcpy.da.SearchCursor(self.proj_line_template_fc, ['SHAPE@']) as cur:
    for row in cur:
        geoms_template_fc.append(row[0])

arcpy.AddMessage(f"{len(geoms_project_fc)} geometries items in the project line fc.")

arcpy.AddMessage(f"{len(geoms_template_fc)} geometries items in the project line TEMPLATE fc.")

arcpy.AddMessage(f"And when running GetCount on line template fc, you have {arcpy.GetCount_management(self.proj_line_template_fc)[0]} features")

 

 

If I run the above code in Arc Pro, the resulting messages show that the process worked as planned, i.e., the APRX line layer had it's geometry swapped out for that of the line submitted by the user:

 

# Messages when running in Arc Pro
0 features in line template feature class after running delete features
After appending, now have 1 features in the line template feature class
1 geometries items in the project line fc.
1 geometries items in the project line TEMPLATE fc.
And when running GetCount on line template fc, you have 1 features

 

 

However, if I publish the tool as a web tool and run in Map Viewer, it doesn't work correctly. As the messages show below:

  • It appears that DeleteFeatures worked as expected
  • After appending, it looks like there is only 1 feature in the APRX line layer (this seems good!)
  • BUT, if I run search cursors to count the number of rows in the APRX line layer, it looks like there are still 2 rows, not 1--this is very weird because running GetCount_Management says there is only 1 row, but the geometry count via search cursor says there are 2 features.

 

# Messages if I run as a web tool in map viewer:
0 features in line template feature class after running delete features
After appending, now have 1 features in the line template feature class
1 geometries items in the project line fc..
2 geometries items in the project line TEMPLATE # THIS SHOULD ONLY BE 1, NOT 2!
And when running GetCount on line template fc, you have 1 features # THIS SHOULD MATCH THE GEOMETRY COUNT, BUT IT DOES NOT

 

 

 

The problem with this error is that the map PNG produced by the tool does not show the user's line; rather, it shows whatever line was already in the APRX line layer.

Has anyone come across this issue?

 

Thank you,

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DarrenConly
Occasional Contributor

Hi Amanda,

I seem to have found a solution! Instead of running DeleteFeatures, I instead run TruncateTable. TruncateTable in my case is the same as DeleteFeatures (except unlike DeleteFeatures it doesn't have the option to only remove a subset of rows--it removes all rows, which is what I want).

As is my experience with other ESRI functions, this doesn't explain why DeleteFeatures doesn't work.

And to your question "why don't you make the line template a feature layer?", I technically could publish as a hosted feature layer, but the line template feature class is one FC within a registered SQL Server database containing about a dozen other FCs (totalling several GB of data) that are part of this same project and appear in the same APRX file. It would be interesting to try this out, and I might do so if after additional testing the TruncateTable approach ends up not working.

 

View solution in original post

5 Replies
ABishop
MVP Regular Contributor

Hello Darren,

Just to clarify.. you are doing the following:

1. Running your geoprocessing tool in ArcGIS Pro Desktop 2.9.2 against a feature class in a versioned geodatabase.

2. Publishing the geoprocessing tool as a web tool and then opening the Map Viewer in ArcGIS Online to run the web tool against the same feature class in a versioned geodatabase?

Amanda Bishop, GISP
0 Kudos
DarrenConly
Occasional Contributor

Hi Amanda,

Re: question 1 - The template line that I'm running DeleteFeatures on is stored in an Enterprise Geodatabase in SQL Server using "Traditional" versioning method.

Re: question 2 - correct: I am running the web tool (and specifically DeleteFeatures) against the same template line in the versioned database.

 

ABishop
MVP Regular Contributor

Hello Darren,

The first part seems normal to me but the second where you are running a web tool in the online Map Viewer against a feature class in the versioned database seems a bit odd.  Is there a reason you aren't publishing this feature class as a feature layer and then running the DeleteFeatures web tool against the feature layer?  

Amanda Bishop, GISP
0 Kudos
DarrenConly
Occasional Contributor

Hi Amanda,

I seem to have found a solution! Instead of running DeleteFeatures, I instead run TruncateTable. TruncateTable in my case is the same as DeleteFeatures (except unlike DeleteFeatures it doesn't have the option to only remove a subset of rows--it removes all rows, which is what I want).

As is my experience with other ESRI functions, this doesn't explain why DeleteFeatures doesn't work.

And to your question "why don't you make the line template a feature layer?", I technically could publish as a hosted feature layer, but the line template feature class is one FC within a registered SQL Server database containing about a dozen other FCs (totalling several GB of data) that are part of this same project and appear in the same APRX file. It would be interesting to try this out, and I might do so if after additional testing the TruncateTable approach ends up not working.

 

ABishop
MVP Regular Contributor

Good job Darren!  Not sure why I didn't think of the truncate tool.  I actually use that one quite a bit in my daily workflows.  Glad you got it working!  Yes, I would try publishing the line template as a hosted feature layer with edit capabilities if that is your main platform for editing and running the GP tool.

Amanda Bishop, GISP