I have a couple of hardware GPS trackers which we install on vehicles. The gps points from these trackers arrive on one of my servers, from where I want to push the locations into Arcgis Online.
I have tried this piece of Python code to append a track point to the track layer:
gis = GIS(username="redacted", password="redacted")
izinto_item = gis.content.get("redated")
feature_layer = izinto_item.layers[1]
new_feature = {"geometry": {"x": redacted, "y": redacted, "spatialReference": {"wkid": 4326, "latestWkid": 4326}}, "attributes": {"activity": 0, "altitude": 511.228589377366, "app_id": "izinto", "battery_percentage": 55, "battery_state": 1, "course": None, "device_id": "test", "floor": None, "horizontal_accuracy": 46.4931761266777, "location_source": None, "location_timestamp": 1636544141380, "session_id": "test", "signal_strength": None, "speed": 0.646095156669617, "vertical_accuracy": 7.64551688182702, "full_name": "redacted", "category": None, "created_user": "redacted", "created_date": 1636544328798, "last_edited_user": "redacted", "last_edited_date": 1636544328798}}
feature_layer.edit_features(adds=[new_feature])
Running this code however results in an error:
Traceback (most recent call last):
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/track.py", line 52, in <module>
main()
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/track.py", line 48, in main
feature_layer.edit_features(adds=[new_feature])
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/venv/lib/python3.8/site-packages/arcgis/features/layer.py", line 2827, in edit_features
return self._con.post_multipart(path=edit_url, postdata=params)
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/venv/lib/python3.8/site-packages/arcgis/gis/_impl/_con/_connection.py", line 863, in post_multipart
return self._handle_response(
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/venv/lib/python3.8/site-packages/arcgis/gis/_impl/_con/_connection.py", line 625, in _handle_response
self._handle_json_error(data["error"], errorcode)
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/venv/lib/python3.8/site-packages/arcgis/gis/_impl/_con/_connection.py", line 648, in _handle_json_error
raise Exception(errormessage)
Exception:
This operation is not supported.
(Error Code: 400)Process finished with exit code 1
What would be the proper way to upload GPS tracks that are obtained from a hardware GPS tracker device?
Does excluding the created_user, created_date, last_edited_user, and last_edited_date attributes from the feature make a difference? Those attributes are typically automatically set by the feature service if they were added to the feature service when editing was enabled.
Thanks for the suggestion @J_R_Matchett. I've done the following:
new_feature = {"geometry": {"x": 12.345, "y": -12.345,
"spatialReference": {"wkid": 4326, "latestWkid": 4326}},
"attributes": {"altitude": 511.228589377366,
"battery_percentage": 55, "device_id": "test",
"location_timestamp": 1636544141380, "signal_strength": None,
}
}
feature_layer.edit_features(adds=[new_feature])
But now I get a different error:
Traceback (most recent call last):
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/track.py", line 59, in <module>
main()
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/track.py", line 55, in main
feature_layer.edit_features(adds=[new_feature])
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/venv/lib/python3.8/site-packages/arcgis/features/layer.py", line 2827, in edit_features
return self._con.post_multipart(path=edit_url, postdata=params)
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/venv/lib/python3.8/site-packages/arcgis/gis/_impl/_con/_connection.py", line 863, in post_multipart
return self._handle_response(
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/venv/lib/python3.8/site-packages/arcgis/gis/_impl/_con/_connection.py", line 625, in _handle_response
self._handle_json_error(data["error"], errorcode)
File "/home/jpmeijers/PycharmProjects/arcgis-update-feature/venv/lib/python3.8/site-packages/arcgis/gis/_impl/_con/_connection.py", line 648, in _handle_json_error
raise Exception(errormessage)
Exception:
This operation is not supported.
(Error Code: 400)Process finished with exit code 1
I'm starting to wonder if this Tracks feature is only supported by the mobile apps, and not third party integrations. Does anyone have a good suggestion how I can upload GPS tracker data into Arcgis Online?
I have now create a new item with only a points layer. I can successfully upload GPS tracker points to this points layer, using application credentials.
For reference my full code
import datetime
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
import requests # pip install requests
def get_token():
params = {
'client_id': "redacted",
'client_secret': "redacted",
'grant_type': "client_credentials"
}
request = requests.get('https://www.arcgis.com/sharing/rest/oauth2/token',
params=params)
response = request.json()
token = response["access_token"]
return token
def main():
# token = get_token()
# gis = GIS(token=token,
# referer="https://redacted")
gis = GIS(username="redacted", password="redacted")
installation_feature = None
izinto_item = gis.content.get("redacted")
print(izinto_item)
for feature_layer in izinto_item.layers:
if feature_layer.properties['geometryType'] == 'esriGeometryPoint':
"""
Time location_timestamp Date
Hardware Serial Number hardware_serial String
Installation ID installation_id Integer
Logger ID logger_id Integer
Site ID site_id Integer
GPS Status gps_status Integer
GPS Fix gps_fix Integer
Battery battery Double
Moving moving Integer
Button Pressed button Integer
Fall detected fall Integer
Heading heading Double
Speed speed Double
:return:
"""
new_feature = {
"geometry": {
"x": 12.34,
"y": -12.34,
"spatialReference": {"wkid": 4326, "latestWkid": 4326}},
"attributes": {
"location_timestamp": datetime.datetime.now(),
"hardware_serial": "test",
"installation_id": 79,
"logger_id": 1,
"site_id": 32,
"gps_status": 3,
"gps_fix": 0,
"battery": 100,
"moving": 0,
"button": 0,
"fall": 0,
}
}
feature_layer.edit_features(adds=[new_feature])
if __name__ == '__main__':
main()