Skip to content

Instantly share code, notes, and snippets.

@davepape
Last active November 2, 2015 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepape/7be294c2e21cecf61f42 to your computer and use it in GitHub Desktop.
Save davepape/7be294c2e21cecf61f42 to your computer and use it in GitHub Desktop.
Visualize earthquake data from USGS
#pragma strict
// Script to visualize earthquake data in GeoJSON format from USGS
// (http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php)
// 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
// with a 20 unit diameter. A prefab to use for marking the quakes should
// be assigned to the variable "prefab" in the Unity editor.
import SimpleJSON;
var prefab : GameObject;
function Start ()
{
var datastring = System.IO.File.ReadAllText("quakes.json");
var data = JSON.Parse(datastring);
var i : int;
var lat : float;
var lon : float;
var mag : float;
var radius = 10.0;
for (i=0; i < data["metadata"]["count"].AsInt; i++)
{
lon = data["features"][i]["geometry"]["coordinates"][0].AsFloat;
lat = data["features"][i]["geometry"]["coordinates"][1].AsFloat;
mag = data["features"][i]["properties"]["mag"].AsFloat;
Debug.Log("quake at " + lat + " " + lon + ", magnitude " + mag);
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);
var size = mag/10.0;
marker.transform.localScale = Vector3(size,size,size);
marker.transform.parent = transform;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment