Skip to content

Instantly share code, notes, and snippets.

@davepape
Created November 12, 2019 18:37
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/4f4e3c04a55fc7959a023ce73acd7420 to your computer and use it in GitHub Desktop.
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.
/* 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