Created
November 16, 2015 15:52
-
-
Save davepape/131bfa3b4f0963c2a84e to your computer and use it in GitHub Desktop.
Plot AQI data from AirNow, similar to the plotquakes.js example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
// Script to visualize AQI data in JSON format from airnowapi.org | |
// Requires the SimpleJSON plugin from http://wiki.unity3d.com/index.php/SimpleJSON | |
// The script should be attached to a model of the Earth such as a sphere | |
// scaled by a factor of 20 and positioned at (0,0,0). A prefab to use for marking | |
// the readings should be assigned to the variable "prefab" in the Unity editor. | |
import SimpleJSON; | |
var prefab : GameObject; | |
function Start () | |
{ | |
var colormap = [ Color.white, Color.green, Color.yellow, Color(1.0,0.5,0.0), Color.red, Color(0.7,0,0.3) ]; | |
var datastring = System.IO.File.ReadAllText("Assets/data/aqi.json"); | |
var data = JSON.Parse(datastring); | |
var i : int; | |
var lat : float; | |
var lon : float; | |
var aqi : float; | |
var cat : int; | |
var radius = 10.0; | |
for (i=0; i < data.AsArray.Count; i++) | |
{ | |
lon = data[i]["Longitude"].AsFloat; | |
lat = data[i]["Latitude"].AsFloat; | |
aqi = data[i]["AQI"].AsFloat; | |
cat = data[i]["Category"].AsInt; | |
lon = lon * Mathf.Deg2Rad; | |
lat = lat * Mathf.Deg2Rad; | |
var pos = new Vector3(Mathf.Cos(lon)*Mathf.Cos(lat)*radius, Mathf.Sin(lat)*radius, Mathf.Sin(lon)*Mathf.Cos(lat)*radius); | |
var marker = Instantiate(prefab, pos, Quaternion.identity); | |
marker.GetComponent.<Renderer>().material.color = colormap[cat]; | |
marker.transform.parent = transform; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment