@@ -35,6 +35,16 @@ export const setOrigin = createAction('set origin', (origin) => {
35
35
return origin
36
36
} )
37
37
38
+ export const setDestinationLabel = createAction ( 'set destination label' , ( label ) => {
39
+ setKeyTo ( 'end' , label )
40
+ return label
41
+ } )
42
+
43
+ export const setOriginLabel = createAction ( 'set origin label' , ( label ) => {
44
+ setKeyTo ( 'start' , label )
45
+ return label
46
+ } )
47
+
38
48
export const clearEnd = createAction ( 'clear end' , ( ) => {
39
49
setKeyTo ( 'end' , null )
40
50
} )
@@ -94,14 +104,15 @@ export function updateOrigin ({browsochrones, destinationLatlng, latlng, label,
94
104
)
95
105
} else {
96
106
actions . push (
107
+ setOrigin ( { latlng} ) ,
97
108
addActionLogItem ( `Finding start address for ${ lonlng ( latlng ) . toString ( ) } ` ) ,
98
109
reverseGeocode ( { latlng} )
99
110
. then ( ( { features} ) => {
100
111
if ( ! features || features . length < 1 ) return
101
112
const label = featureToLabel ( features [ 0 ] )
102
113
return [
103
114
addActionLogItem ( `Set start address to: ${ label } ` ) ,
104
- setOrigin ( { label, latlng : lonlng ( features [ 0 ] . geometry . coordinates ) } )
115
+ setOriginLabel ( label )
105
116
]
106
117
} )
107
118
)
@@ -111,24 +122,24 @@ export function updateOrigin ({browsochrones, destinationLatlng, latlng, label,
111
122
112
123
const point = browsochrones . base . pixelToOriginPoint ( Leaflet . CRS . EPSG3857 . latLngToPoint ( latlng , zoom ) , zoom )
113
124
if ( browsochrones . base . pointInQueryBounds ( point ) ) {
114
- actions . push (
115
- fetchBrowsochronesFor ( {
116
- browsochrones : browsochrones . base ,
117
- destinationLatlng ,
118
- latlng ,
119
- name : 'base' ,
120
- timeCutoff ,
121
- zoom
122
- } ) ,
123
- fetchBrowsochronesFor ( {
125
+ actions . push ( fetchBrowsochronesFor ( {
126
+ browsochrones : browsochrones . base ,
127
+ destinationLatlng ,
128
+ latlng ,
129
+ name : 'base' ,
130
+ timeCutoff ,
131
+ zoom
132
+ } ) )
133
+ if ( browsochrones . comparison ) {
134
+ actions . push ( fetchBrowsochronesFor ( {
124
135
browsochrones : browsochrones . comparison ,
125
136
destinationLatlng,
126
137
latlng,
127
138
name : 'comparison' ,
128
139
timeCutoff,
129
140
zoom
130
- } )
131
- )
141
+ } ) )
142
+ }
132
143
} else {
133
144
console . log ( 'point out of bounds' ) // TODO: Handle
134
145
}
@@ -156,7 +167,7 @@ function fetchBrowsochronesFor ({
156
167
157
168
return [
158
169
decrementWork ( ) ,
159
- generateAccessiblityFor ( { browsochrones, name, timeCutoff} ) ,
170
+ generateAccessiblityFor ( { browsochrones, latlng , name, timeCutoff} ) ,
160
171
generateIsochroneFor ( { browsochrones, latlng, name, timeCutoff} ) ,
161
172
destinationLatlng && generateDestinationDataFor ( {
162
173
browsochrones,
@@ -171,14 +182,19 @@ function fetchBrowsochronesFor ({
171
182
]
172
183
}
173
184
174
- function generateAccessiblityFor ( { browsochrones, name, timeCutoff} ) {
185
+ const storedAccessibility = { }
186
+ const storedIsochrones = { }
187
+
188
+ function generateAccessiblityFor ( { browsochrones, latlng, name, timeCutoff} ) {
175
189
return [
176
190
incrementWork ( ) ,
177
191
addActionLogItem ( `Generating accessibility surface for ${ name } ` ) ,
178
192
( async ( ) => {
179
193
const accessibility = { }
180
194
for ( const grid of browsochrones . grids ) {
181
- accessibility [ grid ] = await browsochrones . getAccessibilityForGrid ( grid , timeCutoff )
195
+ const key = `${ name } -${ lonlng . toString ( latlng ) } -${ timeCutoff } -${ grid } `
196
+ accessibility [ grid ] = storedAccessibility [ key ] || await browsochrones . getAccessibilityForGrid ( grid , timeCutoff )
197
+ storedAccessibility [ key ] = accessibility [ grid ]
182
198
}
183
199
return [
184
200
setAccessibilityFor ( { accessibility, name} ) ,
@@ -193,8 +209,10 @@ function generateIsochroneFor ({browsochrones, latlng, name, timeCutoff}) {
193
209
incrementWork ( ) ,
194
210
addActionLogItem ( `Generating travel time isochrone for ${ name } ` ) ,
195
211
( async ( ) => {
196
- const isochrone = await browsochrones . getIsochrone ( timeCutoff )
197
- isochrone . key = `${ name } -${ lonlng . toString ( latlng ) } -${ timeCutoff } `
212
+ const key = `${ name } -${ lonlng . toString ( latlng ) } -${ timeCutoff } `
213
+ const isochrone = storedIsochrones [ key ] || await browsochrones . getIsochrone ( timeCutoff )
214
+ isochrone . key = key
215
+ storedIsochrones [ key ] = isochrone
198
216
199
217
return [
200
218
setIsochroneFor ( { isochrone, name} ) ,
@@ -212,7 +230,10 @@ function generateDestinationDataFor ({browsochrones, fromLatlng, toLatlng, name,
212
230
const destinationPoint = browsochrones . pixelToOriginPoint ( Leaflet . CRS . EPSG3857 . latLngToPoint ( toLatlng , zoom ) , zoom )
213
231
const data = await browsochrones . generateDestinationData ( {
214
232
from : fromLatlng || null ,
215
- to : destinationPoint
233
+ to : {
234
+ ...toLatlng ,
235
+ ...destinationPoint
236
+ }
216
237
} )
217
238
data . transitive . key = `${ name } -${ lonlng . toString ( toLatlng ) } `
218
239
return [
@@ -230,9 +251,12 @@ export function updateSelectedTimeCutoff ({browsochrones, latlng, timeCutoff}) {
230
251
231
252
if ( browsochrones . base && browsochrones . base . isLoaded ( ) ) {
232
253
actions . push ( generateIsochroneFor ( { browsochrones : browsochrones . base , latlng, name : 'base' , timeCutoff} ) )
254
+ actions . push ( generateAccessiblityFor ( { browsochrones : browsochrones . base , latlng, name : 'base' , timeCutoff} ) )
255
+ }
256
+
257
+ if ( browsochrones . comparison && browsochrones . comparison . isLoaded ( ) ) {
233
258
actions . push ( generateIsochroneFor ( { browsochrones : browsochrones . comparison , latlng, name : 'comparison' , timeCutoff} ) )
234
- actions . push ( generateAccessiblityFor ( { browsochrones : browsochrones . base , name : 'base' , timeCutoff} ) )
235
- actions . push ( generateAccessiblityFor ( { browsochrones : browsochrones . comparison , name : 'comparison' , timeCutoff} ) )
259
+ actions . push ( generateAccessiblityFor ( { browsochrones : browsochrones . comparison , latlng, name : 'comparison' , timeCutoff} ) )
236
260
}
237
261
238
262
return actions
@@ -266,13 +290,17 @@ export function updateDestination ({
266
290
actions . push ( setDestination ( { label, latlng} ) )
267
291
} else {
268
292
actions . push (
293
+ setDestination ( { latlng} ) ,
269
294
reverseGeocode ( { latlng} )
270
- . then ( ( { features} ) => setDestination ( { label : featureToLabel ( features [ 0 ] ) , latlng : lonlng ( features [ 0 ] . geometry . coordinates ) } ) )
295
+ . then ( ( { features} ) => setDestinationLabel ( featureToLabel ( features [ 0 ] ) ) )
271
296
)
272
297
}
273
298
274
299
if ( browsochrones . base && browsochrones . base . isLoaded ( ) ) {
275
300
actions . push ( generateDestinationDataFor ( { browsochrones : browsochrones . base , fromLatlng, toLatlng : latlng , name : 'base' , zoom} ) )
301
+ }
302
+
303
+ if ( browsochrones . comparison && browsochrones . comparison . isLoaded ( ) ) {
276
304
actions . push ( generateDestinationDataFor ( { browsochrones : browsochrones . comparison , fromLatlng, toLatlng : latlng , name : 'comparison' , zoom} ) )
277
305
}
278
306
0 commit comments