Skip to content

Instantly share code, notes, and snippets.

@watab0shi
Last active June 7, 2022 08:28
Show Gist options
  • Select an option

  • Save watab0shi/71181a380b42caef818052579d821371 to your computer and use it in GitHub Desktop.

Select an option

Save watab0shi/71181a380b42caef818052579d821371 to your computer and use it in GitHub Desktop.
Unity ARCore Geospatial API Altitude Calculator

AltitudeCalculator

Unity で Geospatial API アプリ制作時に、現在の緯度軽度から高度を求めるスクリプト。
標高APIジオイド高API を叩いて、緯度軽度に対応する高度を求めます。

double altitude = elevation + goidHeight - 1.5;

↑謎の1.5mを引くとピッタリになりました(AltitudeCalculator.cs側でオフセットしてあります)

Dependencies

UniTask

Menu > PaclageManager > + > Add package from git URL...

https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask

Usage

ボタンクリックなど任意のタイミングで

var altitude = await AltitudeCalculator.fetchAltitude(latitude, longitude);

を呼ぶと高度が取得できます。

UniTask を使用しているため実行する関数を async にしてください。

private async void OnSetAnchorClicked()// <- add async
{
  var pose = EarthManager.CameraGeospatialPose;
  var altitude = await AltitudeCalculator.fetchAltitude(pose.Latitude, pose.Longitude);// <- get altitude at current lat,lng
  GeospatialAnchorHistory history = new GeospatialAnchorHistory(pose.Latitude, pose.Longitude, altitude, pose.Heading);// <- set altitude
  
  if (PlaceGeospatialAnchor(history))
  {
    _historyCollection.Collection.Add(history);
    SnackBarText.text = $"{_anchorObjects.Count} Anchor(s) Set!";
  }
  else
  {
    SnackBarText.text = "Failed to set an anchor!";
  }
}

Reference

API Reference - ARCore Extensions for AR Foundation  |  Google Developers

ARCore Geospatial API を使用して、グローバル スケールの没入型の位置情報ベース AR エクスペリエンスを構築します。 | Google Developers

google-ar/arcore-unity-extensions

ジオイド | 国土地理院

たぬ福さんのツイート

【超初心者向け】UniTaskの始め方3つのポイント - 渋谷ほととぎす通信

Lisence

MIT

/*
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment