2021-04-27 20:28:03 +08:00

169 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GeoJSON.Net.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TurfCS;
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();
}
}
private void CalcDistance()
{
//_List[0].Distance = 0;
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] });
var value = Turf.Distance(pt1, pt2, "kilometers") ;
_List[i].Distance = Math.Round(value*1000, 2);
}
}
/// <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; }
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; }
}
public override string ToString()
{
//return base.ToString();
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
}
}