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,这在最初的时候,也不觉得有什么问题,因为拿到我手上的静态页 ...
随机推荐
- 没人告诉你关于z-index的一些事
关于z-index的问题是很多程序员都不知道它是如何起作用的.说起来不难,但是大部分人并没有花时间去看规范,这往往会照成严重的后果. 你不信?那就一起来看看下面的问题. 问题 在下面的HTML我们写了 ...
- 【noiOJ】p8210
10:河中跳房子 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一个岩石跳到另一个岩石.这项激动人心 ...
- 通过Ajax post Json类型的数据到Controller
View function postSimpleData() { $.ajax({ type: "POST", url: "/Service/SimpleData&quo ...
- ThinkPHP3.2.3--Linux服务器首页文件index.php路径配置问题
在windows服务器环境下,可以define ('SITE_URL','http://192.168.1.101/'); 但上传到linux服务器环境下不能正常解析,可使用相对路径:define ( ...
- 【转】在C#中使用SendMessage
SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...
- POI Word 模板 文字 图片 替换
实验环境:POI3.7+Word2007 Word模板: 替换后效果: 代码: 1.入口文件 public class Test { public static void main(String[] ...
- [zt]矩阵求导公式
今天推导公式,发现居然有对矩阵的求导,狂汗--完全不会.不过还好网上有人总结了.吼吼,赶紧搬过来收藏备份. 基本公式:Y = A * X --> DY/DX = A'Y = X * A --&g ...
- Odoo Website 替换 谷歌地图为 百度地图
由于众所周知的原因,国内使用谷歌地图是件非常痛苦的事,更为接地气的做法是替换为百度地图. 模块地址参见群公告. 演示地址:http://timesup.cn:8069
- Git分布式项目管理
Git简介 Git是什么? Git和SVN一样都是一种高效的管理代码的系统. Git是目前世界上最先进的分布式版本控制系统(没有之一). 创建版本库 什么是版本库呢?版本库又名仓库,英文名 ...
- js 字符串拼接
正常来说已经使用es6 的 模板了如`` //页面层 layer.open({ type: 1, content:`<div class="child_card"> & ...