这是一篇记录帖,应届毕业生一枚,第一次写博客有点小紧张,有大神路过的话也希望帮忙看看对不对~
本文的目的是用Unity读取tif图像数据的经纬度转换成Unity的坐标并放到对应的位置上
vs2019+Unity2021.3.6
首先解决Unity读取tif数据的问题,这里我用了GDAL来读取tif数据,相信有小伙伴不知道怎么配置GDAL,这里我就把我踩坑配置的方法说下。
首先去官网http://www.gisinternals.com/release.php下载Unity的GDAL ,(这里我参考了(2条消息) GDAL在Unity3D中的配置与测试(超详细!)_减肥的吉吉的博客-CSDN博客这位大佬的配置教学)没法科学上网没关系,在csdn上面也有很多GDAL可以下载,自行搜索下载就好。我这里用的是2.4.4版本,(GDAL—release-1911-x64-gdal-2-4-4-mapserver-7-4-3) ,这里不在过多描述导入的过程了。
我们可以在start函数里面去测试一下导入是否成功,path就是你存放tif的路径,我们这里的w和h就是图片的宽和高。
新建一个double类型数组,用来储存读取tif时候的数据。
然后利用GDAL的API来进行读取。
该Api会存放给你6个值,这6个值代表了不同的意义,如图下。
0 和3代表经度和纬度,也就是你读取影像的左上角的经纬度,1代表遥感图像的水平空间分辨率,5代表遥感图像的垂直空间分辨率,一般相等。如果遥感图是正的话没有发生偏转,2和4就是0。
我们可以按照图上的点算出对应点的对应地理坐标,(参考(2条消息) Gdal中GetGeoTransfrom的含义_alexsci的博客-CSDN博客_getgeotransform这位大佬)
如果行数和列数分别为row,column;
xGeo = geoTransform[0] + column * geoTransform[1] + row * geoTransform[2]
yGeo = geoTransform[3] + column * geoTransform[4] + row * geoTransform[5]
我们就可以求出点的四个边角的经纬度了,从而能求出中点的经纬度
网上有这方面的代码,大家可以自行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()
{
}
///
/// 度转角度
///
/// 十进制度/param>
/// 弧度
public double ToRadians(double degrees)
{
return degrees * (Math.PI / 180.0);
}
///
/// 向量求模
///
///
///
public double MagnitudeSquared(Cartesian3 cartesian)
{
return cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z;
}
///
/// 模的平方
///
///
///
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;
}
///
/// 角度转世界坐标
///
///
///
///
///
///
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);
}
}