181 lines
5.6 KiB
C#
Raw Normal View History

using GeoJSON.Net.Geometry;
using System;
2021-03-22 16:05:40 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TurfCS;
2021-03-22 16:05:40 +08:00
namespace Assets.Scripts.Apis.Models
{
public class MapDataModel
{
public string Type { get; set; }
public double TotalDistance { get; set; }
//private double? _TotalClimb;
///// <summary>
///// 累计爬升
///// </summary>
//public double TotalClimb
//{
// get
// {
// if(_TotalClimb == null)
// {
// var total = 0D;
// for (int i = 0; i < List.Count; i++)
// {
// if(i < List.Count - 1)
// {
// var item = List[i];
// var next = List[i + 1];
// var value = next.Elevation - item.Elevation;
// if(value > 0)
// {
// total += value;
// }
// }
// }
// _TotalClimb = total;
// }
// return _TotalClimb.GetValueOrDefault(0);
// }
//}
//public double AverageGrade
//{
// get
// {
// }
//}
private List<Item> _List;
public List<Item> List
{
get
{
return _List;
}
set
{
_List = value;
if (_List == null) return;
this.CalcDistance();
this.CalcGrade();
this.CalcBbox();
this.CalcCenter();
2022-03-04 16:12:15 +08:00
this.CalcBearing();
}
}
private void CalcBearing()
{
for (int i = 0; i < _List.Count - 1; i++)
{
var pt1 = Turf.Point(new double[] { _List[i].Point[1], _List[i].Point[0] });
var pt2 = Turf.Point(new double[] { _List[i + 1].Point[1], _List[i + 1].Point[0] });
_List[i].Bearing = Turf.Bearing(pt1, pt2);
2021-03-22 16:05:40 +08:00
}
}
private void CalcDistance()
{
2021-04-27 15:15:51 +08:00
//_List[0].Distance = 0;
2021-04-27 20:28:03 +08:00
for (int i = 0; i < _List.Count - 1; i++)
{
2021-04-27 15:15:51 +08:00
var pt1 = Turf.Point(new double[] { _List[i].Point[1], _List[i].Point[0] });
var pt2 = Turf.Point(new double[] { _List[i+1].Point[1], _List[i+1].Point[0] });
2021-03-28 18:17:15 +08:00
var value = Turf.Distance(pt1, pt2, "kilometers") ;
_List[i].Distance = Math.Round(value*1000, 2);
}
2021-03-22 16:05:40 +08:00
}
/// <summary>
/// 计算坡度数据
/// </summary>
private void CalcGrade()
{
for (int i = 0; i < this._List.Count - 1; i++)
{
var a = _List[i + 1].Elevation - _List[i].Elevation;
if (a == 0)
{
//grade.Add(0);
_List[i].Grade = 0;
continue;
}
//勾股定理
var c = _List[i].Distance;//如果车停了下来c为0
//如果当前点到下一个点的距离是0并且不是第一条记录则坡度用上一个点的坡度
if (c == 0 && i > 0)
{
_List[i].Grade = _List[i - 1].Grade;
continue;
}
var b = Math.Sqrt(c * c - a * a);
if (double.IsNaN(b))
{
_List[i].Grade = 0;
}
else if (b == 0)//如果b等于这就是垂直的墙壁
{
_List[i].Grade = _List[i - 1].Grade;
}
else
{
_List[i].Grade = Math.Round(a / b * 100, 4);
}
}
}
private void CalcCenter()
{
var list = this.List.Select(p => new GeoJSON.Net.Geometry.GeographicPosition(p.Point[0], p.Point[1]));
LineString lineString = new LineString(list);
var ll = Turf.Centroid(lineString);
var p1 = ((Point)ll.Geometry).Coordinates;
Center = new double[] { ((GeographicPosition)p1).Latitude, ((GeographicPosition)p1).Longitude };
}
private void CalcBbox()
{
var list = this.List.Select(p => new GeoJSON.Net.Geometry.GeographicPosition(p.Point[0], p.Point[1]));
LineString lineString = new LineString(list);
Bbox = Turf.Bbox(lineString);
}
public Double[] Center { set; get; }
public List<Double> Bbox { set; get; }
2021-03-22 16:05:40 +08:00
public MapDataModel()
{
//List = new List<Item>();
}
public class Item
{
/// <summary>
/// 坐标(lat,lon)
/// </summary>
public double[] Point { get; set; }
/// <summary>
/// 距离(单位米)
/// </summary>
public double Distance { get; set; }
/// <summary>
/// 海拔
/// </summary>
public double Elevation { get; set; }
public double Grade { get; set; }
2022-03-04 16:12:15 +08:00
public double Bearing { get; set; }
2022-03-10 18:32:53 +08:00
public double Speed { get; set; }
2021-03-22 16:05:40 +08:00
}
public override string ToString()
{
//return base.ToString();
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
}
}