Hi All,
Here's what I'm trying to do:
- A user submits a 1-feature line feature class (`project_fc` in the excerpt below)
- 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.
- 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,