Created
November 12, 2019 18:37
-
-
Save davepape/4f4e3c04a55fc7959a023ce73acd7420 to your computer and use it in GitHub Desktop.
Starting point for visualization project - read earthquake data from the USGS geojson data feed.
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
/* Example of reading earthquake data from the USGS data feed. | |
Starting point for visualization project. | |
This script requests GEOJson formatted data, then parses it and prints some of the information. | |
It requires the JSONObject class for Unity, which can be downloaded from https://github.com/mtschoen/JSONObject | |
*/ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
public class readQuakes : MonoBehaviour | |
{ | |
public string url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson"; | |
void Start() | |
{ | |
StartCoroutine(GetData(url)); | |
} | |
IEnumerator GetData(string url) | |
{ | |
Debug.Log($"sending request {url}"); | |
UnityWebRequest req = UnityWebRequest.Get(url); | |
yield return req.SendWebRequest(); | |
if (req.isNetworkError) | |
Debug.Log($"Error ({url}): {req.error}"); | |
else | |
{ | |
JSONObject data = new JSONObject(req.downloadHandler.text, -2, false, false); | |
for (int i=0; i < data["metadata"]["count"].n; i++) | |
{ | |
float lat = data["features"][i]["geometry"]["coordinates"][1].n; | |
float lon = data["features"][i]["geometry"]["coordinates"][0].n; | |
float mag = data["features"][i]["properties"]["mag"].n; | |
Debug.Log($"lat: {lat} lon: {lon} mag: {mag}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment