/
lunar_assembler_helpful_functions_for_map_styles_generate_symbolic_zebra_bars.js
233 lines (218 loc) · 10.5 KB
/
lunar_assembler_helpful_functions_for_map_styles_generate_symbolic_zebra_bars.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
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
/*
lunar_assembler - tool for generating SVG files from OpenStreetMap data. Available as a website.
Copyright (C) 2021 Mateusz Konieczny
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, under version 3 of the
License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
function generateZebraBarCrossings(dataGeojson, roadAreaWithCrossing) {
if (roadAreaWithCrossing == undefined) {
showWarning("zebra crossing bars not generated as undefined was passed as crossing geometry - please report bug if crossing is mapped here");
}
if (roadAreaWithCrossing.type != "Feature") {
showFatalError("roadAreaWithCrossing is of wrong type: " + roadAreaWithCrossing.type + " " + reportBugMessage());
}
crossingLines = listUnifiedCrossingLines(dataGeojson);
var i = crossingLines.length;
while (i--) {
var feature = crossingLines[i];
// startEndOfActualCrossing is necessary as sometimes footway=crossing is applied between sidewalks, including segment outside road area
// also, this allows to catch unsupported cases (one footway=crossing across independent crossings or split footway=crossing line)
// and invalid OpenStreetMap data (like footway=crossing shorter than actual crossing or footway=crossing outside crossings)
showCrossingData(feature, roadAreaWithCrossing);
var startEndOfActualCrossing = turf.lineIntersect(roadAreaWithCrossing, feature);
if (startEndOfActualCrossing.features.length < 2 || startEndOfActualCrossing.features.length % 2 == 1) {
complainAboutCrossingLineMismatchingCrossingArea(startEndOfActualCrossing, roadAreaWithCrossing, feature);
}
if (startEndOfActualCrossing.features.length < 2) {
// skipping, generation impossible
continue;
}
console.log("startEndOfActualCrossing.features.length");
console.log(startEndOfActualCrossing.features.length);
var k = startEndOfActualCrossing.features.length - 1;
while (k >= 0) {
console.log(k);
dataGeojson = generateGroupOfZebraBars(
startEndOfActualCrossing.features[k - 1].geometry.coordinates,
startEndOfActualCrossing.features[k].geometry.coordinates,
roadAreaWithCrossing,
dataGeojson
);
k -= 2;
}
}
return dataGeojson;
}
function generateGroupOfZebraBars(crossingLinePoint1, crossingLinePoint2, roadAreaWithCrossing, dataGeojson) {
// always three strips, change later if needed
// so
// 1st empty space
// 1st strip
// 2nd empty space
// 2nd strip
// 3rd empty space
// 3rd strip
// 4th empty space
//
// so we need to split distance in 7
var point1 = crossingLinePoint1;
var point2 = crossingLinePoint2;
dataGeojson.features.push(makeBarOfZebraCrossing(roadAreaWithCrossing, point1, point2, 1 / 7, 2 / 7));
dataGeojson.features.push(makeBarOfZebraCrossing(roadAreaWithCrossing, point1, point2, 3 / 7, 4 / 7));
dataGeojson.features.push(makeBarOfZebraCrossing(roadAreaWithCrossing, point1, point2, 5 / 7, 6 / 7));
return dataGeojson;
}
function showCrossingData(feature, roadAreaWithCrossing) {
console.log();
console.log("-0--0--0--0--0--0--0--0-");
console.log("crossing line:");
console.log(feature);
console.log("crossing areas:");
console.log(roadAreaWithCrossing);
}
function complainAboutCrossingLineMismatchingCrossingArea(startEndOfActualCrossing, roadAreaWithCrossing, feature) {
const link = "https://www.openstreetmap.org/" + feature.id;
showFatalError(
link +
" and touching crossing ways is/are unexpectedly crossing with road area not exactly two times (or even even count) but " +
startEndOfActualCrossing.features.length +
" times, which is unhandled.\n\n" +
"Please, check is footway=crossing at correct object. If area:highway=crossing area exists - is end of crossing line attached to it?\n" +
"Road area:\n" +
JSON.stringify(roadAreaWithCrossing) +
"\n\n" +
"crossingLine:\n" +
JSON.stringify(feature) +
"\n\n" +
reportBugMessageButGeodataMayBeWrong()
);
}
function listUnifiedCrossingLines(dataGeojson) {
var i = dataGeojson.features.length;
var crossingLines = [];
while (i--) {
var feature = dataGeojson.features[i];
if (feature.properties["footway"] == "crossing" && feature.properties["area:highway_generated_automatically"] != "yes") {
crossingLines.push(JSON.parse(JSON.stringify(feature)));
}
}
var oldCount = undefined;
while (oldCount != crossingLines.length) {
// lets imagine case
// aaaa x cccc x bbbb
// aaaa joins cccc
// cccc joins bbbb
// aaaa is not merged with bbbb
// aaaa is merged with cccc
// end of a single run is
// aaaaaaaaa x bbbbb
//
// so either smarter algorithm or rerunning is needed
oldCount = crossingLines.length;
crossingLines = unifyCrossings(crossingLines);
}
return crossingLines;
}
function unifyCrossings(crossingLines) {
// TODO - merge split crossings to prevent warnings above
var i = -1;
while (i + 1 < crossingLines.length) {
i++;
var k = i;
while (k + 1 < crossingLines.length) {
k++;
var feature = crossingLines[i];
var possiblyMatching = crossingLines[k];
const link1 = "https://www.openstreetmap.org/" + feature.id;
const link2 = "https://www.openstreetmap.org/" + possiblyMatching.id;
var coordsOfCandidate = possiblyMatching.geometry.coordinates;
if (isTheSameJSON(feature.geometry.coordinates[0], coordsOfCandidate[0])) {
feature.geometry.coordinates.reverse();
coordsOfCandidate.shift();
feature.geometry.coordinates.push(...coordsOfCandidate);
crossingLines.splice(k, 1); // remove matching element
//showFatalError("merge 0-0 " + link1 + " " + link2 + " " + JSON.stringify(feature))
} else if (isTheSameJSON(feature.geometry.coordinates[0], coordsOfCandidate[coordsOfCandidate.length - 1])) {
feature.geometry.coordinates.reverse();
coordsOfCandidate.reverse();
coordsOfCandidate.shift();
feature.geometry.coordinates.push(...coordsOfCandidate);
crossingLines.splice(k, 1); // remove matching element
//showFatalError("merge 0-last " + link1 + " " + link2 + " " + JSON.stringify(feature))
} else if (isTheSameJSON(feature.geometry.coordinates[feature.geometry.coordinates.length - 1], coordsOfCandidate[coordsOfCandidate.length - 1])) {
coordsOfCandidate.reverse();
coordsOfCandidate.shift();
feature.geometry.coordinates.push(...coordsOfCandidate);
crossingLines.splice(k, 1); // remove matching element
//showFatalError("merge last-last " + link1 + " " + link2 + " " + JSON.stringify(feature))
} else if (isTheSameJSON(feature.geometry.coordinates[feature.geometry.coordinates.length - 1], coordsOfCandidate[0])) {
coordsOfCandidate.shift();
feature.geometry.coordinates.push(...coordsOfCandidate);
crossingLines.splice(k, 1); // remove matching element
//showFatalError("merge last-0 " + link1 + " " + link2 + " " + JSON.stringify(feature))
} else {
/*
showError("================")
showWarning((feature.geometry.coordinates[0] == coordsOfCandidate[0]) )
showError("-------------------")
showWarning(feature.geometry.coordinates[0] == coordsOfCandidate[coordsOfCandidate.length - 1])
showWarning(feature.geometry.coordinates[0][0] == coordsOfCandidate[coordsOfCandidate.length - 1][0])
showWarning(feature.geometry.coordinates[0][1] == coordsOfCandidate[coordsOfCandidate.length - 1][1])
showError("-------------------")
showWarning(feature.geometry.coordinates[feature.geometry.coordinates.length - 1] == coordsOfCandidate[coordsOfCandidate.length - 1])
showError("-------------------")
showWarning(feature.geometry.coordinates[feature.geometry.coordinates.length - 1] == coordsOfCandidate[0])
showError("-------------------")
showError("")
showWarning(link1)
showError(feature.geometry.coordinates[0])
showError(feature.geometry.coordinates[feature.geometry.coordinates.length - 1])
showError("")
showWarning(link2)
showError(coordsOfCandidate[0])
showError(coordsOfCandidate[coordsOfCandidate.length - 1])
showError("================")
*/
}
}
}
return crossingLines;
}
function isTheSameJSON(pointA, pointB) {
return JSON.stringify(pointA) === JSON.stringify(pointB);
}
function makeBarOfZebraCrossing(roadAreaWithCrossing, start, end, fractionOfCrossingForBarStart, fractionOfCrossingForBarEnd) {
var bearingOfCrossing = turf.bearing(start, end);
var lonDiff = end[0] - start[0];
var latDiff = end[1] - start[1];
var startOnCenterline = JSON.parse(JSON.stringify(start));
startOnCenterline[0] += lonDiff * fractionOfCrossingForBarStart;
startOnCenterline[1] += latDiff * fractionOfCrossingForBarStart;
var endOnCenterline = JSON.parse(JSON.stringify(start));
endOnCenterline[0] += lonDiff * fractionOfCrossingForBarEnd;
endOnCenterline[1] += latDiff * fractionOfCrossingForBarEnd;
var distance = 10;
var options = { units: "meters" };
var offset1From = turf.destination(startOnCenterline, distance, bearingOfCrossing + 90, options);
var offset1To = turf.destination(endOnCenterline, distance, bearingOfCrossing + 90, options);
var offset2From = turf.destination(startOnCenterline, distance, bearingOfCrossing - 90, options);
var offset2To = turf.destination(endOnCenterline, distance, bearingOfCrossing - 90, options);
console.log();
console.log("bar");
var geometry_of_bar = {
type: "Polygon",
coordinates: [[offset1From.geometry.coordinates, offset1To.geometry.coordinates, offset2To.geometry.coordinates, offset2From.geometry.coordinates, offset1From.geometry.coordinates]],
};
geometry_of_bar.coordinates = polygonClipping.intersection(geometry_of_bar.coordinates, roadAreaWithCrossing.geometry.coordinates);
geometry_of_bar.type = "MultiPolygon";
console.log(geometry_of_bar);
return { type: "Feature", geometry: geometry_of_bar, properties: { zebra_crossing_bar_generated_by_lunar_assembler: "yes" } };
}