-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus_lines.js
204 lines (138 loc) · 4.34 KB
/
bus_lines.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
// Global variables
var vectorBusLinesGroup;
var strokeStyleBusLine;
var circleStyleBusLine;
// Functions
function initBusLinesLayers() {
strokeStyleBusLine = new ol.style.Stroke({
color: [238, 28, 37, 1.0],
width: 15.0
})
circleStyleBusLine = new ol.style.Circle({
fill: new ol.style.Fill({
color: [238, 28, 37, 1.0]
}),
radius: 7,
stroke: strokeStyleBusLine
})
// Layer group
vectorBusLinesGroup = new ol.layer.Group({
minZoom: 14,
layers: [
// vectorBusLine
]
})
map.addLayer(vectorBusLinesGroup);
}
var customStyleFunction = function(feature, resolution) {
if (feature.get('role') === 'platform') {
imageValue = new ol.style.Icon({
size: [64, 64],
opacity: 1,
scale: 0.40,
src: '../img/bus_stop.png'
});
}
else {
imageValue = circleStyleBusLine;
}
return [new ol.style.Style({
stroke: strokeStyleBusLine,
image: imageValue
})];
}
function getVisibleBusLines(zoom) {
// Get map view extends
var viewExtends = mapView.calculateExtent();
var pos1 = [viewExtends[0], viewExtends[1]];
var pos2 = [viewExtends[2], viewExtends[3]];
var coord1 = ol.proj.transform(pos1, 'EPSG:3857', 'EPSG:4326');
var coord2 = ol.proj.transform(pos2, 'EPSG:3857', 'EPSG:4326');
//console.log(coord1);
//console.log(coord2);
var minLon = coord1[0];
var maxLon = coord2[0];
var minLat = coord1[1];
var maxLat = coord2[1];
// Create fetch url
var url = "/fetchBusLines.php?" +
"minLon=" + minLon + "&" +
"maxLon=" + maxLon + "&" +
"minLat=" + minLat + "&" +
"maxLat=" + maxLat;
//console.log(url);
fetch(url)
.then(response => {
if (response.ok) {
//console.log("Fetch response Ok!!");
return response.text();
}
else {
console.log("Fetch response NOT Ok!!");
return "NOT Ok!!";
}
})
.then(text => {
console.log("Fetch received text: " + text);
// Create collection
var fetchedBusLines;
if (text.startsWith("BUS_LINES")) {
var start = text.indexOf('<');
var end = text.indexOf('>');
console.log("Fetched Bus Lines: " + text.substring(start+1, end));
var lines = text.substring(start+1, end).split(",");
// Create collection with fetched bus lines
fetchedBusLines = new ol.Collection(lines);
}
else {
// Create empty collection
fetchedBusLines = new ol.Collection();
}
showBusLines(fetchedBusLines);
});
}
function showBusLines(fetchedBusLines) {
var busLinesCollection = vectorBusLinesGroup.getLayers();
console.log("Initial bus lines count: " + busLinesCollection.getLength());
busLinesCollection.forEach(currentBusLine => {
var busId = currentBusLine.get('title');
console.log("Checking existing bus line: " + busId);
var found = false;
// Check if bus line is already loaded...
for (let i = 0; i < fetchedBusLines.getLength(); i++) {
var fetchedLine = fetchedBusLines.item(i);
if (fetchedLine.localeCompare(busId) == 0) {
// Bus line found in fetched lines
console.log("Bus line #" + busId + " found in fetched lines");
// Remove bus line from fetched lines
fetchedBusLines.removeAt(i);
found = true;
break;
}
}
if (!found) {
// The existing bus line has not been fetched
// Delete from shown bus lines...
busLinesCollection.remove(currentBusLine);
console.log("Removed bus line #" + busId + " from shown lines");
}
})
console.log("Final fetched bus lines count: " + fetchedBusLines.getLength());
// Add final fetched bus lines
fetchedBusLines.forEach(function(fetchedLine) {
var busFileName = "./data/bus_lines/bus_line_" + fetchedLine + ".geojson";
var vectorSourceBusLine = new ol.source.Vector({
url: busFileName,
format: new ol.format.GeoJSON()
})
var vectorBusLine = new ol.layer.VectorImage({
source: vectorSourceBusLine,
visible: true,
title: fetchedLine,
style: customStyleFunction
})
busLinesCollection.push(vectorBusLine);
})
console.log("Final bus lines count: " + busLinesCollection.getLength());
console.log("--------------------------------------------------------");
}