1
1
import clone from 'lodash.clonedeep'
2
- import oneLine from 'common-tags/lib/oneLine'
2
+ // import oneLine from 'common-tags/lib/oneLine'
3
3
import { List } from 'immutable'
4
4
import randomColor from 'randomcolor'
5
5
import SortDirection from 'react-virtualized/dist/commonjs/Table/SortDirection'
@@ -107,7 +107,7 @@ export const getControlPoints = createSelector(
107
107
return { patternSegments, controlPoints}
108
108
}
109
109
if ( previousControlPoints && previousControlPoints . length && previousPatternSegments && previousPatternSegments . length ) {
110
- console . log ( 'defaulting to state\'s control points/patternSegments' , previousControlPoints , previousPatternSegments )
110
+ // console.log('defaulting to state\'s control points/patternSegments', previousControlPoints, previousPatternSegments)
111
111
return {
112
112
controlPoints : previousControlPoints ,
113
113
patternSegments : previousPatternSegments
@@ -124,7 +124,7 @@ export const getControlPoints = createSelector(
124
124
// stop, rather than assuming they're all missing if the first one is.
125
125
const projectStops = patternStops [ 0 ] . shapeDistTraveled === null &&
126
126
shapePoints . filter ( sp => sp . pointType === POINT_TYPE . STOP ) < patternStops . length
127
- console . log ( 'projecting stops, shape dist traveled:' , patternStops [ 0 ] . shapeDistTraveled )
127
+ // console.log('projecting stops, shape dist traveled:', patternStops[0].shapeDistTraveled)
128
128
// if distance values are null, calculate distance values.
129
129
// let newShapePoints = shapePoints
130
130
// if (shapePoints.filter(sp => sp.pointType === POINT_TYPE.STOP) < patternStops.length) {
@@ -143,7 +143,7 @@ export const getControlPoints = createSelector(
143
143
}
144
144
patternSegments = result . patternSegments
145
145
controlPoints = getControlPointsFromShapePoints ( result . shapePoints )
146
- console . log ( controlPoints , patternSegments )
146
+ // console.log(controlPoints, patternSegments)
147
147
if ( controlPoints . length - 1 > patternSegments . length ) {
148
148
// There are non-stop control points that need to be added in.
149
149
for ( let i = 0 ; i < controlPoints . length ; i ++ ) {
@@ -152,20 +152,21 @@ export const getControlPoints = createSelector(
152
152
// If the following control point is a user-defined anchor, slice
153
153
// current line at the following control point and splice into
154
154
// array of segments.
155
- console . log ( `control point ${ i } ; pattern segment ${ i - 1 } ` , nextControlPoint , patternSegments [ i ] )
155
+ // console.log(`control point ${i}; pattern segment ${i - 1}`, nextControlPoint, patternSegments[i])
156
156
const { beforeSlice, afterSlice} = getLineSlices ( lineString ( patternSegments [ i ] ) , nextControlPoint . point )
157
157
patternSegments . splice ( i , 1 , beforeSlice . geometry . coordinates , afterSlice . geometry . coordinates )
158
158
}
159
159
}
160
160
}
161
- console . log ( featurecollection ( [
161
+ featurecollection ( [
162
162
...patternSegments . map ( ps => {
163
163
const lineSegment = lineString ( ps )
164
164
lineSegment . properties . stroke = randomColor ( )
165
165
return lineSegment
166
166
} ) ,
167
167
...controlPoints . map ( cp => cp . point )
168
- ] ) )
168
+ ] )
169
+ // console.log()
169
170
} else {
170
171
// No pattern stops, return empty array (below).
171
172
// FIXME: Return control points from shape points instead?
@@ -208,7 +209,7 @@ export const getControlPoints = createSelector(
208
209
}
209
210
// console.log('control points', controlPoints)
210
211
const sortedControlPoints = controlPoints . sort ( ( a , b ) => a . distance - b . distance )
211
- console . log ( 'sorted control points' , sortedControlPoints )
212
+ // console.log('sorted control points', sortedControlPoints)
212
213
return {
213
214
controlPoints : sortedControlPoints ,
214
215
patternSegments
@@ -226,12 +227,12 @@ export const getPatternCoordinates = createSelector(
226
227
return null
227
228
}
228
229
if ( patternSegments ) {
229
- console . log ( 'using coordinates from history' , patternSegments )
230
+ // console.log('using coordinates from history', patternSegments)
230
231
return patternSegments
231
232
}
232
- console . log ( 'using shapepoints' , shapePoints )
233
+ // console.log('using shapepoints', shapePoints)
233
234
// if (shapePoints.filter(sp => sp.pointType).length === 0) {
234
- // console.log('shape points do not contain control points. Merging.')
235
+ // // console.log('shape points do not contain control points. Merging.')
235
236
// controlPoints.forEach((cp, index) => {
236
237
// const itemsToReplace = index < 0 && index < controlPoints.length - 1
237
238
// ? 0
@@ -268,13 +269,14 @@ export const getPatternCoordinates = createSelector(
268
269
}
269
270
splitAndAddSegment ( shapePoints , segments , fromIndex , toIndex )
270
271
}
271
- let sum = 0
272
- segments . forEach ( e => { sum += e . length } )
273
- console . log ( oneLine `
274
- points: ${ sum } ; shapepoints: ${ shapePoints . length } ;
275
- cp: ${ controlPoints . length } ; seg: ${ segments . length }
276
- ${ controlPoints . length === segments . length - 1 ? '✅' : '❌' }
277
- ` , featurecollection ( segments . map ( seg => lineString ( seg ) ) ) )
272
+ // let sum = 0
273
+ // segments.forEach(e => { sum += e.length })
274
+ // const qcString = oneLine`
275
+ // points: ${sum}; shapepoints: ${shapePoints.length};
276
+ // cp: ${controlPoints.length}; seg: ${segments.length}
277
+ // ${controlPoints.length === segments.length - 1 ? '✅' : '❌'}
278
+ // `
279
+ // console.log(qcString, featurecollection(segments.map(seg => lineString(seg)))
278
280
return segments
279
281
}
280
282
)
@@ -285,7 +287,7 @@ function splitAndAddSegment (shapePoints, segments, fromIndex, toIndex) {
285
287
} else if ( ( toIndex - fromIndex < 2 ) || ( fromIndex === shapePoints . length - 1 ) ) {
286
288
console . warn ( `Should not slice shapepoints for segment with less than two coordinates (from ${ fromIndex } to ${ toIndex } ).` )
287
289
} else {
288
- console . log ( `slicing ${ shapePoints . length } items from ${ fromIndex } to ${ toIndex } ` )
290
+ // console.log(`slicing ${shapePoints.length} items from ${fromIndex} to ${toIndex}`)
289
291
}
290
292
const nextSegment = shapePoints
291
293
. slice ( fromIndex , toIndex )
@@ -334,17 +336,17 @@ function addPatternStopsToShapePoints (oldShapePoints, patternStops, stops, proj
334
336
const patternSegments = [ ]
335
337
const computedShapePoints = [ ]
336
338
// Keep only those shape points that are associated with stops
337
- // (this is an empty array if pattern shape is unedited ).
339
+ // (this is an empty array if pattern shape has never been edited ).
338
340
const stopControlPoints = shapePointsCopy . filter ( sp => sp . pointType === POINT_TYPE . STOP )
339
341
const patternLine = lineString ( coordinatesFromShapePoints ( shapePointsCopy ) )
340
342
const patternLength = lineDistance ( patternLine , 'meters' )
341
343
const beginPoint = point ( patternLine . geometry . coordinates [ 0 ] )
342
344
const endPointIndex = patternLine . geometry . coordinates . length - 1
343
345
const endPoint = point ( patternLine . geometry . coordinates [ endPointIndex ] )
344
346
if ( projectStops ) {
345
- console . log ( 'Projecting pattern stops onto shape (distances are null)' , shapePointsCopy , stopControlPoints )
347
+ // console.log('Projecting pattern stops onto shape (distances are null)', shapePointsCopy, stopControlPoints)
346
348
} else {
347
- console . log ( 'Generating control points from pattern stops (using pre-existing stop distances)' , shapePointsCopy , stopControlPoints )
349
+ // console.log('Generating control points from pattern stops (using pre-existing stop distances)', shapePointsCopy, stopControlPoints)
348
350
}
349
351
let previousDistance = 0
350
352
let remainingLine = patternLine
@@ -363,14 +365,14 @@ function addPatternStopsToShapePoints (oldShapePoints, patternStops, stops, proj
363
365
if ( i === 0 ) {
364
366
// First stop must always be placed at first latlng coordinate
365
367
insertPoint = beginPoint
366
- console . log ( 'first stop placed at begin point' )
368
+ // console.log('first stop placed at begin point')
367
369
insertIndex = 0
368
370
distance = 0
369
371
itemsToDelete = 1
370
372
} else if ( i === patternStops . length - 1 ) {
371
373
// Last stop must always be placed at last latlng coordinate
372
374
insertPoint = endPoint
373
- console . log ( 'last stop placed at end point' )
375
+ // console.log('last stop placed at end point')
374
376
insertIndex = endPointIndex
375
377
distance = patternLength
376
378
itemsToDelete = 1
@@ -393,13 +395,13 @@ function addPatternStopsToShapePoints (oldShapePoints, patternStops, stops, proj
393
395
if ( i < stopControlPoints . length && stopControlPoints [ i ] && stopControlPoints [ i ] . shapeDistTraveled !== null ) {
394
396
// Check shape points for existence of control points. If index is less
395
397
// than number of stop control points, skip.
396
- console . log ( `skipping index ${ i } (control point should exist for stop)` )
398
+ // console.log(`skipping index ${i} (control point should exist for stop)`)
397
399
stopControlPoints [ i ] . stopId = stopId
398
400
distance = stopControlPoints [ i ] . shapeDistTraveled
399
401
if ( i > 0 ) {
400
402
const sliceDistance = ( distance - previousDistance ) / 1000
401
403
let lineSegment
402
- console . log ( remainingLine , sliceDistance , stopControlPoints [ i ] )
404
+ // console.log(remainingLine, sliceDistance, stopControlPoints[i])
403
405
if ( sliceDistance <= 0 ) {
404
406
// If there is no more line segment left, extend line to stop control
405
407
// point and TODO signal warning to user?
@@ -417,7 +419,7 @@ function addPatternStopsToShapePoints (oldShapePoints, patternStops, stops, proj
417
419
}
418
420
continue
419
421
}
420
- console . log ( `generating shape point for stop #${ i } at distance: ${ patternStop . shapeDistTraveled } ` , patternStop , remainingLine )
422
+ // console.log(`generating shape point for stop #${i} at distance: ${patternStop.shapeDistTraveled}`, patternStop, remainingLine)
421
423
if ( i === 0 && patternStop . shapeDistTraveled !== 0 ) {
422
424
console . warn ( `Distance for first stop is not zero. Coercing to zero.` )
423
425
distance = 0
@@ -436,28 +438,32 @@ function addPatternStopsToShapePoints (oldShapePoints, patternStops, stops, proj
436
438
// FIXME: what if this happens not on the first pattern stop
437
439
if ( i !== 0 ) console . warn ( `shape dist traveled for pattern stop ${ i } equals zero. this should not be the case!` )
438
440
insertPoint = pointAtFirstCoordinate ( remainingLine )
441
+ insertIndex = 0
439
442
distance = 0
440
- itemsToDelete = 0
443
+ itemsToDelete = 1
441
444
} else {
442
445
// This should cover all cases except the first stop.
443
446
const remainingLineDistance = lineDistance ( remainingLine , 'meters' )
444
447
// FIXME this is breaking stuff when the remaining line has only one coordinate!!!
445
- console . log ( `remaining line dist = ${ remainingLineDistance } ` , remainingLine , patternStop . shapeDistTraveled / 1000 )
448
+ // console.log(`remaining line dist = ${remainingLineDistance}`, remainingLine, patternStop.shapeDistTraveled / 1000)
446
449
if ( patternStop . shapeDistTraveled === remainingLineDistance ) {
447
450
// This should only be the case with the last stop
448
- console . log ( `pattern stop ${ i } /${ patternStops . length } distance is equal to remaining lint distance, setting to end of shape` )
451
+ // console.log(`pattern stop ${i}/${patternStops.length} distance is equal to remaining lint distance, setting to end of shape`)
449
452
// FIXME: what if this happens not on the last pattern stop
450
453
insertPoint = pointAtLastCoordinate ( remainingLine )
454
+ insertIndex = shapePointsCopy . length - 1
451
455
distance = remainingLineDistance
452
- itemsToDelete = 0
456
+ itemsToDelete = 1
453
457
} else {
454
458
// This should be the case for all but first and last stops
455
- const lineSegment = lineSliceAlong ( remainingLine , 0 , patternStop . shapeDistTraveled / 1000 )
456
- console . log ( `pattern stop ${ i } /${ patternStops . length } remaining segment and previous dist` , lineSegment , previousDistance )
457
- distance = previousDistance + lineDistance ( lineSegment , 'meters' )
459
+ distance = patternStop . shapeDistTraveled
460
+ // Use the pattern line to find the lat/lng to use for the generated
461
+ // shape point.
462
+ const lineSegment = lineSliceAlong ( patternLine , 0 , patternStop . shapeDistTraveled / 1000 )
458
463
insertPoint = pointAtLastCoordinate ( lineSegment )
459
- // FIXME: How to determine insert index?
460
- // insertIndex = result.index
464
+ // Insert generated shape point in the correct location according to
465
+ // its distance along the line.
466
+ insertIndex = shapePointsCopy . findIndex ( sp => sp . shapeDistTraveled > distance )
461
467
itemsToDelete = 0
462
468
}
463
469
}
@@ -474,22 +480,33 @@ function addPatternStopsToShapePoints (oldShapePoints, patternStops, stops, proj
474
480
// every shape point below in the re-mapping.
475
481
}
476
482
// Insert closest point into pattern shape
483
+ // console.log(`inserting shape point at ${insertIndex}`, newShapePoint, shapePointsCopy)
477
484
shapePointsCopy . splice ( insertIndex , itemsToDelete , newShapePoint )
478
485
if ( i > 0 ) {
479
486
// Add pattern segment to sliced segments
480
487
let sliceDistance = ( distance - previousDistance ) / 1000
481
488
if ( sliceDistance <= 0 ) {
489
+ console . warn ( `Slice distance is ${ sliceDistance } , setting to 1 meter` )
482
490
sliceDistance = 1 / 1000 // set slice distance to 1 meter if negative or zero
483
491
}
484
- console . log ( `slicing line at ${ sliceDistance } km` , remainingLine )
492
+ // console.log(`slicing line at ${sliceDistance} km`, remainingLine)
485
493
const lineSegment = lineSliceAlong ( remainingLine , 0 , sliceDistance )
486
- console . log ( `slice #${ i } /${ patternStops . length - 1 } ` , lineSegment )
494
+ // console.log(`slice #${i}/${patternStops.length - 1}`, lineSegment)
487
495
remainingLine = lineSliceAlong ( remainingLine , sliceDistance , lineDistance ( remainingLine ) )
488
- console . log ( 'remainingLine' , remainingLine )
496
+ // console.log('remainingLine', remainingLine)
497
+ if ( remainingLine . geometry . coordinates . length < 2 ) {
498
+ throw new Error ( `Remaining line after pattern stop #${ i } /${ patternStops . length - 1 } has fewer than two coordinates` )
499
+ }
489
500
patternSegments . push ( lineSegment . geometry . coordinates )
490
501
previousDistance = distance
491
502
}
492
503
computedShapePoints . push ( newShapePoint )
504
+ // const shapePointFeatures = featurecollection(computedShapePoints.map(sp => {
505
+ // const p = point([sp.shapePtLon, sp.shapePtLat])
506
+ // p.properties = {stopId: sp.stopId}
507
+ // return p
508
+ // }))
509
+ // console.log(shapePointFeatures)
493
510
}
494
511
let previousShapePoint
495
512
for ( let i = 0 ; i < computedShapePoints . length ; i ++ ) {
0 commit comments