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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
use std::collections::BTreeMap;

use anyhow::Result;
use serde::Deserialize;
use serde_json::Value;

use abstio::{CityName, MapName};
use geom::Speed;

use crate::{
    osm, AccessRestrictions, Direction, EditCmd, EditRoad, LaneSpec, LaneType, Map, OriginalRoad,
    PermanentMapEdits, RoadID,
};

/// When the PermanentMapEdits format changes, add a transformation here to automatically convert
/// edits written with the old format.
///
/// This problem is often solved with something like protocol buffers, but the resulting proto
/// usually winds up with permanent legacy fields, unless the changes are purely additive. For
/// example, protobufs wouldn't have helped with the fix_intersection_ids problem. Explicit
/// transformation is easier!
pub fn upgrade(mut value: Value, map: &Map) -> Result<PermanentMapEdits> {
    // c46a74f10f4f1976a48aa8642ac11717d74b262c added an explicit version field. There are a few
    // changes before that.
    if value.get("version").is_none() {
        // I don't remember the previous schema change before this. If someone files a bug and has
        // an older file, can add support for it then.
        fix_offset(&mut value);
        fix_intersection_ids(&mut value);

        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(0.into()));
    }
    if value["version"] == Value::Number(0.into()) {
        fix_road_direction(&mut value);
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(1.into()));
    }
    if value["version"] == Value::Number(1.into()) {
        fix_old_lane_cmds(&mut value, map)?;
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(2.into()));
    }
    if value["version"] == Value::Number(2.into()) {
        fix_merge_zones(&mut value);
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(3.into()));
    }
    if value["version"] == Value::Number(3.into()) {
        fix_map_name(&mut value);
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(4.into()));
    }
    if value["version"] == Value::Number(4.into()) {
        fix_phase_to_stage(&mut value);
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(5.into()));
    }
    if value["version"] == Value::Number(5.into()) {
        fix_adaptive_stages(&mut value);
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(6.into()));
    }
    if value["version"] == Value::Number(6.into()) {
        fix_plans(&mut value);
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(7.into()));
    }
    if value["version"] == Value::Number(7.into()) {
        fix_city_name(&mut value);
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(8.into()));
    }
    if value["version"] == Value::Number(8.into()) {
        fix_lane_widths(&mut value, map)?;
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(9.into()));
    }
    if value["version"] == Value::Number(9.into()) {
        fix_f64s(&mut value);
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(10.into()));
    }
    if value["version"] == Value::Number(10.into()) {
        remove_vehicle_caps(&mut value);
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(11.into()));
    }
    if value["version"] == Value::Number(11.into()) {
        fix_turn_restrictions(&mut value);
        value
            .as_object_mut()
            .unwrap()
            .insert("version".to_string(), Value::Number(12.into()));
    }
    if value["version"] == Value::Number(12.into()) {
        bail!("Breaking changes happened to map edits between v12 and v13. Recreate your edits from scratch; sorry.");
    }

    abstutil::from_json(&value.to_string().into_bytes())
}

// Recursively walks the entire JSON object. Will call transform on all of the map objects. If the
// callback returns true, won't recurse into that map.
fn walk<F: Fn(&mut serde_json::Map<String, Value>) -> bool>(value: &mut Value, transform: &F) {
    match value {
        Value::Array(list) => {
            for x in list {
                walk(x, transform);
            }
        }
        Value::Object(map) => {
            if !(transform)(map) {
                for x in map.values_mut() {
                    walk(x, transform);
                }
            }
        }
        _ => {}
    }
}

// eee179ce8a6c1e6133dc212b73c3f79b11603e82 added an offset_seconds field
fn fix_offset(value: &mut Value) {
    walk(value, &|map| {
        if map.len() == 1 && map.contains_key("TrafficSignal") {
            let ts = map
                .get_mut("TrafficSignal")
                .unwrap()
                .as_object_mut()
                .unwrap();
            if ts.get("offset_seconds").is_none() {
                ts.insert("offset_seconds".to_string(), Value::Number(0.into()));
            }
            true
        } else {
            false
        }
    })
}

// 11cefb118ab353d2e7fa5dceaab614a9b775e6ec changed { "osm_node_id": 123 } to just 123
fn fix_intersection_ids(value: &mut Value) {
    match value {
        Value::Array(list) => {
            for x in list {
                fix_intersection_ids(x);
            }
        }
        Value::Object(map) => {
            if map.len() == 1 && map.contains_key("osm_node_id") {
                *value = Value::Number(map["osm_node_id"].as_i64().unwrap().into());
            } else {
                for x in map.values_mut() {
                    fix_intersection_ids(x);
                }
            }
        }
        _ => {}
    }
}

