-
Notifications
You must be signed in to change notification settings - Fork 1
/
expand_abbreviations.js
117 lines (103 loc) · 3.31 KB
/
expand_abbreviations.js
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
function current_layer() {
var layers = require("josm/layers");
return layers.activeLayer;
}
function name_expand(name) {
var console = require("josm/scriptingconsole");
var expanded = [];
var words = name.split(" ");
var mappings = [
["dr", "Drive"],
["ave", "Avenue"],
["av", "Avenue"],
["blvd", "Boulevard"],
["bl", "Boulevard"],
["blv", "Boulevard"],
["cr", "Crossing"],
["cir", "Circle"],
["ct", "Court"],
["cv", "Cove"],
["ext", "Extension"],
["extn", "Extension"],
["hwy", "Highway"],
["ln", "Lane"],
["pl", "Place"],
["st", "Street"],
["snt", "Saint"],
["tr", "Trail"],
["trl", "Trail"],
["wy", "Way"],
["pkwy", "Parkway"],
["pky", "Parkway"],
["rd", "Road"],
["ca", "Calle"],
["expy", "Expressway"],
["n", "North"],
["e", "East"],
["s", "South"],
["w", "West"],
["ne", "Northeast"],
["nw", "Northwest"],
["se", "Southeast"],
["sw", "Southwest"],
];
for (var i = 0; i < words.length; ++i) {
var word = words[i];
var lword = word.toLowerCase();
if (lword.length() == 0) continue;
// Remove any trailing "." so things like "Dr." will match "Dr"
if (lword.slice(lword.length() - 1, lword.length()) == '.') {
lword = lword.slice(0, lword.length() - 1);
}
if (lword == "st" && i == 0) {
lword = "snt";
}
var replaced = false;
// Check all mappings, replace if match found
for (var j = 0; j < mappings.length; ++j) {
if (lword == mappings[j][0]) {
if (lword == "cr") {
console.println(" WARNING: Cr could be Crossing or Creek")
}
else if (lword == "st" || lword == "snt") {
console.println(" WARNING: St could be Street or Saint")
}
expanded.push(mappings[j][1]);
replaced = true;
}
}
// Keep original word
if (!replaced) expanded.push(word);
}
return expanded.join(" ");
}
function main() {
var console = require("josm/scriptingconsole");
var layer = current_layer();
if (layer == null) {
console.println("Current layer empty, aborting.");
return;
}
var util = require("josm/util");
var command = require("josm/command");
var dataset = layer.data;
var result = dataset.query("type:way");
var renames = 0;
console.println("\nFound " + result.length + " ways in current layer.");
for (j = 0; j < result.length; j++) {
var way = result[j];
var name = way.get("name");
if (name == null) continue;
if (name.length() < 4) continue;
var words = name.split(" ");
if (words.length < 2) continue;
newname = name_expand(name);
if (name == newname) continue;
console.println(" Renaming [" + name + "] to [" + newname + "]");
layer.apply( command.change(dataset.way(way.id), {tags: {name: newname}}) );
renames++;
way.setModified(true);
}
console.println("Modified " + renames + " ways.");
}
main();