-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapVisualization.js
More file actions
268 lines (228 loc) · 8.52 KB
/
MapVisualization.js
File metadata and controls
268 lines (228 loc) · 8.52 KB
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
// Global variables (Need to find a better way to handle this)
let gMainView;
// Projection objects
let d3Path;
let simRatio = 40;
function newMapVisualization(centerCoords, // Coords object
boundingBox, // List of two Coords objects [SW, NE]
load_callback = () => 1,
zomm_level = 13,
containerID = '#viz-container',
tileFunction = getTileURL,
tileMode = 'light_all') {
// Append div that will contain the map
d3.select(containerID).append('div').attr('id', 'map');
// Initializing leaflet map object
map = L.map('map', {
zoomControl: false
});
map.on('load', load_callback);
map.setView([centerCoords.lat, centerCoords.lng], zomm_level);
// Adding tiles to the map
var tileURL = tileFunction(tileMode);
actualLayer = L.tileLayer(tileURL, {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>'
});
map.addLayer(actualLayer);
// Setting the bounding box
southWest = L.latLng(boundingBox[0].lat, boundingBox[0].lng),
northEast = L.latLng(boundingBox[1].lat, boundingBox[1].lng);
var bounds = L.latLngBounds(southWest, northEast);
map.setMaxBounds(bounds);
map.on('drag', function () {
map.panInsideBounds(bounds, {
animate: false
});
});
// Overlay SVG
svg = d3.select(map.getPanes().overlayPane).append("svg");
g = svg.append("g").attr("class", "leaflet-zoom-hide");
// Do something with the update callback
gMainView = {
svg: svg,
g: g,
southWest: boundingBox[0],
northEast: boundingBox[1]
}
reset();
map.on('viewreset', reset);
map.on('zoom', reset);
transform = d3.geoTransform({
point: projectPoint
});
d3Path = d3.geoPath().projection(transform);
return {
svg: svg,
g: g,
southWest: boundingBox[0],
northEast: boundingBox[1]
}
}
class Coords {
constructor(lat, lng) {
this.lat = lat;
this.lng = lng;
}
}
// Map related functions
function getTileURL(mode) {
return 'http://{s}.basemaps.cartocdn.com/' + mode + '/{z}/{x}/{y}.png';
}
function projectPoint(x, y) {
var point = map.latLngToLayerPoint(new L.LatLng(y, x));
this.stream.point(point.x, point.y);
}
let drawnPoints = {}
let drawnStreets = {}
function reset() {
// Adjust SVG object to new bounds
var bottomLeft = map.latLngToLayerPoint(gMainView.southWest),
topRight = map.latLngToLayerPoint(gMainView.northEast);
gMainView.svg.attr('width', topRight.x - bottomLeft.x + "px")
.attr('height', bottomLeft.y - topRight.y + "px")
.style("left", bottomLeft.x + "px")
.style("top", topRight.y + "px");
gMainView.g.attr('transform', 'translate(' + -bottomLeft.x + ', ' + -topRight.y + ')');
Object.keys(drawnPoints).forEach((key) => {
drawnPoints[key]
.attr('cx', function (d) {
coor = d.geometry.coordinates
return map.latLngToLayerPoint([coor[1], coor[0]]).x;
}).attr('cy', function (d) {
coor = d.geometry.coordinates
return map.latLngToLayerPoint([coor[1], coor[0]]).y;
});
});
d3.selectAll('path').attr("d", d3Path);
d3.selectAll('circle.school')
.attr('cx', function (d) {
coor = d.geometry.coordinates
return map.latLngToLayerPoint([coor[1], coor[0]]).x;
}).attr('cy', function (d) {
coor = d.geometry.coordinates
return map.latLngToLayerPoint([coor[1], coor[0]]).y;
});
d3.selectAll('circle.student-served, circle.student-unserved')
.attr('cx', function (d) {
coor = d.geometry.coordinates
return map.latLngToLayerPoint([coor[1], coor[0]]).x;
}).attr('cy', function (d) {
coor = d.geometry.coordinates
return map.latLngToLayerPoint([coor[1], coor[0]]).y;
});
Object.keys(drawnStreets).forEach((key) => {
drawnStreets[key]
.attr('d', d3Path);
});
}
// User level drawing functions
function drawStaticPoints({
mapView,
data_path,
name = '',
class_type = '',
r = 3,
attrs = {},
style = {},
callback = () => undefined
} = {}) {
if (class_type === '') {
class_type = 'element' + drawnPoints.length;
}
if (name === '') {
name = 'element' + drawnPoints.length;
}
Promise.all([
d3.json(data_path)
]).then((data) => {
collection = data[0];
points = mapView.g.selectAll('circle.' + class_type)
.data(collection.features)
.enter()
.append('circle')
.attr("r", r)
.attr("class", class_type)
.attr('cx', function (d) {
coor = d.geometry.coordinates
return map.latLngToLayerPoint([coor[1], coor[0]]).x;
}).attr('cy', function (d) {
coor = d.geometry.coordinates
return map.latLngToLayerPoint([coor[1], coor[0]]).y;
});
Object.keys(attrs).forEach((key) => {
points.attr(key, attrs[key]);
});
Object.keys(style).forEach((key) => {
points.style(key, style[key]);
});
drawnPoints[name] = points;
reset(mapView);
})
.then(callback);
}
function getPoints(name) {
return drawnPoints[name];
}
function animatePath({mapView,
path,
id = 'route',
pointClass = 'truck',
edgeClass = 'street-route',
r = 3,
ipath = 0,
assignments = [],
onEndFunction = () => undefined,
transient = false} = {}){
currentPath = path[ipath];
time = $("#timer").html();
//addNewEventLog(title=time + " | Calle", text="ID: " + currentPath.properties.osmid);
var pathObject = mapView.g.append('path')
.datum(currentPath)
.attr("class", edgeClass)
.attr("d", d3Path)
.attr('id', 'path_' + ipath + '_' + id);
//console.log(pathObject.node())
var circle = mapView.g.append("circle")
.attr("class", pointClass)
.attr("r", r)
.attr('id', 'circle_' + id)
.attr("transform", function() {
var p = pathObject.node().getPointAtLength(0);
return "translate(" + p.x + ", " + p.y + ")";
})
.transition()
.ease(d3.easeLinear)
.duration(currentPath.properties.time_secs * 1000 / simRatio)
.attrTween('transform', function() {
return function (t) {
var pathLength = pathObject.node().getTotalLength();
var p = pathObject.node().getPointAtLength(t * pathLength);
pathObject.style("opacity", t);
return "translate(" + p.x + ", " + p.y + ")";
}
})
.on("end", function() {
//pathObject.remove();
if (transient){
pathObject.transition()
.duration(500)
.style('opacity',0)
.on('end', ()=>{
pathObject.remove();
});
}
d3.select(this).remove();
onEndFunction(mapView, path, ipath, r, assignments, edgeClass, pointClass, transient);
})
}
function startTimer() {
var t = d3.timer(function (elapsed) {
var realMili = simRatio * elapsed;
var minutes = Math.floor(realMili / (60000));
var miliseconds = realMili % (60000);
var seconds = Math.floor(miliseconds / 1000);
returnStr = ('0' + minutes).slice(-2) + ":" + ('0' + seconds).slice(-2);
$("#timer").html(returnStr);
}, 100)
return t;
}