-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonGenerator.cs
302 lines (269 loc) · 11.7 KB
/
JsonGenerator.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using WienerLinienApi.Information;
namespace WienerLinienApi.JsonGenerator
{
/// <summary>
/// Generates a combined JSON file of the 3 csv files returned by the WienerLinien API
/// </summary>
internal class JsonGenerator
{
#region "Constants"
/// <summary>
/// Static link for the stations-csv file
/// </summary>
private const string StationsLink = "http://data.wien.gv.at/csv/wienerlinien-ogd-haltestellen.csv";
/// <summary>
/// Static link for the lines-csv file
/// </summary>
private const string LinesLink = "http://data.wien.gv.at/csv/wienerlinien-ogd-linien.csv";
/// <summary>
/// Static link for the platforms-csv file
/// </summary>
private const string PlatformsLink = "http://data.wien.gv.at/csv/wienerlinien-ogd-steige.csv";
/// <summary>
/// List of lines
/// </summary>
private List<LinienModel> Lines { get; set; }
/// <summary>
/// List of platforms
/// </summary>
private List<SteigModel> Platforms { get; set; }
/// <summary>
/// List of stations
/// </summary>
private List<HaltestellenModel> Haltetellen { get; set; }
/// <summary>
/// Http client used for requests
/// </summary>
private readonly HttpClient client;
#endregion
#region "Methods"
public JsonGenerator()
{
client = new HttpClient();
}
/// <summary>
/// Creates and returns the JSON string
/// </summary>
/// <returns>Json model of all stations</returns>
public async Task<string> GetJsonAsync()
{
await DownloadFilesAsync();
// DownloadFiles();
var listPlatformsModel =
from platform in Platforms
join line in Lines on platform.FkLineId equals line.LineId
select new WienerLinienModel.PlatformsModel
{
Line = line.LineId,
Realtime = Convert.ToBoolean(Convert.ToInt16(line.Realtime)),
MeansOfTransport = line.MeansOfTransport,
RblNumber = platform.RblNumber.TryParse(0),
Area = platform.Area,
Direction = platform.Direction,
Order = platform.Order,
PlatformName = platform.PlatformId,
PlatformWgs84Lat = platform.PlatformWgs84Lat.TryParse(0.0),
PlatformWgs84Lon = platform.PlatformWgs84Lon.TryParse(0.0),
StationId = platform.FkStationId.TryParse(0)
};
var listModel =
from platforms in listPlatformsModel.ToList()
join haltestelle in Haltetellen on platforms.StationId equals int.Parse(haltestelle.StationId)
group platforms by haltestelle
into grp
select new WienerLinienModel
{
StationId = grp.Key.StationId.TryParse(0),
// StationId = IntTryParse(grp.Key.StationId),
Typ = grp.Key.Typ,
Diva = grp.Key.Diva,
Name = grp.Key.Name,
Municipality = grp.Key.Municipality,
MunicipalityId = grp.Key.MunicipalityId.TryParse(0),
Wgs84Lat = grp.Key.Wgs84Lat.TryParse(0.0),
Wgs84Lon = grp.Key.Wgs84Lon.TryParse(0.0),
Stand = grp.Key.Stand,
Platforms = grp.ToList()
};
var list = listModel.ToList();
var json = JsonConvert.SerializeObject(list);
return json;
}
/// <summary>
/// Downloads a string asynchronous
/// </summary>
/// <param name="url">The url you want to download from</param>
/// <returns>the downloaded string</returns>
private async Task<string> DownloadAsync(string url)
{
return await client.GetStringAsync(url);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private async Task DownloadFilesAsync()
{
await Task.Factory.StartNew(DownloadFiles);
}
/// <summary>
/// Downloads the 3 static CSV files and parses them to their corresponding models
/// </summary>
private void DownloadFiles()
{
var stationsA = DownloadAsync(StationsLink).Result;
var linesA = DownloadAsync(LinesLink).Result;
var platformsA = DownloadAsync(PlatformsLink).Result;
var listStations = stationsA.Remove(stationsA.LastIndexOf((Environment.NewLine), StringComparison.Ordinal))
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
.Skip(1)
.Select(columns => columns.Split(';'))
.Select(columns => new HaltestellenModel
{
StationId = columns[0].Replace("\"", ""),
Typ = columns[1].Replace("\"", ""),
Diva = columns[2].Replace("\"", ""),
Name = (columns[3]).Replace("\"", ""),
Municipality = columns[4].Replace("\"", ""),
MunicipalityId = columns[5].Replace("\"", ""),
Wgs84Lat = columns[6].Replace("\"", ""),
Wgs84Lon = columns[7].Replace("\"", ""),
}
)
;
Haltetellen = listStations.ToList();
var listLines = linesA.Remove(linesA.LastIndexOf((Environment.NewLine), StringComparison.Ordinal))
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
.Skip(1)
.Select(columns => columns.Split(';'))
.Select(columns => new LinienModel
{
LineId = columns[0].Replace("\"", ""),
Description = columns[1].Replace("\"", ""),
Order = columns[2].Replace("\"", ""),
Realtime = columns[3].Replace("\"", ""),
MeansOfTransport = MeansOfTransportWrapper.GetMeansOfTransportFromString(columns[4].Replace("\"", "")),
Stand = columns[5].Replace("\"", "")
})
;
Lines = listLines.ToList();
var listPlatforms = platformsA.Remove(platformsA.LastIndexOf((Environment.NewLine), StringComparison.Ordinal))
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
.Skip(1)
.Select(columns => columns.Split(';'))
.Select(columns => new SteigModel
{
PlatformId = columns[0].Replace("\"", ""),
FkLineId = columns[1].Replace("\"", ""),
FkStationId = columns[2].Replace("\"", ""),
Direction = columns[3].Replace("\"", ""),
Order = columns[4].Replace("\"", ""),
RblNumber = columns[5].Replace("\"", ""),
Area = columns[6].Replace("\"", ""),
Platform = columns[7].Replace("\"", ""),
PlatformWgs84Lat = columns[8].Replace("\"", ""),
PlatformWgs84Lon = columns[9].Replace("\"", ""),
Stand = columns[10].Replace("\"", "")
})
;
Platforms = listPlatforms.ToList();
// System.Diagnostics.Debug.WriteLine(Platforms[0].RblNumber);
}
#endregion
#region Structs and Models
public struct HaltestellenModel
{
public string StationId { get; set; }
public string Typ { get; set; }
public string Diva { get; set; }
public string Name { get; set; }
public string Municipality { get; set; }
public string MunicipalityId { get; set; }
public string Wgs84Lat { get; set; }
public string Wgs84Lon { get; set; }
public string Stand { get; set; }
}
public struct SteigModel
{
public string PlatformId { get; set; }
public string FkLineId { get; set; }
public string FkStationId { get; set; }
public string Direction { get; set; }
public string Order { get; set; }
public string RblNumber { get; set; }
public string Area { get; set; }
public string Platform { get; set; }
public string PlatformWgs84Lat { get; set; }
public string PlatformWgs84Lon { get; set; }
public string Stand { get; set; }
}
public struct LinienModel
{
public string LineId { get; set; }
public string Description { get; set; }
public string Order { get; set; }
public string Realtime { get; set; }
public MeansOfTransport MeansOfTransport { get; set; }
public string Stand { get; set; }
}
public struct WienerLinienModel
{
public int StationId { get; set; }
public string Typ { get; set; }
public string Diva { get; set; }
public string Name { get; set; }
public string Municipality { get; set; }
public int MunicipalityId { get; set; }
public double Wgs84Lat { get; set; }
public double Wgs84Lon { get; set; }
public string Stand { get; set; }
public List<PlatformsModel> Platforms { get; set; }
public struct PlatformsModel
{
public string Line { get; set; }
public bool Realtime { get; set; }
public MeansOfTransport MeansOfTransport { get; set; }
public int RblNumber { get; set; }
public string Area { get; set; }
public string Direction { get; set; }
public string Order { get; set; }
public string PlatformName { get; set; }
public double PlatformWgs84Lat { get; set; }
public double PlatformWgs84Lon { get; set; }
public int StationId { get; set; }
}
}
#endregion
}
public static class StringExtensions
{
/// <summary>
/// Tries to parse a string into an integer
/// </summary>
/// <param name="input">The string you want to convert</param>
/// <param name="valueIfNotConverted">The value the int should have if the input is not valid</param>
/// <returns></returns>
public static int TryParse(this string input, int valueIfNotConverted)
{
int value;
return int.TryParse(input, out value) ? value : valueIfNotConverted;
}
/// <summary>
/// Tries to parse a string into a double
/// </summary>
/// <param name="input">The string you want to convert</param>
/// <param name="valueIfNotConverted">The value the double should have if the input is not valid</param>
/// <returns></returns>
public static double TryParse(this string input, double valueIfNotConverted)
{
double value;
return double.TryParse(input, out value) ? value : valueIfNotConverted;
}
}
}