How To: Auto-populate attribute fields in a hosted feature layer using Python
When adding new features to data layers, attributes can be added and edited too. When working with a geodatabase, attribute rules can be used to automatically populate attributes, restrict invalid edits during edit operations, and perform quality assurance checks on existing features. Feature layers can also be published with attribute rules as registered data to your Enterprise environment. By doing this, the attribute rules are applied when edits are made to the referenced layer.
Note: Attribute rules are not yet supported in ArcGIS Online. When collecting data to a layer hosted on ArcGIS Online, it may be desirable to automatically populate an attribute field. There is the option to assign default values to an attribute field, use the feature templates that enable assigning different default values for each new feature type, or leverage Smart Form capabilities in ArcGIS Field Maps. However, these options do not allow for dynamic calculation. For instance, it may be necessary to calculate the values in an attribute field using the values entered in another field. Some apps such as ArcGIS Survey123 allow using calculations to do this during the data collection. This functionality is also on the roadmap for ArcGIS Field Maps and will be added in the future releases of the app. See the Esri Community blog: Support for Attribute rules in ArcGIS Online This can be also done post data collection by editing data in the attribute table. You can use Field Calculator in ArcGIS Online or Desktop to calculate values for any of the attribute fields.
Use ArcGIS API for Python to update the attributes for the features in a hosted feature layer. The sample script below calculates the values in a field named "TargetField" for a subset of features (with dates after Jan 15, 2022 in the "DateField" field) by concatenating existing values in two other fields (i.e. "Field1" and "Field2"). If the "TargetField" has existing values, they are overwritten by the new values after updating features with this script.
- ArcGIS API for Python: Updating features in a feature layer
#log in to Portal/ArcGIS Online from arcgis.gis import GIS gis = GIS('https://arcgis.com/', "Username", "Password") #find the hosted feature layer hfl = gis.content.get("ItemID") hflayer =hfl.layers #query the features that need to be updated fset = hflayer.query(where = 'DateField > DATE \'2022-01-15\'') # updating selected features' attribute for f in (fset.features): f.attributes['TargetField'] = str(f.attributes['Field1']) + "_" + str(f.attributes['Field2']) hflayer.edit_features(updates = fset.features) print ('Attributes were successfully updated!')
This process can be automated by running the script using a third-party app like Windows Task Scheduler or use ArcGIS Notebooks Tasks:
ArcGIS Online: Schedule a notebook task
- ArcGIS Pro: Introduction to attribute rules
- ArcGIS Pro: Share datasets with attribute rules
- Esri Australia Blog: FAQ: Are attribute rules on referenced feature layers supported for offline editing?
- ArcGIS Online: Add or delete a field
- ArcGIS Field Maps: Configure the form
Last Published: 9/15/2022
Article ID: 000027245
Software: ArcGIS API for Python 2.0.1, 2.0, 1.9.1, 1.9, 1.8.5, 1.8.4, 1.8.3, 1.8.2, 1.8.1, 1.8.0, 1.7.1, 1.7.0, 1.6.1, 1.6 ArcGIS Online Current ArcGIS Pro 3.0.1, 3.0, 2.9.4, 2.9.3, 2.9.2, 2.9.1, 2.9, 2.8.7, 2.8.6, 2.8.5, 2.8.4, 2.8.3, 2.8.2, 2.8.1, 2.8