// b137735e019adbe0f2a7372a579aa987f8496e19 changed direction from a boolean to an enum.
fn fix_road_direction(value: &mut Value) {
    walk(value, &|map| {
        if map.contains_key("num_fwd") {
            map.insert(
                "dir".to_string(),
                if map["fwd"].as_bool().unwrap() {
                    "Fwd".into()
                } else {
                    "Back".into()
                },
            );
            true
        } else {
            false
        }
    });
}

// b6ab06d51a3b22702b66db296ed4dfd27e8403a0 (and adjacent commits) removed some commands that
// target a single lane in favor of a consolidated ChangeRoad.
fn fix_old_lane_cmds(value: &mut Value, map: &Map) -> Result<()> {
    // TODO Can we assume map is in its original state? I don't think so... it may have edits
    // applied, right?

    let mut modified: BTreeMap<RoadID, EditRoad> = BTreeMap::new();
    let mut commands = Vec::new();
    for mut orig in value.as_object_mut().unwrap()["commands"]
        .as_array_mut()
        .unwrap()
        .drain(..)
    {
        let cmd = orig.as_object_mut().unwrap();
        if let Some(obj) = cmd.remove("ChangeLaneType") {
            let obj: ChangeLaneType = serde_json::from_value(obj).unwrap();
            let (r, idx) = obj.id.lookup(map)?;
            let road = modified.entry(r).or_insert_with(|| map.get_r_edit(r));
            if road.lanes_ltr[idx].lt != obj.orig_lt {
                bail!("{:?} lane type has changed", obj);
            }
            road.lanes_ltr[idx].lt = obj.lt;
        } else if let Some(obj) = cmd.remove("ReverseLane") {
            let obj: ReverseLane = serde_json::from_value(obj).unwrap();
            let (r, idx) = obj.l.lookup(map)?;
            let dst_i = map.find_i_by_osm_id(obj.dst_i)?;
            let road = modified.entry(r).or_insert_with(|| map.get_r_edit(r));
            let edits_dir = if dst_i == map.get_r(r).dst_i {
                Direction::Fwd
            } else if dst_i == map.get_r(r).src_i {
                Direction::Back
            } else {
                bail!("{:?}'s road doesn't point to dst_i at all", obj);
            };
            if road.lanes_ltr[idx].dir == edits_dir {
                bail!("{:?}'s road already points to dst_i", obj);
            }
            road.lanes_ltr[idx].dir = edits_dir;
        } else if let Some(obj) = cmd.remove("ChangeSpeedLimit") {
            let obj: ChangeSpeedLimit = serde_json::from_value(obj).unwrap();
            let r = map.find_r_by_osm_id(obj.id)?;
            let road = modified.entry(r).or_insert_with(|| map.get_r_edit(r));
            if road.speed_limit != obj.old {
                bail!("{:?} speed limit has changed", obj);
            }
            road.speed_limit = obj.new;
        } else if let Some(obj) = cmd.remove("ChangeAccessRestrictions") {
            let obj: ChangeAccessRestrictions = serde_json::from_value(obj).unwrap();
            let r = map.find_r_by_osm_id(obj.id)?;
            let road = modified.entry(r).or_insert_with(|| map.get_r_edit(r));
            if road.access_restrictions != obj.old {
                bail!("{:?} access restrictions have changed", obj);
            }
            road.access_restrictions = obj.new.clone();
        } else {
            commands.push(orig);
        }
    }

    for (r, new) in modified {
        let old = map.get_r_edit(r);
        commands
            .push(serde_json::to_value(EditCmd::ChangeRoad { r, old, new }.to_perma(map)).unwrap());
    }
    value.as_object_mut().unwrap()["commands"] = Value::Array(commands);
    Ok(())
}

// a3af291b2966c89d63b719e41821705077d063d2 added a map-wide merge_zones field
fn fix_merge_zones(value: &mut Value) {
    let obj = value.as_object_mut().unwrap();
    if !obj.contains_key("merge_zones") {
        obj.insert("merge_zones".to_string(), Value::Bool(true));
    }
}

