GIS简单计算Helper类
using System;
using ESRI.ArcGIS.Client.Geometry; namespace GISProject.Extensions
{
/// <summary>
/// Extension methods for geodesic calculations.
/// </summary>
public static class Geodesic
{
private const double EarthRadius = 6378.137; //kilometers. Change to miles to return all values in miles instead /// <summary>
/// Gets the distance between two points in Kilometers.
/// </summary>
/// <param name="start">The start point.</param>
/// <param name="end">The end point.</param>
/// <returns></returns>
public static double GetSphericalDistance(this MapPoint start, MapPoint end)
{
double lon1 = start.X / 180 * Math.PI;
double lon2 = end.X / 180 * Math.PI;
double lat1 = start.Y / 180 * Math.PI;
double lat2 = end.Y / 180 * Math.PI;
return 2 * Math.Asin(Math.Sqrt(Math.Pow((Math.Sin((lat1 - lat2) / 2)), 2) +
Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin((lon1 - lon2) / 2), 2))) * EarthRadius;
}
/// <summary>
/// Returns a polygon with a constant distance from the center point measured on the sphere.
/// </summary>
/// <param name="center">The center.</param>
/// <param name="distKM">Radius in kilometers.</param>
/// <returns></returns>
public static Polygon GetRadiusAsPolygon(this MapPoint center, double distKM)
{
Polyline line = GetRadius(center, distKM);
Polygon poly = new Polygon(); if (line.Paths.Count > 1)
{
PointCollection ring = line.Paths[0];
MapPoint last = ring[ring.Count - 1];
for (int i = 1; i < line.Paths.Count; i++)
{
PointCollection pnts = line.Paths[i];
ring.Add(new MapPoint(180 * Math.Sign(last.X), 90 * Math.Sign(center.Y)));
last = pnts[0];
ring.Add(new MapPoint(180 * Math.Sign(last.X), 90 * Math.Sign(center.Y)));
foreach (MapPoint p in pnts)
ring.Add(p);
last = pnts[pnts.Count - 1];
}
poly.Rings.Add(ring);
//pnts.Add(first);
}
else
{
poly.Rings.Add(line.Paths[0]);
}
if (distKM > EarthRadius * Math.PI / 2 && line.Paths.Count != 2)
{
PointCollection pnts = new PointCollection();
pnts.Add(new MapPoint(-180, -90));
pnts.Add(new MapPoint(180, -90));
pnts.Add(new MapPoint(180, 90));
pnts.Add(new MapPoint(-180, 90));
pnts.Add(new MapPoint(-180, -90));
poly.Rings.Add(pnts); //Exterior
}
return poly;
}
/// <summary>
/// Returns a polyline with a constant distance from the center point measured on the sphere.
/// </summary>
/// <param name="center">The center.</param>
/// <param name="distKM">Radius in kilometers.</param>
// <returns></returns>
public static Polyline GetRadius(this MapPoint center, double distKM)
{
Polyline line = new Polyline();
PointCollection pnts = new PointCollection();
line.Paths.Add(pnts);
for (int i = 0; i < 360; i++)
{
//double angle = i / 180.0 * Math.PI;
MapPoint p = GetPointFromHeading(center, distKM, i);
if (pnts.Count > 0)
{
MapPoint lastPoint = pnts[pnts.Count - 1];
int sign = Math.Sign(p.X);
if (Math.Abs(p.X - lastPoint.X) > 180)
{ //We crossed the date line
double lat = LatitudeAtLongitude(lastPoint, p, sign * -180);
pnts.Add(new MapPoint(sign * -180, lat));
pnts = new PointCollection();
line.Paths.Add(pnts);
pnts.Add(new MapPoint(sign * 180, lat));
}
}
pnts.Add(p);
}
pnts.Add(line.Paths[0][0]);
return line;
} /// <summary>
/// Gets the shortest path line between two points. THe line will be following the great
/// circle described by the two points.
/// </summary>
/// <param name="start">The start point.</param>
/// <param name="end">The end point.</param>
/// <returns></returns>
public static Polyline GetGeodesicLine(this MapPoint start, MapPoint end)
{
Polyline line = new Polyline();
if (Math.Abs(end.X - start.X) <= 180) // Doesn't cross dateline
{
PointCollection pnts = GetGeodesicPoints(start, end);
line.Paths.Add(pnts);
}
else
{
double lon1 = start.X / 180 * Math.PI;
double lon2 = end.X / 180 * Math.PI;
double lat1 = start.Y / 180 * Math.PI;
double lat2 = end.Y / 180 * Math.PI;
double latA = LatitudeAtLongitude(lat1, lon1, lat2, lon2, Math.PI) / Math.PI * 180;
//double latB = LatitudeAtLongitude(lat1, lon1, lat2, lon2, -180) / Math.PI * 180; line.Paths.Add(GetGeodesicPoints(start, new MapPoint(start.X < 0 ? -180 : 180, latA)));
line.Paths.Add(GetGeodesicPoints(new MapPoint(start.X < 0 ? 180 : -180, latA), end));
}
return line; } private static PointCollection GetGeodesicPoints(MapPoint start, MapPoint end)
{
double lon1 = start.X / 180 * Math.PI;
double lon2 = end.X / 180 * Math.PI;
double lat1 = start.Y / 180 * Math.PI;
double lat2 = end.Y / 180 * Math.PI;
double dX = end.X - start.X;
int points = (int)Math.Floor(Math.Abs(dX));
dX = lon2 - lon1;
PointCollection pnts = new PointCollection();
pnts.Add(start);
for (int i = 1; i < points; i++)
{
double lon = lon1 + dX / points * i;
double lat = LatitudeAtLongitude(lat1, lon1, lat2, lon2, lon);
pnts.Add(new MapPoint(lon / Math.PI * 180, lat / Math.PI * 180));
}
pnts.Add(end);
return pnts;
} /// <summary>
/// Gets the latitude at a specific longitude for a great circle defined by p1 and p2.
/// </summary>
/// <param name="p1">The p1.</param>
/// <param name="p2">The p2.</param>
/// <param name="lon">The longitude in degrees.</param>
/// <returns></returns>
private static double LatitudeAtLongitude(MapPoint p1, MapPoint p2, double lon)
{
double lon1 = p1.X / 180 * Math.PI;
double lon2 = p2.X / 180 * Math.PI;
double lat1 = p1.Y / 180 * Math.PI;
double lat2 = p2.Y / 180 * Math.PI;
lon = lon / 180 * Math.PI;
return LatitudeAtLongitude(lat1, lon1, lat2, lon2, lon) / Math.PI * 180;
} /// <summary>
/// Gets the latitude at a specific longitude for a great circle defined by lat1,lon1 and lat2,lon2.
/// </summary>
/// <param name="lat1">The start latitude in radians.</param>
/// <param name="lon1">The start longitude in radians.</param>
/// <param name="lat2">The end latitude in radians.</param>
/// <param name="lon2">The end longitude in radians.</param>
/// <param name="lon">The longitude in radians for where the latitude is.</param>
/// <returns></returns>
private static double LatitudeAtLongitude(double lat1, double lon1, double lat2, double lon2, double lon)
{
return Math.Atan((Math.Sin(lat1) * Math.Cos(lat2) * Math.Sin(lon - lon2)
- Math.Sin(lat2) * Math.Cos(lat1) * Math.Sin(lon - lon1)) / (Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(lon1 - lon2)));
}
/// <summary>
/// Gets the true bearing at a distance from the start point towards the new point.
/// </summary>
/// <param name="start">The start point.</param>
/// <param name="end">The point to get the bearing towards.</param>
/// <param name="distanceKM">The distance in kilometers travelled between start and end.</param>
/// <returns></returns>
public static double GetTrueBearing(MapPoint start, MapPoint end, double distanceKM)
{
double d = distanceKM / EarthRadius; //Angular distance in radians
double lon1 = start.X / 180 * Math.PI;
double lat1 = start.Y / 180 * Math.PI;
double lon2 = end.X / 180 * Math.PI;
double lat2 = end.Y / 180 * Math.PI;
double tc1;
if (Math.Sin(lon2 - lon1) < 0)
tc1 = Math.Acos((Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(d)) / (Math.Sin(d) * Math.Cos(lat1)));
else
tc1 = 2 * Math.PI - Math.Acos((Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(d)) / (Math.Sin(d) * Math.Cos(lat1)));
return tc1 / Math.PI * 180;
} /// <summary>
/// Gets the point based on a start point, a heading and a distance.
/// </summary>
/// <param name="start">The start.</param>
/// <param name="distanceKM">The distance KM.</param>
/// <param name="heading">The heading.</param>
/// <returns></returns>
public static MapPoint GetPointFromHeading(MapPoint start, double distanceKM, double heading)
{
double brng = heading / 180 * Math.PI;
double lon1 = start.X / 180 * Math.PI;
double lat1 = start.Y / 180 * Math.PI;
double dR = distanceKM / 6378.137; //Angular distance in radians
double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(dR) + Math.Cos(lat1) * Math.Sin(dR) * Math.Cos(brng));
double lon2 = lon1 + Math.Atan2(Math.Sin(brng) * Math.Sin(dR) * Math.Cos(lat1), Math.Cos(dR) - Math.Sin(lat1) * Math.Sin(lat2));
double lon = lon2 / Math.PI * 180;
double lat = lat2 / Math.PI * 180;
while (lon < -180) lon += 360;
while (lat < -90) lat += 180;
while (lon > 180) lon -= 360;
while (lat > 90) lat -= 180;
return new MapPoint(lon, lat);
}
}
}
GIS简单计算Helper类的更多相关文章
- Task的在主线程处理异常信息的Helper类
最近使用task时候需要把异常记录日志,直接注入非单例模式的实例进入异步线程,在高并发情况下会出现一些问题. 所以需要把异常反馈给主线程 ,并且不在主线程里进行等待,研究相关资料后,自己写了一个简单的 ...
- [C#] 简单的 Helper 封装 -- RegularExpressionHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- MVC中使用HTML Helper类扩展HTML控件
文章摘自:http://www.cnblogs.com/zhangziqiu/archive/2009/03/18/1415005.html MVC在view页面,经常需要用到很多封装好的HTML控件 ...
- .NET中的加密算法总结(自定义加密Helper类续)
1.1.1 摘要 相信许多人都使用过.NET提供的加密算法,而且在使用的过程我们必须了解每种加密算法的特点(对称或非对称,密钥长度和初始化向量等等).我也看到过很多人写过.NET中加密算法总结,但我发 ...
- BigDecimal精确计算工具类
前言 在实际开发中,遇到例如货币,统计等商业计算的时候,一般需要采用java.math.BigDecimal类来进行精确计算.而这类操作通常都是可预知的,也就是通用的.所以,写了个工具类来方便以后的工 ...
- Util和Helper类
Util和Helper Util Util类,应该是一个无状态的类,只有静态方法. 比如在获取某些类的全局实例化对象的时候可以使用. public class ParamUtil { ... publ ...
- VC++ 一个简单的Log类
在软件开发中,为程序建立Log日志是很必要的,它可以记录程序运行的状态以及出错信息,方便维护和调试. 下面实现了一个简单的Log类,使用非常简单,仅供参考. // CLogHelper.h : hea ...
- 一点ASP.NET MVC Html.Helper类的方法
一点ASP.NET MVC Html.Helper类 这里就只写一个Html.ActionLink()和Html.DropdownList(). Html.ActionLink()里有三个参数,第一个 ...
- 之前采用的是Helper类的方法重构时改用了扩展方法
在手机端输入网址不方全,通常会将网址做成一个二维码,然后用手机扫一下就可以打开预览.我们每改一下样式,就在手机上点一下刷新或电脑上按一下F5,这在最初的时候,也不觉得有什么问题,因为拿到我手上的静态页 ...
随机推荐
- wind.onload和$(document).ready()的区别例示
例子: <html> <script type="text/javascript" src="jquery-1.7.1.min.js"> ...
- poj 1847 最短路简单题,dijkstra
1.poj 1847 Tram 最短路 2.总结:用dijkstra做的,算出a到其它各个点要改向的次数.其它应该也可以. 题意: 有点难懂.n个结点,每个点可通向ki个相邻点,默认指向第一个 ...
- spin_lock 和 spin_lock_irqsave
一 .spin_lock_irqsave . spin_unlock_irqrestore 如果自旋锁在中断处理函数中被用到,那么在获取该锁之前需要关闭本地中断,spin_lock_irqsave ...
- iOS 上线被拒收集
根据上线被拒的原因 自己 也在慢慢总结 希望对各位有所帮助 1)QQ 微信 等第三方平台 必须要做是否安装应用的检测
- spring Thymeleaf 中文乱码
前言: 项目用springmvc+Thymeleaf ,但是发现html中的所有中文都乱码. 但是自己的html的编码确定已经是UTF-8,在web.xml文件里面也添加了CharacterEncod ...
- dataTransfer.getData()在dragover,dragenter,dragleave中无法获取数据的问题
做拖拽相关效果时,想在ondragover时给被拖拽元素添加一些样式,于是在dragover事件的函数中通过dataTransfer.getData()获取在dragstart中设置的数据,然而发现d ...
- Iterator用法
<% List<Emp> all=DAOFactory.getIEmpDAOInstance().findAll(keyWord); Itrator<Emp> iter= ...
- [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大
17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...
- Android中Activity的四种启动模式
要了解Android的启动模式先要了解一下Activity的管理方式: 1.Activity的管理机制 Android的管理主要是通过Activity栈来进行的.当一个Activity启动时,系统根据 ...
- scala 代码随笔
def myfunc[T](iter: Iterator[T]) : Iterator[(T, T)] = { var res = List[(T, T)]() var pre = iter.next ...