C#的经度和经度边界框?

前端之家收集整理的这篇文章主要介绍了C#的经度和经度边界框?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我只想找到一个简单的C#类,它接收起始纬度和经度以及距离并找到边界框(max lat,min lat,max lon,min lon).在这里还有其他类似的问题,但是没有一个真正回答这个问题,那些问题不在C#中.

救命.

解决方法

这就是你要求的.感谢Federico A. Ramponi在 Python here中编写原文.
  1. public class MapPoint
  2. {
  3. public double Longitude { get; set; } // In Degrees
  4. public double Latitude { get; set; } // In Degrees
  5. }
  6.  
  7. public class BoundingBox
  8. {
  9. public MapPoint MinPoint { get; set; }
  10. public MapPoint MaxPoint { get; set; }
  11. }
  12.  
  13. // Semi-axes of WGS-84 geoidal reference
  14. private const double WGS84_a = 6378137.0; // Major semiaxis [m]
  15. private const double WGS84_b = 6356752.3; // Minor semiaxis [m]
  16.  
  17. // 'halfSideInKm' is the half length of the bounding Box you want in kilometers.
  18. public static BoundingBox GetBoundingBox(MapPoint point,double halfSideInKm)
  19. {
  20. // Bounding Box surrounding the point at given coordinates,// assuming local approximation of Earth surface as a sphere
  21. // of radius given by WGS84
  22. var lat = Deg2rad(point.Latitude);
  23. var lon = Deg2rad(point.Longitude);
  24. var halfSide = 1000 * halfSideInKm;
  25.  
  26. // Radius of Earth at given latitude
  27. var radius = WGS84EarthRadius(lat);
  28. // Radius of the parallel at given latitude
  29. var pradius = radius * Math.Cos(lat);
  30.  
  31. var latMin = lat - halfSide / radius;
  32. var latMax = lat + halfSide / radius;
  33. var lonMin = lon - halfSide / pradius;
  34. var lonMax = lon + halfSide / pradius;
  35.  
  36. return new BoundingBox {
  37. MinPoint = new MapPoint { Latitude = Rad2deg(latMin),Longitude = Rad2deg(lonMin) },MaxPoint = new MapPoint { Latitude = Rad2deg(latMax),Longitude = Rad2deg(lonMax) }
  38. };
  39. }
  40.  
  41. // degrees to radians
  42. private static double Deg2rad(double degrees)
  43. {
  44. return Math.PI * degrees / 180.0;
  45. }
  46.  
  47. // radians to degrees
  48. private static double Rad2deg(double radians)
  49. {
  50. return 180.0 * radians / Math.PI;
  51. }
  52.  
  53. // Earth radius at a given latitude,according to the WGS-84 ellipsoid [m]
  54. private static double WGS84EarthRadius(double lat)
  55. {
  56. // http://en.wikipedia.org/wiki/Earth_radius
  57. var An = WGS84_a * WGS84_a * Math.Cos(lat);
  58. var Bn = WGS84_b * WGS84_b * Math.Sin(lat);
  59. var Ad = WGS84_a * Math.Cos(lat);
  60. var Bd = WGS84_b * Math.Sin(lat);
  61. return Math.Sqrt((An*An + Bn*Bn) / (Ad*Ad + Bd*Bd));
  62. }

猜你在找的C#相关文章