The Temperature Map

Here’s an example of using the content of graphic metadata tags in a document to retrieve and apply the current temperature data to a map showing various cities in the United States.

The script in the provided Omni Automation action uses the fetch( ) method of the URL class to retrieve textual weather data from the National Oceanic and Atmospheric Administration in XML format that can be parsed for the temperature data for the cities whose weather station codes have been assigned as metadata tags to the text graphics containing the names of the cities.

Metadata Tags

You can DOWNLOAD the OmniGraffle template document.

/*{ "type": "action", "targets": ["omnigraffle"], "author": "Otto Automator", "identifier": "com.omni-automation.weather-map", "version": "1.5", "description": "This action is designed to fetch and insert the current temperatures for the cities in the U.S. Temperature Map OmniGraffle document.", "label": "Insert Current Temperatures", "shortLabel": "Get Temps" }*/ var _ = function(){ var action = new PlugIn.Action(function(selection, sender){ // ACTION CODE try { var cnvs = document.windows[0].selection.canvas var layerNames = cnvs.layers.map(function(layer){ return layer.name }) var targetName = "Cities" if (layerNames.includes(targetName)){ var targetLayer = cnvs.layers.filter(function(layer){ if (layer.name === targetName){return layer} }) } else { throw new Error("Layer not found.") } targetLayer = targetLayer[0] var baseURLStr = 'https://w1.weather.gov/xml/current_obs/' targetLayer.graphics.forEach(function(graphic){ var stationCode = graphic.userData["station-code"] var cityName = graphic.name if (typeof stationCode !== 'undefined'){ aURL = URL.fromString(baseURLStr + stationCode + ".xml") aURL.fetch((function(graphic,data){ dataStr = data.toString() startTag = '' startTagOffset = dataStr.indexOf(startTag) if (startTagOffset === -1){ graphic.text = cityName + "\n" + "Data N/A" } else { endTag = '' endTagOffset = dataStr.indexOf(endTag) tempString = dataStr.substring( startTagOffset + startTag.length, endTagOffset ) graphic.text = cityName + "\n" + tempString } }).bind(this,graphic)) } }) } catch (err){console.error(err)} }); action.validate = function(selection, sender){ // VALIDATION CODE // selection options: canvas, document, graphics, lines, solids, view return true }; return action; }(); _;

 14-17  Get the names of the layers in the current canvas

 18-26  Check to see that the layer “Cities” is in the current canvas. If it is, store a reference to it, otherwise throw an error.

 28  The base URL for the NOAA query. current_objs stands for “current observation”

 30-47  Process each of the text graphics in the target layer.

 31  Get the value for the metadata tag “station-code”. The result is a 4-character string of uppercase letters.

 32  Get the value of the name property of the text graphic. Note that the value of the name property may not be the same as the contents of the text graphic.

 33  Create the web service query URL using the fromString() of the URL class, passing in the base URL string appended with the retrieved station code and the “.xml” file extension.

 35-45  Use the fetch() method of the URL class to query the web service and retrieve and process the resulting XML data.

 37-43  Extract the temperature string from the XML data by locating the pair of tags using the indexOf() method.

 44  Set the contents of the text graphic to the city name and extracted temperature string.

 X  Use the bind() method to create a function for each graphic instance.

UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER