forked from vbguyny/ws4kp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateStationsJson.aspx
165 lines (137 loc) · 5.26 KB
/
GenerateStationsJson.aspx
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Globalization" %>
<script runat="server">
protected void Page_Load()
{
string TextFileName = Server.MapPath(@"stations.txt");
string JsFileName = Server.MapPath(@"stations.js");
string JsText = string.Empty;
using (TextWriter tw = new StreamWriter(JsFileName))
{
JsText = "var _StationInfo = {" + Environment.NewLine;
tw.Write(JsText);
using (TextReader tr = new StreamReader(TextFileName))
{
while (true)
{
string Line = tr.ReadLine();
string StationId = string.Empty;
string City = string.Empty;
string State = string.Empty;
string Latitude = string.Empty;
string Longitude = string.Empty;
if (Line == null)
{
// Finished reading entire file.
break;
}
else if (string.IsNullOrWhiteSpace(Line) == true)
{
// Empty Line.
continue;
}
else if (Line.StartsWith("!") == true)
{
// Comment line.
continue;
}
else if (Line.Length == 30)
{
// State header.
continue;
}
else if (Line.Length != 83)
{
continue;
}
StationId = Line.Substring(20, 4);
if (string.IsNullOrWhiteSpace(StationId) == true)
{
// Empty Station Id.
continue;
}
else if (StationId == "ICAO")
{
// Station Id header.
continue;
}
State = Line.Substring(0, 2);
if (string.IsNullOrWhiteSpace(State) == true)
{
continue;
}
City = NormalizeCityName(Line.Substring(3, 16));
Latitude = ConvertDegreesMinutesToDegreesDecimal(Line.Substring(39, 7));
Longitude = ConvertDegreesMinutesToDegreesDecimal(Line.Substring(47, 7));
JsText = " " + StationId + ":" + Environment.NewLine;
JsText += " {" + Environment.NewLine;
JsText += " StationId: '" + StationId + "'," + Environment.NewLine;
JsText += " City: \"" + City + "\"," + Environment.NewLine;
JsText += " State: '" + State + "'," + Environment.NewLine;
JsText += " Latitude: '" + Latitude + "'," + Environment.NewLine;
JsText += " Longitude: '" + Longitude + "'," + Environment.NewLine;
JsText += " }," + Environment.NewLine;
tw.Write(JsText);
}
}
JsText = "}" + Environment.NewLine;
tw.Write(JsText);
}
}
private string ConvertDegreesMinutesToDegreesDecimal(string DegreesMinutes)
{
// Example: 39 51N -> 39.85
// Example: 104 39W -> -104.65
//return "";
string[] DegreeArray = DegreesMinutes.Split(' ');
double Degrees = Convert.ToDouble(DegreeArray[0]);
double Minutes = Convert.ToDouble(DegreeArray[1].Substring(0, 2));
string Direction = DegreeArray[1].Substring(2, 1);
double Sign = 0;
switch (Direction)
{
case "N":
case "E":
Sign = 1;
break;
case "S":
case "W":
Sign = -1;
break;
}
return Convert.ToString((Degrees + (Minutes / 60)) * Sign);
}
private string NormalizeCityName(string CityName)
{
// Return the city name only with the first character of word capitalized.
CityName = CityName.TrimEnd();
//string[] CityNameArray = CityName.Split(' ');
//string NormalizedCityName = string.Empty;
//foreach (string CityNamePart in CityNameArray)
//{
// if (string.IsNullOrEmpty(CityNamePart) == true)
// {
// continue;
// }
// NormalizedCityName += CityNamePart.Substring(0, 1).ToUpper() + CityNamePart.Substring(1).ToLower() + " ";
//}
//return NormalizedCityName.TrimEnd();
TextInfo textInfo = new CultureInfo("en-US",false).TextInfo;
return textInfo.ToTitleCase(CityName.ToLower());
}
</script>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblTest" Text="DONE" runat="server"></asp:Label>
</form>
</body>
</html>