Select to view content in your preferred language

Append selected records and calculate value for one field

1550
13
11-12-2024 08:39 AM
AmyRoust
Frequent Contributor

I'm new to using the ArcGIS API for Python in an ArcGIS Online Notebook and am looking for help to identify the right function(s) for a data maintenance script I'm migrating from a scheduled task on a server.

The start of the script works fine: I select a small number of records from a hosted feature layer. (If it matters, the hosted feature layer is part of a Survey123 survey).

I'm struggling with finding the function(s) to:

1. Append those selected records to another hosted feature layer, and

2. On those newly appended records, update the value in the status field to 'Not Reviewed'. The status field does not exist in the source layer, but it does in the target layer. 

These two steps don't have to be in this order. I'm fine with exporting the records, adding a status field, calculating that 'Not Reviewed' value and then appending the updated records to my target layer.

Appreciate any hints that the community can offer!

Tags (2)
0 Kudos
13 Replies
AmyRoust
Frequent Contributor

I'm a lot closer now after making quite a few changes. Now I'm getting the error message: 

A general error occurred: Circular reference detected
ERROR: A general error occurred: Circular reference detected

 I believe that the error is in line 42. I replaced some info with ... for security purposes.

 

from arcgis.gis import GIS
gis = GIS("home")

# Import arcpy module
from arcgis.gis import GIS
from arcgis.features import FeatureLayer, GeoAccessor # might not need that last one?
import time, arcpy, os, datetime

# Variables
s123Layer = r'https://services.arcgis.com/.../FeatureServer/0'
report_copy_url = r'https://services.arcgis.com/.../FeatureServer/0'

try:
    item = gis.content.get("...")
    feature_layer = item.layers[0]
    field_name = "ReportDate"
    query_result = feature_layer.query(out_statistics=[{"statisticType": "max", "onStatisticField": field_name}])
    milliseconds = query_result.features[0].attributes[f"MAX_{field_name}"]
    date_time = datetime.datetime.utcfromtimestamp(milliseconds / 1000.0)
    max_date = date_time.strftime('%Y-%m-%d %H:%M:%S')
    print("Most recent report date in ...:", max_date)

    feature_layer = FeatureLayer(s123Layer, gis=gis)
    query = f"submission_date_and_time > timestamp '{max_date}'"
    selected_features = feature_layer.query(where=query)
    print(f"Number of new features to append to ...: {len(selected_features)}")
    
###### SCRIPT WORKS TO THIS POINT #######
    
    if len(selected_features.features)>0:
        fm = [{'ObservationDate':'ReportObservationDate',
                         'CampsiteDescription':'ReportCampsiteDescription',
                         'FollowUpRequest':'ReportFollowUpRequest',
                         'SubmitterName':'ReportSubmitterName',
                         'PreferredContact':'ReportSubmitterPreferredContact',
                         'EmailAddress':'ReportSubmitterEmailAddress',
                         'PhoneNumber':'ReportSubmitterPhoneNumber',
                         'submission_date_and_time':'ReportDate'
                        }]
        
        targetLayer = FeatureLayer(report_copy_url,gis=gis)
        targetLayer.append(selected_features.features, field_mappings = fm, return_messages = True)

    else:
        print("No new reports to load into ...")

except Exception as e:
    print(e)
    arcpy.AddError(str(e))

 

 @JakeSkinner  @Clubdebambos  - do you see any issues?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

@AmyRoust I see your importing arcpy.  Are you using an Advanced runtime in AGOL Notebooks?

0 Kudos
AmyRoust
Frequent Contributor

Good question! I believe I am:

AmyRoust_0-1733927378819.png

 

0 Kudos
JakeSkinner
Esri Esteemed Contributor

@AmyRoust since you are using arcpy, I would use these tools to perform the append.  You can perform these steps in ArcGIS Pro and copy the code to your Notebook.  For example, use the Make Feature Layer tool to create a feature layer with the query.  Then, use the Append tool to append the records with the field mapping configured.  You can easily copy the code you need by clicking the dropdown arrow next to Run:

JakeSkinner_0-1734350662042.png

 

0 Kudos