I am trying to build a Python script to pull off features from a GeoEvent streaming service but I have not found ANY examples of how to use the StreamLayer class. Not even Esri has samples for how to use it.
Hi Weldon,
You're in luck - I actually wrote on article on this exact topic not too long ago. I believe our content team is still working to make it public-facing, but I'll paste a copy for you below. This is a simple example that utilizes one of our sample GeoEvent Servers. The events are printed for illustrative purposes, but you may of course modify the function to process the received events however you wish:
In order to use the subscribe method, you must have the following packages installed: autobahn, twisted, pyOpenssl, and service_identity.If you already have the ArcGIS API for Python installed, then you may already have some of the packages installed and just need to install twisted and autobahn:conda install twistedconda -c conda-forge install autobahnThe subscribe method expects at minimum a callback function. The below sample illustrates how you can define a callback function that print events (JSON) as they come in. This is a very basic example - you could further manipulate the JSON to view only certain information or even perform additional processing/data analysis if desired.<SPAN class="keyword token">from</SPAN> arcgis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>realtime <SPAN class="keyword token">import</SPAN> StreamLayer <SPAN class="keyword token">import</SPAN> ssl<SPAN class="punctuation token">,</SPAN> json ssl<SPAN class="punctuation token">.</SPAN>_create_default_https_context <SPAN class="operator token">=</SPAN> ssl<SPAN class="punctuation token">.</SPAN>_create_unverified_context url <SPAN class="operator token">=</SPAN> <SPAN class="string token">"https://geoeventsample1.esri.com:6443/arcgis/rest/services/AirportTraffics/StreamServer"</SPAN> streamLayer <SPAN class="operator token">=</SPAN> StreamLayer<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">callback</SPAN><SPAN class="punctuation token">(</SPAN>event<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> event_as_json <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>event<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>event_as_json<SPAN class="punctuation token">)</SPAN> streamLayer<SPAN class="punctuation token">.</SPAN>subscribe<SPAN class="punctuation token">(</SPAN>callback<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Note: This script may not run well in a Jupyter Notebook - if you run the code and decide to stop the script, the next time you attempt to run the script you may encounter errors like these:--------------------------------------------------------------------------- ReactorNotRestartable Traceback (most recent call last) <ipython-input-2-7cd591176ebd> in <module> 10 print(event) 11 ---> 12 streamLayer.subscribe(callback) When this happens, restart the kernel and you should be able to re-execute. Otherwise, the script runs consistently from the command line.
In order to use the subscribe method, you must have the following packages installed: autobahn, twisted, pyOpenssl, and service_identity.
If you already have the ArcGIS API for Python installed, then you may already have some of the packages installed and just need to install twisted and autobahn:
conda install twistedconda -c conda-forge install autobahn
The subscribe method expects at minimum a callback function. The below sample illustrates how you can define a callback function that print events (JSON) as they come in. This is a very basic example - you could further manipulate the JSON to view only certain information or even perform additional processing/data analysis if desired.
<SPAN class="keyword token">from</SPAN> arcgis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>realtime <SPAN class="keyword token">import</SPAN> StreamLayer <SPAN class="keyword token">import</SPAN> ssl<SPAN class="punctuation token">,</SPAN> json ssl<SPAN class="punctuation token">.</SPAN>_create_default_https_context <SPAN class="operator token">=</SPAN> ssl<SPAN class="punctuation token">.</SPAN>_create_unverified_context url <SPAN class="operator token">=</SPAN> <SPAN class="string token">"https://geoeventsample1.esri.com:6443/arcgis/rest/services/AirportTraffics/StreamServer"</SPAN> streamLayer <SPAN class="operator token">=</SPAN> StreamLayer<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">callback</SPAN><SPAN class="punctuation token">(</SPAN>event<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> event_as_json <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>event<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>event_as_json<SPAN class="punctuation token">)</SPAN> streamLayer<SPAN class="punctuation token">.</SPAN>subscribe<SPAN class="punctuation token">(</SPAN>callback<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Note: This script may not run well in a Jupyter Notebook - if you run the code and decide to stop the script, the next time you attempt to run the script you may encounter errors like these:
--------------------------------------------------------------------------- ReactorNotRestartable Traceback (most recent call last) <ipython-input-2-7cd591176ebd> in <module> 10 print(event) 11 ---> 12 streamLayer.subscribe(callback)
When this happens, restart the kernel and you should be able to re-execute. Otherwise, the script runs consistently from the command line.
Kind regards,
Earl
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.