// fef306489ba5e73735e0badad0172f3992d342db split map/city name into a dedicated struct
fn fix_map_name(value: &mut Value) {
    let root = value.as_object_mut().unwrap();
    if let Value::String(ref name) = root["map_name"].clone() {
        // At the time of this change, there likely aren't many people who have edits saved in
        // other maps.
        root.insert(
            "map_name".to_string(),
            serde_json::to_value(MapName::seattle(name)).unwrap(),
        );
    }
}

// 03fe9400c2ab98b8870e09562b1f35b91036f3cf renamed "phase" to "stage"
fn fix_phase_to_stage(value: &mut Value) {
    walk(value, &|map| {
        if let Some(list) = map.remove("phases") {
            map.insert("stages".to_string(), list);
        }
        if let Some(obj) = map.remove("phase_type") {
            map.insert("stage_type".to_string(), obj);
        }
        false
    });
}

// 34e8b0536a4517c68b0e16e5d55cb5e22dae37d8 remove adaptive signal stages.
fn fix_adaptive_stages(value: &mut Value) {
    walk(value, &|map| {
        if let Some(seconds) = map.remove("Adaptive") {
            // The old adaptive policy would repeat the entire stage if there was any demand at
            // all, so this isn't quite equivalent, since it only doubles the original time at
            // most. This adaptive policy never made any sense, so capturing its behavior more
            // clearly here isn't really worth it.
            let minimum = seconds.clone();
            let delay = Value::Number(1.into());
            let additional = seconds;
            map.insert(
                "Variable".to_string(),
                Value::Array(vec![minimum, delay, additional]),
            );
        }
        false
    });
}

// e08d76c8ba51d1ca8045e7692195b5f6245150c4 added traffic signal plans.
fn fix_plans(value: &mut Value) {
    walk(value, &|map| {
        if map.len() == 1 && map.contains_key("TrafficSignal") {
            let ts = map
                .get_mut("TrafficSignal")
                .unwrap()
                .as_object_mut()
                .unwrap();
            let mut plan = serde_json::Map::new();
            plan.insert("start_time_seconds".to_string(), Value::Number(0.into()));
            plan.insert("stages".to_string(), ts.remove("stages").unwrap());
            plan.insert(
                "offset_seconds".to_string(),
                ts.remove("offset_seconds").unwrap(),
            );
            ts.insert("plans".to_string(), Value::Array(vec![Value::Object(plan)]));
            true
        } else {
            false
        }
    })
}

// 39f5d50fcd981d62792562429003ce9725c17277 split city name into a dedicated struct
fn fix_city_name(value: &mut Value) {
    let root = value.as_object_mut().unwrap();
    let map_name = root["map_name"].as_object_mut().unwrap();
    if let Value::String(ref name) = map_name["city"].clone() {
        // At the time of this change, there are only a few maps that somebody likely had edits
        // for.
        let country = match name.as_ref() {
            "salzburg" => "at",
            "montreal" => "ca",
            "berlin" => "de",
            "paris" => "fr",
            "leeds" | "london" => "gb",
            "tel_aviv" => "il",
            "krakow" | "warsaw" => "pl",
            _ => "us",
        };
        map_name.insert(
            "city".to_string(),
            serde_json::to_value(CityName::new(country, name)).unwrap(),
        );
    }
}

// 0e09c1decfece370eff72d3433ae00b4aff3332f added lane width to EditRoad.
fn fix_lane_widths(value: &mut Value, map: &Map) -> Result<()> {
    for orig in value.as_object_mut().unwrap()["commands"]
        .as_array_mut()
        .unwrap()
    {
        let cmd = orig.as_object_mut().unwrap();
        if let Some(cmd) = cmd.get_mut("ChangeRoad") {
            let road_id: OriginalRoad = serde_json::from_value(cmd["r"].clone()).unwrap();
            let road = map.get_r(map.find_r_by_osm_id(road_id)?);
            let cmd = cmd.as_object_mut().unwrap();

            for key in ["old", "new"] {
                let mut lanes_ltr = Vec::new();
                for (idx, mut pair) in cmd[key]["lanes_ltr"]
                    .as_array_mut()
                    .unwrap()
                    .drain(..)
                    .enumerate()
                {
                    let pair = pair.as_array_mut().unwrap();
                    let lt: LaneType = serde_json::from_value(pair[0].clone()).unwrap();
                    let dir: Direction = serde_json::from_value(pair[1].clone()).unwrap();
                    lanes_ltr.push(LaneSpec {
                        lt,
                        dir,
                        // Before this commit, lane widths weren't modifiable, so this lookup works
                        // for both "old" and "new".
                        width: road.lanes[idx].width,
                        allowed_turns: Default::default(),
                    });
                }
                cmd[key]["lanes_ltr"] = serde_json::to_value(lanes_ltr).unwrap();
            }
        }
    }
    Ok(())
}

