-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
319 lines (267 loc) · 11.2 KB
/
Program.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// See https://aka.ms/new-console-template for more information
using AngleSharp.Html.Parser;
using prepareBikeParking;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Text;
// Fetch bike share locations
var locationsList = await FetchBikeShareLocations2();
// convert each line into this template:
var items = locationsList.OrderBy(x => x.id).Select(generateGeojsonLine);
// join lines and save as geojson file
var geojson = string.Join("\n", items);
File.WriteAllText("../../../bikeshare.geojson", geojson);
// Get the last committed version of the file
string lastCommittedVersion = GitDiffToGeojson.GetLastCommittedVersion();
// Parse the last committed version into a list of GeoPoints
List<GeoPoint> lastCommittedPoints = lastCommittedVersion
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
.Select(GeoPoint.ParseLine)
.ToList();
// Compare the current points with the last committed points
var currentPoints = locationsList;
var addedPoints = currentPoints.ExceptBy(lastCommittedPoints.Select(p => p.id), p => p.id).ToList();
var removedPoints = lastCommittedPoints.ExceptBy(currentPoints.Select(p => p.id), p => p.id).ToList();
var movedOrRenamedPoints = currentPoints
.Join(lastCommittedPoints,
current => current.id,
last => last.id,
(current, last) => new
{
Current = current,
Last = last,
HasMoved =
GeoPoint.ParseCoords(current.lat) != GeoPoint.ParseCoords(last.lat) ||
GeoPoint.ParseCoords(current.lon) != GeoPoint.ParseCoords(last.lon),
HasRenamed = current.name.Trim() != last.name.Trim()
})
.Where(p => p.HasMoved || p.HasRenamed)
.ToList();
var movedPointsFinal = movedOrRenamedPoints
.Where(p => p.HasMoved)
.Select(p => p.Current)
.ToList();
var renamedPoints = movedOrRenamedPoints
.Where(p => p.HasRenamed && !p.HasMoved)
.Select(p => p.Current)
.ToList();
var addedPointsFinal = addedPoints.ToList();
var removedPointsFinal = removedPoints.ToList();
File.WriteAllText("../../../bikeshare_renamed.geojson", string.Join("\n", renamedPoints.OrderBy(x => x.id).Select(generateGeojsonLine)));
File.WriteAllText("../../../bikeshare_added.geojson", string.Join("\n", addedPointsFinal.OrderBy(x => x.id).Select(generateGeojsonLine)));
File.WriteAllText("../../../bikeshare_toreview.geojson", string.Join("\n", addedPoints.OrderBy(x => x.id).Select(generateGeojsonLine)));
File.WriteAllText("../../../bikeshare_removed.geojson", string.Join("\n", removedPointsFinal.OrderBy(x => x.id).Select(generateGeojsonLine)));
File.WriteAllText("../../../bikeshare_moved.geojson", string.Join("\n", movedPointsFinal.OrderBy(x => x.id).Select(generateGeojsonLine)));
return;
// Call the function to create the Maproulette task
await CreateMaprouletteRemoveTask(53785);
/*
{
"last_updated": 1734912066,
"ttl": 6,
"data": {
"stations": [
{
"station_id": "7000",
"name": "Fort York Blvd / Capreol Ct",
"physical_configuration": "REGULAR",
"lat": 43.639832,
"lon": -79.395954,
"altitude": null,
"address": "Fort York Blvd / Capreol Ct",
"capacity": 47,
"is_charging_station": false,
"rental_methods": [
"KEY",
"TRANSITCARD",
"CREDITCARD",
"PHONE"
],
"groups": [
"South"
],
"obcn": "647-643-9607",
"short_name": "647-643-9607",
"nearby_distance": 500,
"_ride_code_support": true,
"rental_uris": {}
},
{
"station_id": "7001",
"name": "Wellesley Station Green P",
"physical_configuration": "ELECTRICBIKESTATION",
"lat": 43.66496415990742,
"lon": -79.38355031526893,
"altitude": null,
"address": "Yonge / Wellesley",
"post_code": "M4Y 1G7",
"capacity": 23,
"is_charging_station": true,
"rental_methods": [
"KEY",
"TRANSITCARD",
"CREDITCARD",
"PHONE"
],
"groups": [
"E-Charging ",
"South"
],
"obcn": "416-617-9576",
"short_name": "416-617-9576",
"nearby_distance": 500,
"_ride_code_support": true,
"rental_uris": {}
}]
}
}
*/
async Task<List<GeoPoint>> FetchBikeShareLocations2()
{
//download html from url
var url = "https://tor.publicbikesystem.net/ube/gbfs/v1/en/station_information";
var fetchedJson = await new HttpClient().GetStringAsync(url);
var parsedJson = JsonSerializer.Deserialize<JsonElement>(fetchedJson);
var a = parsedJson.GetProperty("data").GetProperty("stations");
var locationsDict = a.EnumerateArray().ToList();
var locationList =
locationsDict
.Select(x => new GeoPoint
{
id = x.GetProperty("station_id").GetString(),
name = x.GetProperty("name").GetString(),
capacity = x.GetProperty("capacity").GetInt32(),
lat = x.GetProperty("lat").GetDouble().ToString(System.Globalization.CultureInfo.InvariantCulture),
lon = x.GetProperty("lon").GetDouble().ToString(System.Globalization.CultureInfo.InvariantCulture)
});
return locationList.ToList();
}
async Task<List<GeoPoint>> FetchBikeShareLocations1()
{
//download html from url
var url = "https://bikesharetoronto.com/system-map/";
var client = new HttpClient();
var html = await client.GetStringAsync(url);
//parse html
var parser = new HtmlParser();
var document = parser.ParseDocument(html);
var displaylist = document.GetElementById("infoWind");
var namesAndCapacities =
Enumerable.Select(displaylist.Children
, x => new
{
id = x.GetAttribute("id"),
name = x.Children[0].TextContent.Trim(),
aproxCapacity = x.Children.Length == 4 ?
IntParseOrZero(x.Children[1].TextContent) +
IntParseOrZero(x.Children[3].TextContent) +
IntParseOrZero(x.Children[2].TextContent) : 0,
});
var locations = document.GetElementById("arr_adr").GetAttribute("value");
//parse json
var json = JsonSerializer.Deserialize<Dictionary<string, string>>(locations);
//dictionary with value split by "_" symbol
var locationsDict = json.ToDictionary(x => x.Key, x => x.Value.Split("_"));
// dictionary converted to list of objects with id, lat, lon values.
var locationsList = locationsDict
.Select(x =>
new GeoPoint
{
id = x.Key,
name = namesAndCapacities.Single(y => y.id == x.Key).name,
capacity = namesAndCapacities.Single(y => y.id == x.Key).aproxCapacity,
lat = x.Value[0],
lon = x.Value[1]
})
.ToList();
return locationsList;
}
// To get a Maproulette API Key:
// 1. Sign up for a Maproulette account at https://maproulette.org/user/profile
// 2. In the settings page, navigate to the "API KEY" section at the bottom
// 5. Copy the generated API key
// 6. Set the API key as an environment variable named MAPROULETTE_API_KEY
// - On Windows (PowerShell): [Environment]::SetEnvironmentVariable("MAPROULETTE_API_KEY", "your_api_key_here", "User")
// - On macOS/Linux: export MAPROULETTE_API_KEY=your_api_key_here
// Note: After setting the environment variable, you may need to restart your terminal or IDE for the changes to take effect
async Task CreateMaprouletteRemoveTask(int projectId)
{
var client = new HttpClient();
var apiKey = Environment.GetEnvironmentVariable("MAPROULETTE_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("MAPROULETTE_API_KEY environment variable is not set.");
return;
}
client.DefaultRequestHeaders.Add("apiKey", apiKey);
var challengeName = $"API TEST: Remove Bikeshare Toronto stations - {DateTime.Now:yyyy-MM-dd} {DateTime.Now}";
// Create challenge
var challengeData = new
{
name = challengeName,
description = "Remove Bikeshare Toronto stations that no longer exist",
instruction = "Please verify and remove the Bikeshare Toronto station from OpenStreetMap.",
blurb = "Please verify and remove the Bikeshare Toronto station from OpenStreetMap.",
enabled = false,
difficulty = 2,
requiresLocal = false,
// localGeoJSON = File.ReadAllText("../../../bikeshare_removed.geojson"),
parent = projectId,
};
var challengeResponse = await client.PostAsync(
"https://maproulette.org/api/v2/challenge",
new StringContent(JsonSerializer.Serialize(challengeData), Encoding.UTF8, "application/json")
);
if (!challengeResponse.IsSuccessStatusCode)
{
Console.WriteLine($"Failed to create challenge: {await challengeResponse.Content.ReadAsStringAsync()}");
return;
}
var challengeResult = JsonSerializer.Deserialize<JsonElement>(await challengeResponse.Content.ReadAsStringAsync());
var challengeId = challengeResult.GetProperty("id").GetInt32();
//var values = File.ReadAllText("../../../bikeshare_removed.geojson").Replace("\u001e", "");
var values = File.ReadAllText("../../../linebyline.geojson").Replace("\u001e", "");
var taskResponse = await client.PutAsync(
$"https://maproulette.org/api/v2/challenge/{challengeId}/addTasks",
new StringContent(values, Encoding.UTF8, "application/json")
);
if (!taskResponse.IsSuccessStatusCode)
{
Console.WriteLine($"Failed to create task: {await taskResponse.Content.ReadAsStringAsync()}");
}
Console.WriteLine($"Maproulette challenge created: {challengeName} (ID: {challengeId})");
}
static string generateGeojsonLine(GeoPoint x)
{
var template = "\u001e{{\"type\":\"FeatureCollection\"" +
",\"features\":[{{\"type\":\"Feature\",\"geometry\":{{\"type\":\"Point\"," +
"\"coordinates\":[{0},{1}]}},\"properties\":{{" +
"\"address\":\"{2}\"," +
"\"latitude\":\"{1}\"," +
"\"longitude\":\"{0}\"," +
"\"name\":\"{3}\"," +
"\"capacity\":\"{4}\"," +
"\"operator\":\"{5}\"}}}}]}}";
return string.Format(
template,
GeoPoint.ParseCoords( x.lon).ToString(System.Globalization.CultureInfo.InvariantCulture),
GeoPoint.ParseCoords(x.lat).ToString(System.Globalization.CultureInfo.InvariantCulture),
x.id,
x.name.Trim(),
x.capacity,
"BikeShare Toronto");
}
static int IntParseOrZero(string inp)
{
var yes = int.TryParse(Regex.Match(inp, @"\d+").Value, out var result);
return yes ? result : 0;
}
//amenity = bicycle_rental
//bicycle_rental = docking_station
//brand = Bike Share Toronto
//brand:wikidata = Q17018523
//brand: wikipedia = en:Bike Share Toronto
//name=Bike Share Toronto
//operator=Toronto Parking Authority
//operator:wikidata = Q7826466
//operator:wikipedia= en:Toronto Parking Authority