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
|
class RoutesMap
constructor: (@target)->
@initMap()
@area = []
@seenStopIds = []
@routes = {}
initMap: ->
@map = new ol.Map
target: @target,
layers: [ new ol.layer.Tile(source: new ol.source.OSM()) ]
controls: [ new ol.control.ScaleLine(), new ol.control.Zoom(), new ol.control.ZoomSlider() ],
interactions: ol.interaction.defaults(zoom: true)
view: new ol.View()
addRoutes: (routes)->
for route in routes
@addRoute route
addRoute: (route)->
geoColPts = []
geoColLns = []
@routes[route.id] = route if route.id
stops = route.stops || route
geoColEdges = [
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([parseFloat(stops[0].longitude), parseFloat(stops[0].latitude)]))
}),
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([parseFloat(stops[stops.length - 1].longitude), parseFloat(stops[stops.length - 1].latitude)]))
})
]
prevStop = null
stops.forEach (stop, i) =>
if stop.longitude && stop.latitude
if prevStop
geoColLns.push new ol.Feature
geometry: new ol.geom.LineString([
ol.proj.fromLonLat([parseFloat(prevStop.longitude), parseFloat(prevStop.latitude)]),
ol.proj.fromLonLat([parseFloat(stop.longitude), parseFloat(stop.latitude)])
])
prevStop = stop
geoColPts.push(new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([parseFloat(stop.longitude), parseFloat(stop.latitude)]))
}))
unless @seenStopIds.indexOf(stop.stoparea_id) > 0
@area.push [parseFloat(stop.longitude), parseFloat(stop.latitude)]
@seenStopIds.push stop.stoparea_id
vectorPtsLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: geoColPts
}),
style: @defaultStyles(),
zIndex: 2
})
route.vectorPtsLayer = vectorPtsLayer if route.id
vectorEdgesLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: geoColEdges
}),
style: @edgeStyles(),
zIndex: 3
})
route.vectorEdgesLayer = vectorEdgesLayer if route.id
vectorLnsLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: geoColLns
}),
style: [@lineStyle()],
zIndex: 1
})
route.vectorLnsLayer = vectorLnsLayer if route.id
@map.addLayer vectorPtsLayer
@map.addLayer vectorEdgesLayer
@map.addLayer vectorLnsLayer
lineStyle: (highlighted=false)->
new ol.style.Style
stroke: new ol.style.Stroke
color: if highlighted then "#ed7f00" else '#007fbb'
width: 3
edgeStyles: (highlighted=false)->
new ol.style.Style
image: new ol.style.Circle
radius: 5
stroke: new ol.style.Stroke
color: if highlighted then "#ed7f00" else '#007fbb'
width: 2
fill: new ol.style.Fill
color: if highlighted then "#ed7f00" else '#007fbb'
width: 2
defaultStyles: (highlighted=false)->
new ol.style.Style
image: new ol.style.Circle
radius: 4
stroke: new ol.style.Stroke
color: if highlighted then "#ed7f00" else '#007fbb'
width: 2
fill: new ol.style.Fill
color: '#ffffff'
width: 2
addRoutesLabels: ->
labelsContainer = $("<ul class='routes-labels'></ul>")
labelsContainer.appendTo $("##{@target}")
@vectorPtsLayer = null
@vectorEdgesLayer = null
@vectorLnsLayer = null
Object.keys(@routes).forEach (id)=>
route = @routes[id]
label = $("<li>#{route.name}</ul>")
label.appendTo labelsContainer
label.mouseleave =>
route.vectorPtsLayer.setStyle @defaultStyles(false)
route.vectorEdgesLayer.setStyle @edgeStyles(false)
route.vectorLnsLayer.setStyle @lineStyle(false)
route.vectorPtsLayer.setZIndex 2
route.vectorEdgesLayer.setZIndex 3
route.vectorLnsLayer.setZIndex 1
@fitZoom()
label.mouseenter =>
route.vectorPtsLayer.setStyle @defaultStyles(true)
route.vectorEdgesLayer.setStyle @edgeStyles(true)
route.vectorLnsLayer.setStyle @lineStyle(true)
route.vectorPtsLayer.setZIndex 11
route.vectorEdgesLayer.setZIndex 12
route.vectorLnsLayer.setZIndex 10
@fitZoom(route)
fitZoom: (route)->
if route
area = []
route.stops.forEach (stop, i) =>
area.push [parseFloat(stop.longitude), parseFloat(stop.latitude)]
else
area = @area
boundaries = ol.extent.applyTransform(
ol.extent.boundingExtent(area), ol.proj.getTransform('EPSG:4326', 'EPSG:3857')
)
@map.getView().fit boundaries, @map.getSize()
tooCloseToBounds = false
mapBoundaries = @map.getView().calculateExtent @map.getSize()
mapWidth = mapBoundaries[2] - mapBoundaries[0]
mapHeight = mapBoundaries[3] - mapBoundaries[1]
marginSize = 0.1
heightMargin = marginSize * mapHeight
widthMargin = marginSize * mapWidth
tooCloseToBounds = tooCloseToBounds || (boundaries[0] - mapBoundaries[0]) < widthMargin
tooCloseToBounds = tooCloseToBounds || (mapBoundaries[2] - boundaries[2]) < widthMargin
tooCloseToBounds = tooCloseToBounds || (boundaries[1] - mapBoundaries[1]) < heightMargin
tooCloseToBounds = tooCloseToBounds || (mapBoundaries[3] - boundaries[3]) < heightMargin
if tooCloseToBounds
@map.getView().setZoom(@map.getView().getZoom() - 1)
export default RoutesMap
|