// f9c503ba4422ff51b609a7dc68855e8049bc9fd3 started encoding all f64's as rounded integers
fn fix_f64s(value: &mut Value) {
    walk(value, &|map| {
        // There's one Distance and one Speed field to re-encode
        for key in ["width", "speed_limit"] {
            if let Some(value) = map.get_mut(key) {
                if let Value::Number(num) = value {
                    if num.is_f64() {
                        let encoded = (num.as_f64().unwrap() * 10_000.0) as i32;
                        *value = Value::Number(encoded.into());
                    }
                }
            }
        }
        // ChangeRouteSchedule has an 'old' and 'new' Vec<Time>
        if map.contains_key("osm_rel_id") {
            for key in ["old", "new"] {
                for value in map.get_mut(key).unwrap().as_array_mut().unwrap() {
                    if let Value::Number(num) = value {
                        if num.is_f64() {
                            let encoded = (num.as_f64().unwrap() * 10_000.0) as i32;
                            *value = Value::Number(encoded.into());
                        }
                    }
                }
            }
        }

        false
    })
}

// a0dcc255c3212c491e1fa71546a8115fe319312e removed congestion capping
fn remove_vehicle_caps(value: &mut Value) {
    walk(value, &|map| {
        map.remove("cap_vehicles_per_hour");
        false
    });
}

// 6af258636f926a12650063abf243a0b87567b6e0 (well, a bit earlier) added turn restrictions to
// LaneSpecs
fn fix_turn_restrictions(value: &mut Value) {
    for orig in value.as_object_mut().unwrap()["commands"]
        .as_array_mut()
        .unwrap()
    {
        let cmd = orig.as_object_mut().unwrap();
        if let Some(cmd) = cmd.get_mut("ChangeRoad") {
            let cmd = cmd.as_object_mut().unwrap();
            for key in ["old", "new"] {
                for spec in cmd[key]["lanes_ltr"].as_array_mut().unwrap() {
                    // We could try to parse OSM tags and calculate this properly, but this field
                    // isn't used in A/B Street yet at all. So just fill out a blank list.
                    spec.as_object_mut()
                        .unwrap()
                        .insert("turn_restrictions".to_string(), Value::Array(Vec::new()));
                }
            }
        }
    }
}

// These're old structs used in fix_old_lane_cmds.
#[derive(Debug, Deserialize)]
struct OriginalLane {
    parent: OriginalRoad,
    num_fwd: usize,
    num_back: usize,
    dir: Direction,
    idx: usize,
}
#[derive(Debug, Deserialize)]
struct ChangeLaneType {
    id: OriginalLane,
    lt: LaneType,
    orig_lt: LaneType,
}
#[derive(Debug, Deserialize)]
struct ReverseLane {
    l: OriginalLane,
    // New intended dst_i
    dst_i: osm::NodeID,
}
#[derive(Debug, Deserialize)]
struct ChangeSpeedLimit {
    id: OriginalRoad,
    new: Speed,
    old: Speed,
}
#[derive(Debug, Deserialize)]
struct ChangeAccessRestrictions {
    id: OriginalRoad,
    new: AccessRestrictions,
    old: AccessRestrictions,
}

impl OriginalLane {
    fn lookup(&self, map: &Map) -> Result<(RoadID, usize)> {
        let r = map.get_r(map.find_r_by_osm_id(self.parent)?);
        let current_fwd = r.children_forwards();
        let current_back = r.children_backwards();
        if current_fwd.len() != self.num_fwd || current_back.len() != self.num_back {
            bail!(
                "number of lanes in {} is ({} fwd, {} back) now, but ({}, {}) in the edits",
                r.orig_id,
                current_fwd.len(),
                current_back.len(),
                self.num_fwd,
                self.num_back
            );
        }
        let l = if self.dir == Direction::Fwd {
            current_fwd[self.idx].0
        } else {
            current_back[self.idx].0
        };
        Ok((r.id, l.offset))
    }
}