-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoUtils.cs
76 lines (64 loc) · 2.42 KB
/
GeoUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
namespace FaaMvaToSectorFile
{
public class MyPoint
{
public float X { get; set; }
public float Y { get; set; }
public MyPoint(float x, float y)
{
X = x;
Y = y;
}
}
public class LatLng
{
public double Lat { get; set; }
public double Lon { get; set; }
public LatLng(double lat, double lon)
{
Lat = lat;
Lon = lon;
}
}
public static class GeoUtils
{
public const int NmPerDegLat = 60;
public const int NmPerDegLon = 51;
public const double MagVar = 0;
public const double Width = 1920; // we just need a size to do some quick math
public const double Height = 1080;
public static double SecCosMag = Math.Cos(-MagVar * (Math.PI / 180));
public static double SecSinMag = Math.Sin(-MagVar * (Math.PI / 180));
public static double RevSecCosMag = Math.Cos(MagVar * (Math.PI / 180));
public static double RevSecSinMag = Math.Sin(MagVar * (Math.PI / 180));
public static string ToDMS(this double coord)
{
var isLat = coord >= -90 && coord <= 90;
var nsew = (coord >= 0.0) ? (isLat ? "N" : "E") : (isLat ? "S" : "W");
coord = Math.Abs(coord);
var deg = Math.Floor(coord);
var min = Math.Floor((coord - deg) * 60.0);
var sec = (coord - deg - (min / 60.0)) * 3600.0;
return $"{nsew}{deg:000}.{min:00}.{sec:00.000}";
}
public static MyPoint GeoToPixels(double lat, double lon, double centerLat, double centerLon)
{
var dx = (lon - centerLon) * NmPerDegLon;
var dy = (centerLat - lat) * NmPerDegLat;
var dx1 = (dx * SecCosMag) - (dy * SecSinMag);
var dy1 = (dx * SecSinMag) + (dy * SecCosMag);
var x = (dx1 + (Width / 2));
var y = (dy1 + (Height / 2));
return new MyPoint((float) x, (float) y);
}
public static LatLng PixelsToGeo(double x, double y, double centerLat, double centerLon)
{
var dx = x - Width / 2;
var dy = y - Height / 2;
var dx1 = (dx * RevSecCosMag - dy * RevSecSinMag) / NmPerDegLon;
var dy1 = (dx * RevSecSinMag + dy * RevSecCosMag) / NmPerDegLat;
return new LatLng(centerLat - dy1, centerLon + dx1);
}
}
}