Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.