We all know the power of using feature services with ArcGIS.com to share our data and maps. These maps become even more powerful when the content automatically updates. GeoRSS feeds allow you to quickly pull content into your map. Well, what happens if you find an API or some geo-enabled information that doesn’t provide a GeoRSS feed but you want to get that information into your feature service? Python can act as the “glue” to combine the geo-information from the web into your hosted feature service.
Dave MacLean from the Centre of Geographic Sciences (COGS) has a webmap that captures tweets and photos from the astronauts on the International Space Station (ISS). Recently he worked with us (at Esri) to write a Python script that captures the ISS location from a website supplying the location as JSON and then pushes the relevant information into a hosted feature service. The current ISS location on his map allows viewers to prepare for an ISS sighting.
Dave found a website that provides the real-time location of the ISS making the information available in JSON, which the Python script parses. The script runs on an old laptop with a 60-second delay (You could investigate a cloud or other hosting solution to run your script if you can’t dedicate a computer to running it). With the script updating the feature service, he used the refresh layer method while building his web application to refresh the layer every minute.
You can download, fork or just explore the Python code we wrote to upload the ISS location from the github repo. Detailed instructions to setup and deploy the script are also provided. Here are some of the key points you should note about the script before modifying it to fit your workflows.
- The script requires an existing feature service with a point layer and specific attributes. It the layer is empty, temporary geometry is inserted into the layer as the script makes updates on existing features. Note the hosted layer and point location are in WGS84. Make sure the spatial reference of your data matches the layer when posting updates.
- A user and password which is either an owner or administrator is required. The credentials to obtain a token are saved inside the python file as clear text. Be careful who you share your script with.
- The working parts of the script are placed inside a try/except with a loop that runs indefinitely and a 300 second sleep. So, every 5 minutes the script will update the feature service (Dave’s routine operates every minute). If a problem occurs, the script should recover and continue working.
Once you have setup the script, you could run it from any computer that has Python 2.6+ installed (no ArcGIS desktop software required!).
The ISS position is just one example of a website that serves geo-enabled information. USGS earthquake reporting services provides useful information with geographic coordinates. Many other websites provide an API which may not include geographic coordinates, but you could use Python to combine this data with your own local data before pushing updates to your feature service. For example, data.gov provides an API and Python code examples for consuming it.
Commenting is not enabled for this article.