• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

tif数据84坐标经纬度转Unity3D坐标

武飞扬头像
Gusliy
帮助1

 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坐标的代码。

  1.  
    public class Cartesian3
  2.  
    {
  3.  
     
  4.  
    public double x = 0;
  5.  
    public double y = 0;
  6.  
    public double z = 0;
  7.  
     
  8.  
     
  9.  
    Transform3D tan = new Transform3D();
  10.  
     
  11.  
     
  12.  
    public Cartesian3(double x = 0, double y = 0, double z = 0)
  13.  
    {
  14.  
    this.x = x;
  15.  
    this.y = y;
  16.  
    this.z = z;
  17.  
    }
  18.  
     
  19.  
    public void print()
  20.  
    {
  21.  
    Console.Write($"{x}");
  22.  
    }
  23.  
    }
  24.  
    #endregion
  25.  
     
  26.  
     
  27.  
    public class Transform3D
  28.  
    {
  29.  
    public Transform3D()
  30.  
    {
  31.  
     
  32.  
    }
  33.  
     
  34.  
    /// <summary>
  35.  
    /// 度转角度
  36.  
    /// </summary>
  37.  
    /// <param name="degrees">十进制度/param>
  38.  
    /// <returns>弧度</returns>
  39.  
    public double ToRadians(double degrees)
  40.  
    {
  41.  
    return degrees * (Math.PI / 180.0);
  42.  
    }
  43.  
     
  44.  
    /// <summary>
  45.  
    /// 向量求模
  46.  
    /// </summary>
  47.  
    /// <param name="cartesian"></param>
  48.  
    /// <returns></returns>
  49.  
    public double MagnitudeSquared(Cartesian3 cartesian)
  50.  
    {
  51.  
    return cartesian.x * cartesian.x cartesian.y * cartesian.y cartesian.z * cartesian.z;
  52.  
    }
  53.  
     
  54.  
    /// <summary>
  55.  
    /// 模的平方
  56.  
    /// </summary>
  57.  
    /// <param name="cartesian"></param>
  58.  
    /// <returns></returns>
  59.  
    public double Magnitude(Cartesian3 cartesian)
  60.  
    {
  61.  
    return Math.Sqrt(MagnitudeSquared(cartesian));
  62.  
    }
  63.  
     
  64.  
     
  65.  
    public Cartesian3 Normalize(Cartesian3 cartesian, Cartesian3 result)
  66.  
    {
  67.  
    double ma = Magnitude(cartesian);
  68.  
    result.x = cartesian.x / ma;
  69.  
    result.y = cartesian.y / ma;
  70.  
    result.z = cartesian.z / ma;
  71.  
    return result;
  72.  
    }
  73.  
     
  74.  
    public Cartesian3 MultiplyComponents(Cartesian3 left, Cartesian3 right, Cartesian3 result)
  75.  
    {
  76.  
    result.x = left.x * right.x;
  77.  
    result.y = left.y * right.y;
  78.  
    result.z = left.z * right.z;
  79.  
    return result;
  80.  
    }
  81.  
     
  82.  
    public double dot(Cartesian3 left, Cartesian3 right)
  83.  
    {
  84.  
    double result = left.x * right.x left.y * right.y left.z * right.z;
  85.  
    return result;
  86.  
    }
  87.  
     
  88.  
    public Cartesian3 DivideByScalar(Cartesian3 cartesian, double scalar, Cartesian3 result)
  89.  
    {
  90.  
    result.x = cartesian.x / scalar;
  91.  
    result.y = cartesian.y / scalar;
  92.  
    result.z = cartesian.z / scalar;
  93.  
    return result;
  94.  
    }
  95.  
     
  96.  
    public Cartesian3 MultiplyByScalar(Cartesian3 cartesian, double scalar, Cartesian3 result)
  97.  
    {
  98.  
    result.x = cartesian.x * scalar;
  99.  
    result.y = cartesian.y * scalar;
  100.  
    result.z = cartesian.z * scalar;
  101.  
    return result;
  102.  
    }
  103.  
     
  104.  
    public Cartesian3 Add(Cartesian3 left, Cartesian3 right, Cartesian3 result)
  105.  
    {
  106.  
    result.x = left.x right.x;
  107.  
    result.y = left.y right.y;
  108.  
    result.z = left.z right.z;
  109.  
    return result;
  110.  
    }
  111.  
     
  112.  
     
  113.  
    /// <summary>
  114.  
    /// 角度转世界坐标
  115.  
    /// </summary>
  116.  
    /// <param name="longitude"></param>
  117.  
    /// <param name="latitude"></param>
  118.  
    /// <param name="height"></param>
  119.  
    /// <param name="result"></param>
  120.  
    /// <returns></returns>
  121.  
    public Cartesian3 FromRadians(double longitude, double latitude, double height)
  122.  
    {
  123.  
    Cartesian3 result = new Cartesian3();
  124.  
    Cartesian3 scratchN = new Cartesian3();
  125.  
    Cartesian3 scratchK = new Cartesian3();
  126.  
    Cartesian3 radiiSquared = new Cartesian3(
  127.  
    6378.1370 * 6378.1370,//缩放1000
  128.  
    6378.1370 * 6378.1370,
  129.  
    6378.1370 * 6378.1370
  130.  
    //6356752.3142451793 * 6356752.3142451793
  131.  
    );
  132.  
    double cosLatitude = Math.Cos(latitude);
  133.  
    scratchN.x = cosLatitude * Math.Cos(longitude);
  134.  
    scratchN.y = cosLatitude * Math.Sin(longitude);
  135.  
    scratchN.z = Math.Sin(latitude);
  136.  
    scratchN = Normalize(scratchN, scratchN); // 转换为单位向量
  137.  
     
  138.  
    scratchK = MultiplyComponents(radiiSquared, scratchN, scratchK);
  139.  
    double gamma = Math.Sqrt(dot(scratchN, scratchK));
  140.  
    scratchK = DivideByScalar(scratchK, gamma, scratchK);
  141.  
    scratchN = MultiplyByScalar(scratchN, height, scratchN);
  142.  
     
  143.  
    return Add(scratchK, scratchN, result);
  144.  
    }
  145.  
     
  146.  
    public Cartesian3 FromDegrees(double longitude, double latitude, double height = 0)
  147.  
    {
  148.  
    longitude = ToRadians(longitude);
  149.  
    latitude = ToRadians(latitude);
  150.  
     
  151.  
    return FromRadians(longitude, latitude, height);
  152.  
    }
  153.  
     
  154.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbfbga
系列文章
更多 icon
同类精品
更多 icon
继续加载