GDAL在Unity3D中的使用以及坐标转换
这是一篇记录帖,应届毕业生一枚,第一次写博客有点小紧张,有大神路过的话也希望帮忙看看对不对~
引言
本文的目的是用Unity读取tif图像数据的经纬度转换成Unity的坐标并放到对应的位置上
开发平台
vs2019+Unity2021.3.6
GDAL下载
首先解决Unity读取tif数据的问题,这里我用了GDAL来读取tif数据,相信有小伙伴不知道怎么配置GDAL,这里我就把我踩坑配置的方法说下。
GDAL使用
我们可以在start函数里面去测试一下导入是否成功,path就是你存放tif的路径,我们这里的w和h就是图片的宽和高。
如果打印成功了也就证明你导入成功了,可以开始下面步骤了。
新建一个double类型数组,用来储存读取tif时候的数据。
然后利用GDAL的API来进行读取。
该Api会存放给你6个值,这6个值代表了不同的意义,如图下。
0 和3代表经度和纬度,也就是你读取影像的左上角的经纬度,1代表遥感图像的水平空间分辨率,5代表遥感图像的垂直空间分辨率,一般相等。如果遥感图是正的话没有发生偏转,2和4就是0。
我们可以按照图上的点算出对应点的对应地理坐标
如果行数和列数分别为row,column;
xGeo = geoTransform[0] + column * geoTransform[1] + row * geoTransform[2]
yGeo = geoTransform[3] + column * geoTransform[4] + row * geoTransform[5]
我们就可以求出点的四个边角的经纬度了,从而能求出中点的经纬度
把经纬度带到经纬度转3d坐标的方法中即可。
分割线————————————————————————————————
经纬度转世界坐标:
网上有这方面的代码,大家可以自行CV,如果你懒的话当我没说。我这里是翻译的python经纬度转3d坐标的代码。
public class Cartesian3
{
public double x = 0;
public double y = 0;
public double z = 0;
Transform3D tan = new Transform3D();
public Cartesian3(double x = 0, double y = 0, double z = 0)
{
this.x = x;
this.y = y;
this.z = z;
}
public void print()
{
Console.Write($"{x}");
}
}
#endregion
public class Transform3D
{
public Transform3D()
{
}
/// <summary>
/// 度转角度
/// </summary>
/// <param name="degrees">十进制度/param>
/// <returns>弧度</returns>
public double ToRadians(double degrees)
{
return degrees * (Math.PI / 180.0);
}
/// <summary>
/// 向量求模
/// </summary>
/// <param name="cartesian"></param>
/// <returns></returns>
public double MagnitudeSquared(Cartesian3 cartesian)
{
return cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z;
}
/// <summary>
/// 模的平方
/// </summary>
/// <param name="cartesian"></param>
/// <returns></returns>
public double Magnitude(Cartesian3 cartesian)
{
return Math.Sqrt(MagnitudeSquared(cartesian));
}
public Cartesian3 Normalize(Cartesian3 cartesian, Cartesian3 result)
{
double ma = Magnitude(cartesian);
result.x = cartesian.x / ma;
result.y = cartesian.y / ma;
result.z = cartesian.z / ma;
return result;
}
public Cartesian3 MultiplyComponents(Cartesian3 left, Cartesian3 right, Cartesian3 result)
{
result.x = left.x * right.x;
result.y = left.y * right.y;
result.z = left.z * right.z;
return result;
}
public double dot(Cartesian3 left, Cartesian3 right)
{
double result = left.x * right.x + left.y * right.y + left.z * right.z;
return result;
}
public Cartesian3 DivideByScalar(Cartesian3 cartesian, double scalar, Cartesian3 result)
{
result.x = cartesian.x / scalar;
result.y = cartesian.y / scalar;
result.z = cartesian.z / scalar;
return result;
}
public Cartesian3 MultiplyByScalar(Cartesian3 cartesian, double scalar, Cartesian3 result)
{
result.x = cartesian.x * scalar;
result.y = cartesian.y * scalar;
result.z = cartesian.z * scalar;
return result;
}
public Cartesian3 Add(Cartesian3 left, Cartesian3 right, Cartesian3 result)
{
result.x = left.x + right.x;
result.y = left.y + right.y;
result.z = left.z + right.z;
return result;
}
/// <summary>
/// 角度转世界坐标
/// </summary>
/// <param name="longitude"></param>
/// <param name="latitude"></param>
/// <param name="height"></param>
/// <param name="result"></param>
/// <returns></returns>
public Cartesian3 FromRadians(double longitude, double latitude, double height)
{
Cartesian3 result = new Cartesian3();
Cartesian3 scratchN = new Cartesian3();
Cartesian3 scratchK = new Cartesian3();
Cartesian3 radiiSquared = new Cartesian3(
6378.1370 * 6378.1370,//缩放1000倍
6378.1370 * 6378.1370,
6378.1370 * 6378.1370
//6356752.3142451793 * 6356752.3142451793
);
double cosLatitude = Math.Cos(latitude);
scratchN.x = cosLatitude * Math.Cos(longitude);
scratchN.y = cosLatitude * Math.Sin(longitude);
scratchN.z = Math.Sin(latitude);
scratchN = Normalize(scratchN, scratchN); // 转换为单位向量
scratchK = MultiplyComponents(radiiSquared, scratchN, scratchK);
double gamma = Math.Sqrt(dot(scratchN, scratchK));
scratchK = DivideByScalar(scratchK, gamma, scratchK);
scratchN = MultiplyByScalar(scratchN, height, scratchN);
return Add(scratchK, scratchN, result);
}
public Cartesian3 FromDegrees(double longitude, double latitude, double height = 0)
{
longitude = ToRadians(longitude);
latitude = ToRadians(latitude);
return FromRadians(longitude, latitude, height);
}
}