Created
March 1, 2017 17:53
-
-
Save davepape/d13c726fdd8326b35d6fe18dd377d3e9 to your computer and use it in GitHub Desktop.
plot earthquake data from USGS - uses WWW module and JSONObject plugin
This file contains hidden or 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 earthquake data in GeoJSON format from USGS | |
// (http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php) | |
// Requires the JSONObject plugin from http://wiki.unity3d.com/index.php/JSONObject | |
// 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 JSONObject; | |
public var prefab : GameObject; | |
public var url = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_month.geojson"; | |
function Start () | |
{ | |
var dataurl : WWW = new WWW(url); | |
yield dataurl; | |
var data = new JSONObject(dataurl.text,-2,false,false); | |
var i : int; | |
var lat : float; | |
var lon : float; | |
var mag : float; | |
var radius = 10.0; | |
for (i=0; i < data["metadata"]["count"].n; i++) | |
{ | |
lon = data["features"][i]["geometry"]["coordinates"][0].n; | |
lat = data["features"][i]["geometry"]["coordinates"][1].n; | |
mag = data["features"][i]["properties"]["mag"].n; | |
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*mag/100.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