|
/* |
|
The MIT License (MIT) |
|
|
|
Copyright 2022 watab0shi |
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
*/ |
|
|
|
using System; |
|
using Cysharp.Threading.Tasks; |
|
using UnityEngine; |
|
using UnityEngine.Networking; |
|
|
|
public class AltitudeCalculator |
|
{ |
|
private const string ELEVATION_API_ENDPOINT = "https://cyberjapandata2.gsi.go.jp/general/dem/scripts/getelevation.php"; |
|
private const string GEOIDHEIGHT_API_ENDPOINT = "https://vldb.gsi.go.jp/sokuchi/surveycalc/geoid/calcgh/cgi/geoidcalc.pl"; |
|
|
|
[Serializable] |
|
public struct ElevationApiResponce |
|
{ |
|
public double elevation; |
|
public string hsrc; |
|
} |
|
|
|
[Serializable] |
|
public struct GeoidHeightApiResponce |
|
{ |
|
[Serializable] |
|
public struct OutputDataProperty |
|
{ |
|
public string latitude; |
|
public string longitude; |
|
public string geoidHeight; |
|
} |
|
|
|
public OutputDataProperty OutputData; |
|
} |
|
|
|
private static double _altitudeOffset = -1.5d; |
|
|
|
/** |
|
* 標高APIのURIを作成 |
|
*/ |
|
private static string createElevationApiUri(double latitude, double longitude) |
|
{ |
|
return $"{ELEVATION_API_ENDPOINT}?lat={latitude.ToString()}&lon={longitude.ToString()}&outtype=JSON"; |
|
} |
|
|
|
/** |
|
* ジオイド高APIのURIを作成 |
|
*/ |
|
private static string createGeoidHeightApiUri(double latitude, double longitude) |
|
{ |
|
return $"{GEOIDHEIGHT_API_ENDPOINT}?latitude={latitude.ToString()}&longitude={longitude.ToString()}&outputType=json"; |
|
} |
|
|
|
/** |
|
* uriにGETリクエストを送信してレスポンス文字列を返す |
|
*/ |
|
private static async UniTask<string> fetch(string uri) |
|
{ |
|
var text = (await UnityWebRequest.Get(uri).SendWebRequest()).downloadHandler.text; |
|
return text; |
|
} |
|
|
|
/** |
|
* 標高APIから標高を取得 |
|
*/ |
|
private static async UniTask<double> fetchElevation(double latitude, double longitude) |
|
{ |
|
string elevationApiUri = createElevationApiUri(latitude, longitude); |
|
string elevationApiResponse = await fetch(elevationApiUri); |
|
var data = JsonUtility.FromJson<ElevationApiResponce>(elevationApiResponse); |
|
double elevation = data.elevation; |
|
return elevation; |
|
} |
|
|
|
/** |
|
* ジオイド高APIからジオイド高を取得 |
|
*/ |
|
private static async UniTask<double> fetchGeoidHeight(double latitude, double longitude) |
|
{ |
|
string geoidHeightApiUri = createGeoidHeightApiUri(latitude, longitude); |
|
string geoidHeightApiResponse = await fetch(geoidHeightApiUri); |
|
var data = JsonUtility.FromJson<GeoidHeightApiResponce>(geoidHeightApiResponse); |
|
double geoidHeight = Convert.ToDouble(data.OutputData.geoidHeight); |
|
return geoidHeight; |
|
} |
|
|
|
/** |
|
* 高度を取得 |
|
*/ |
|
public static async UniTask<double> fetchAltitude(double latitude, double longitude) |
|
{ |
|
var (elevation, geoidHeight) = await UniTask.WhenAll( |
|
fetchElevation(latitude, longitude), |
|
fetchGeoidHeight(latitude, longitude) |
|
); |
|
double altitude = elevation + geoidHeight + _altitudeOffset; |
|
|
|
Debug.Log("fetchAltitude elevation : " + elevation.ToString()); |
|
Debug.Log("fetchAltitude geoidHeight : " + geoidHeight.ToString()); |
|
Debug.Log("fetchAltitude altitude : " + altitude.ToString()); |
|
|
|
return altitude; |
|
} |
|
} |