Select to view content in your preferred language

Technical: Adding data to a feature service via script

1000
0
04-01-2021 08:43 AM
Labels (1)
TomBaker
Esri Regular Contributor
2 0 1,000

The use of Raspberry Pis are increasingly common in schools - especially in STEM, Maker, and field experience courses.  Raspberry Pis (version 4 at the time of this blog) hold the computing power of a laptop - at a fraction of the cost.  They can also run off of 5V battery power and have wifi built-in.  In short, they can useful for collecting data in and around school.  Oh, and did I mention there are dozens if not hundreds of cheap sensors that can be plugged directly into the device for data collection?

Raspi-PGB001.png

This isn't a new technique at all, but we are seeing more sensors and increased computing capacity within the Pi platform - making it increasingly interesting.  I started writing on this about two years ago.  You can see how far we've come in that time.

By way of a quick example, I created a Survey123 form with a grade (numeric) and short name (text) fields.  I set the permissions to allow anyone to contribute data (without logging in).  Survey123 creates some ArcGIS Online components for you when you create a new survey.  Here, I'm only going to focus on the "field_worker"  feature service it creates.  Using a little Python code and the point layer's "Add Feature" REST functionality, I can send data directly into my field_worker feature service with a few lines of code.  In this case, that code looks like:

import requests
import json

url = 'https://services.arcgis.com/BG6nSlhZSAWtExvp/ArcGIS/rest/services/survey123_1570d91b042744f8af17caad...'
params={"f":"pjson","token":"","rollbackOnFailure":"false","features":'{"attributes" : {"shortname" : "test_user_name"}}'}
x = requests.post(url, params=params)


In the first two lines we import two basic Python libraries (json and requests). In the next line, we identify the URL of the "Add Feature" function associated with our feature service's REST endpoint. Parameters allow us to specify the data to pass.  You'll see a few non-attribute parameters like "f", "token", and "rollbackOnFailure".  For starting out, just keep these as-is and change the attribute "short name" to the attribute in your feature service.

As my colleague, @Kylie has noted, this code can also be converted to run inside of a webhook tool like Microsoft Power Automate or Integromat (using the HTTP module).  In a webhook-based case, you may be using the script (with a different endpoint) to update a feature service's data rather than just adding it.

If you or your students are into the STEM, Maker, coder, or IoT learning scenes, be sure to turn on your students' ArcGIS Online Notebook access and check out the Python API!