246 lines
6.1 KiB
C#
246 lines
6.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
/// <summary>
|
||
/// 县级视角得企业信息
|
||
/// </summary>
|
||
[Serializable]
|
||
public class AreaCompanyInformationData
|
||
{
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public int code;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string success;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public List<AreaCompany> data;
|
||
/// <summary>
|
||
/// 操作成功
|
||
/// </summary>
|
||
public string msg;
|
||
}
|
||
[Serializable]
|
||
public class AreaCompany
|
||
{
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string consId;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string consNo;
|
||
/// <summary>
|
||
/// 莱芜供电公司大楼
|
||
/// </summary>
|
||
public string consName;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public double longitude;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public double latitude;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string resourceType;
|
||
}
|
||
|
||
[Serializable]
|
||
public struct Coordinate
|
||
{
|
||
//经度
|
||
public double lon;
|
||
|
||
//纬度
|
||
public double lat;
|
||
|
||
//高度
|
||
public double alt;
|
||
}
|
||
[Serializable]
|
||
public struct Vector2D
|
||
{
|
||
public double x;
|
||
public double y;
|
||
|
||
public Vector2D(double x, double y)
|
||
{
|
||
this.x = x;
|
||
this.y = y;
|
||
}
|
||
|
||
//距离
|
||
public double DistanceTo(Vector2D v)
|
||
{
|
||
var dx = x - v.x;
|
||
var dy = y - v.y;
|
||
return Math.Sqrt(dx * dx + dy * dy);
|
||
}
|
||
|
||
//加法
|
||
public Vector2D Plus(Vector2D v)
|
||
{
|
||
return new Vector2D(x + v.x, y + v.y);
|
||
}
|
||
|
||
//减法
|
||
public Vector2D Minus(Vector2D v)
|
||
{
|
||
return new Vector2D(x - v.x, y - v.y);
|
||
}
|
||
|
||
//乘法,除相当于乘1/v
|
||
public Vector2D Multiply(double v)
|
||
{
|
||
return new Vector2D(x * v, y * v);
|
||
}
|
||
|
||
//叉乘
|
||
public double Cross(Vector2D v)
|
||
{
|
||
return x * v.y - y * v.x;
|
||
}
|
||
|
||
//点在直线ab的右侧
|
||
public bool IsOnRight(Vector2D a, Vector2D b)
|
||
{
|
||
return Cross(b.Minus(a)) >= 0;
|
||
}
|
||
|
||
public Vector3 ToVector3(float height)
|
||
{
|
||
return new Vector3((float)x, height, (float)y);
|
||
}
|
||
|
||
public Coordinate ToCoord(double alt)
|
||
{
|
||
return new Coordinate { lon = x, lat = y, alt = alt };
|
||
}
|
||
}
|
||
|
||
public static class WebApiExtension
|
||
{
|
||
public static Vector3 AsVector3(this Coordinate target)
|
||
{
|
||
//return new Vector3(Lon2X(target.lon), (float)target.alt - 1982, Lat2Z(target.lat));
|
||
return L2V(new Vector2D(target.lon, target.lat)).ToVector3((float)target.alt - 1982);
|
||
}
|
||
|
||
public static Coordinate AsCoord(this Vector3 target)
|
||
{
|
||
//return new Coordinate {lon = X2Lon(target.x), alt = target.y + 1982f, lat = Z2Lat(target.z)};
|
||
return V2L(new Vector2D(target.x, target.z)).ToCoord(target.y + 1982);
|
||
}
|
||
|
||
public static Vector3[] AsVector3S(this Coordinate[] target)
|
||
{
|
||
return target.Select(c => c.AsVector3()).ToArray();
|
||
}
|
||
|
||
public static Coordinate[] AsCoords(this IEnumerable<Vector3> target)
|
||
{
|
||
return target.Select(v => v.AsCoord()).ToArray();
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="x"></param>
|
||
/// <param name="t1"></param>
|
||
/// <param name="t2"></param>
|
||
/// <param name="s1"></param>
|
||
/// <param name="s2"></param>
|
||
/// <returns></returns>
|
||
public static double Remap(double x, double t1, double t2, double s1, double s2)
|
||
{
|
||
//return (s2 - s1) / (t2 - t1) * (x - t1) + s1;
|
||
return (x - t1) / (t2 - t1) * (s2 - s1) + s1;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 经度转X 东西经 南北纬
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
// public static float Lon2X(double x)
|
||
// {
|
||
// //return (float)Remap(x, 103.75417182054126, 103.757260683307, -164.08, 141.841);
|
||
// return (float)Remap(x, 103.7573234, 103.7542207, 141.5, -163.9);
|
||
// }
|
||
// public static double X2Lon(double x)
|
||
// {
|
||
// //return Remap(x, -164.08, 141.841, 103.75417182054126, 103.757260683307);
|
||
// return Remap(x, 141.5, -163.9, 103.7573234, 103.7542207);
|
||
// }
|
||
private static Vector2D _LA = new Vector2D(122.70258955842212, 37.40090039325806);
|
||
private static Vector2D _LB = new Vector2D(118.2260388213308, 34.39627721967489);
|
||
private static Vector2D _LC = new Vector2D(115.29544950948309, 36.50134572113451);
|
||
|
||
private static Vector2D _VA = new Vector2D(-59.36, -30.82);
|
||
private static Vector2D _VB = new Vector2D(4.03, 37.31);
|
||
private static Vector2D _VC = new Vector2D(56.41, -3.06);
|
||
|
||
|
||
public static void SetVector2D(Vector2D _LAT, Vector2D _LBT,Vector2D _LCT, Vector2D _VAT, Vector2D _VBT, Vector2D _VCT)
|
||
{
|
||
_LA = _LAT;
|
||
_LB = _LBT;
|
||
_LC = _LCT;
|
||
_VA = _VAT;
|
||
_VB = _VBT;
|
||
_VC = _VCT;
|
||
}
|
||
|
||
|
||
//由经纬度转米。根据三点转化
|
||
private static Vector2D L2V(Vector2D p)
|
||
{
|
||
var abc = Square(_LA, _LB, _LC);
|
||
var bcp = Square(_LB, _LC, p);
|
||
var acp = Square(_LC, _LA, p);
|
||
var abp = Square(_LA, _LB, p);
|
||
var k1 = bcp / abc;
|
||
var k2 = acp / abc;
|
||
var k3 = abp / abc;
|
||
// Debug.Log($"square : {abc} ==> {bcp} == {acp} == {abp}");
|
||
// Debug.Log($"{k1} == {k2} == {k3} ==> {k1+k2+k3}");
|
||
return _VA.Multiply(k1).Plus(_VB.Multiply(k2)).Plus(_VC.Multiply(k3));
|
||
}
|
||
|
||
//由米转经纬度。根据三点转化
|
||
private static Vector2D V2L(Vector2D p)
|
||
{
|
||
var abc = Square(_VA, _VB, _VC);
|
||
var bcp = Square(_VB, _VC, p);
|
||
var acp = Square(_VC, _VA, p);
|
||
var abp = Square(_VA, _VB, p);
|
||
var k1 = bcp / abc;
|
||
var k2 = acp / abc;
|
||
var k3 = abp / abc;
|
||
// Debug.Log($"square : {abc} ==> {bcp} == {acp} == {abp}");
|
||
// Debug.Log($"{k1} == {k2} == {k3} ==> {k1+k2+k3}");
|
||
return _LA.Multiply(k1).Plus(_LB.Multiply(k2)).Plus(_LC.Multiply(k3));
|
||
}
|
||
|
||
private static double Square(Vector2D a, Vector2D b, Vector2D c)
|
||
{
|
||
//海伦-秦九韶公式
|
||
var ab = a.DistanceTo(b);
|
||
var ac = a.DistanceTo(c);
|
||
var bc = b.DistanceTo(c);
|
||
var p = (ab + bc + ac) / 2;
|
||
var s = Math.Sqrt(p * (p - ab) * (p - bc) * (p - ac));
|
||
if (!c.Minus(a).IsOnRight(a, b)) s *= -1;
|
||
return s;
|
||
}
|
||
}
|
||
|