Skip to content

Commit 84abb63

Browse files
committed
fix(editor-map): handle new gtfs-lib response for encoded polylines
fix #627
1 parent 725e897 commit 84abb63

File tree

5 files changed

+54
-29
lines changed

5 files changed

+54
-29
lines changed

lib/editor/actions/tripPattern.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {ActionCreators} from 'redux-undo'
55
import {toast} from 'react-toastify'
66

77
import {resetActiveGtfsEntity, savedGtfsEntity, updateActiveGtfsEntity, updateEditSetting} from './active'
8-
import {createVoidPayloadAction, secureFetch} from '../../common/actions'
8+
import {createVoidPayloadAction, fetchGraphQL, secureFetch} from '../../common/actions'
99
import {snakeCaseKeys} from '../../common/util/map-keys'
1010
import {generateUID} from '../../common/util/util'
11-
import {fetchGTFSEntities} from '../../manager/actions/versions'
11+
import {fetchGTFSEntities, receiveGTFSEntities} from '../../manager/actions/versions'
1212
import {fetchTripCounts} from './trip'
1313
import {getEditorNamespace} from '../util/gtfs'
1414
import {resequenceStops, resequenceShapePoints} from '../util/map'
@@ -17,6 +17,7 @@ import {entityIsNew} from '../util/objects'
1717
import type {ControlPoint, Pattern, PatternStop} from '../../types'
1818
import type {dispatchFn, getStateFn} from '../../types/reducers'
1919

20+
const fetchingTripPatterns = createVoidPayloadAction('FETCHING_TRIP_PATTERNS')
2021
const savedTripPattern = createVoidPayloadAction('SAVED_TRIP_PATTERN')
2122
export const setActivePatternSegment = createAction(
2223
'SET_ACTIVE_PATTERN_SEGMENT',
@@ -118,8 +119,25 @@ export function togglePatternEditing () {
118119
*/
119120
export function fetchTripPatterns (feedId: string) {
120121
return function (dispatch: dispatchFn, getState: getStateFn) {
122+
dispatch(fetchingTripPatterns())
121123
const namespace = getEditorNamespace(feedId, getState())
122-
return dispatch(fetchGTFSEntities({namespace, type: 'pattern', editor: true}))
124+
const query = `
125+
query shapesQuery ($namespace: String) {
126+
feed(namespace: $namespace) {
127+
feed_version
128+
feed_info {
129+
feed_id
130+
}
131+
shapes (limit: -1) {
132+
shape_id
133+
polyline
134+
}
135+
}
136+
}
137+
`
138+
const variables = {namespace}
139+
return dispatch(fetchGraphQL({query, variables}))
140+
.then(data => dispatch(receiveGTFSEntities({namespace, component: 'pattern', data, editor: true, replaceNew: true})))
123141
}
124142
}
125143

lib/editor/components/map/EditorMapLayersControl.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// @flow
22

3-
import React, {Component} from 'react'
4-
import {Browser} from 'leaflet'
3+
import React, {PureComponent} from 'react'
4+
import L from 'leaflet'
55
import {
66
TileLayer,
77
FeatureGroup,
88
LayersControl,
9-
Polyline,
10-
Tooltip
9+
Polyline
1110
} from 'react-leaflet'
1211

1312
import {getUserMetadataProperty} from '../../../common/util/user'
@@ -26,8 +25,9 @@ type Props = {
2625

2726
type OverlayItem = {component: any, name: string}
2827

29-
export default class EditorMapLayersControl extends Component<Props> {
28+
export default class EditorMapLayersControl extends PureComponent<Props> {
3029
render () {
30+
const canvas = L.canvas()
3131
const { tripPatterns, stops, user } = this.props
3232
const { BaseLayer, Overlay } = LayersControl
3333
const OVERLAYS: Array<OverlayItem> = [
@@ -43,11 +43,8 @@ export default class EditorMapLayersControl extends Component<Props> {
4343
key={tp.id}
4444
positions={tp.latLngs}
4545
weight={2}
46-
color='#888'>
47-
<Tooltip sticky>
48-
<span>{tp.name} ({tp.route_id})</span>
49-
</Tooltip>
50-
</Polyline>
46+
renderer={canvas}
47+
color='#888' />
5148
)
5249
})
5350
: null
@@ -78,7 +75,7 @@ export default class EditorMapLayersControl extends Component<Props> {
7875
name='Route layer'
7976
key='route-layer'>
8077
<TileLayer
81-
url={`https://s3.amazonaws.com/datatools-nysdot/tiles/{z}_{x}_{y}${Browser.retina ? '@2x' : ''}.png`} />
78+
url={`https://s3.amazonaws.com/datatools-nysdot/tiles/{z}_{x}_{y}${L.Browser.retina ? '@2x' : ''}.png`} />
8279
</Overlay>
8380
}
8481
{OVERLAYS.map((overlay: OverlayItem, i: number) => (

lib/editor/containers/ActiveGtfsEditor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const mapStateToProps = (state: AppState, ownProps: Props) => {
6565
const {activeEntityId, subEntityId} = getIdsFromParams(ownProps.routeParams)
6666
const {data, editSettings: editSettingsState, mapState} = state.editor
6767
const {present: editSettings} = editSettingsState
68-
const {active, tables, status} = data
68+
const {active, tables, tripPatterns, status} = data
6969
// FIXME: entityId is now a non-string line number and somewhere the number is
7070
// being cast to a string.
7171
const activeEntity = active.entity && active.entity.id === activeEntityId
@@ -130,6 +130,7 @@ const mapStateToProps = (state: AppState, ownProps: Props) => {
130130
tableData: tables,
131131
tableView,
132132
timetableStatus,
133+
tripPatterns,
133134
user,
134135
validationErrors
135136
}

lib/editor/reducers/data.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import update from 'react-addons-update'
44
import clone from 'lodash/cloneDeep'
5+
import {decode as decodePolyline} from 'polyline'
56
import SortDirection from 'react-virtualized/dist/commonjs/Table/SortDirection'
67

78
import {ENTITY} from '../constants'
@@ -22,6 +23,11 @@ const getUpdateEntityKeys = component => component === 'trippattern'
2223
export const defaultState = {
2324
active: {},
2425
lock: {},
26+
sort: {
27+
key: 'name',
28+
direction: SortDirection.ASC
29+
},
30+
status: {},
2531
tables: {
2632
agency: [],
2733
calendar: [],
@@ -36,11 +42,7 @@ export const defaultState = {
3642
service_id: []
3743
}
3844
},
39-
sort: {
40-
key: 'name',
41-
direction: SortDirection.ASC
42-
},
43-
status: {}
45+
tripPatterns: null
4446
}
4547

4648
/* eslint-disable complexity */
@@ -51,6 +53,10 @@ const data = (state: DataState = defaultState, action: Action): DataState => {
5153
return update(state, {active: {$merge: {feedSourceId}}})
5254
case 'RECEIVE_BASE_GTFS': {
5355
const feed = clone(action.payload.feed)
56+
if (!feed) {
57+
console.warn('Could not fetch base GTFS!')
58+
return state
59+
}
5460
if (feed.feed_info.length === 0) {
5561
console.warn(`No feed info found. Adding feed info with null values.`)
5662
feed.feed_info.push(generateNullProps('feedinfo'))
@@ -223,6 +229,11 @@ const data = (state: DataState = defaultState, action: Action): DataState => {
223229
if (!tableName) return state
224230
return update(state, {tables: {[tableName]: {$push: [entity]}}})
225231
}
232+
case 'FETCHING_TRIP_PATTERNS': {
233+
// Set value to an empty array to prevent the user from accidentally
234+
// triggering multiple fetches.
235+
return update(state, {tripPatterns: {$set: []}})
236+
}
226237
case 'RECEIVE_GTFS_ENTITIES': {
227238
let activePattern
228239
const {data, editor, component} = action.payload
@@ -235,15 +246,13 @@ const data = (state: DataState = defaultState, action: Action): DataState => {
235246
// Handle trip patterns to be drawn as overlay layer in editor
236247
return update(state, {
237248
tripPatterns: {$set:
238-
data.feed.patterns.map(pattern => {
249+
data.feed.shapes.map(shape => {
239250
return {
240-
id: pattern.id,
241-
name: pattern.name,
242-
route_id: pattern.route_id,
243-
latLngs: pattern.shape_points
244-
? (pattern.shape_points.map(sp =>
245-
({lon: sp.shape_pt_lon, lat: sp.shape_pt_lat})))
246-
: null
251+
id: shape.shape_id,
252+
// Decode polyline and coords divide by ten (gtfs-lib
253+
// simplification level requires this).
254+
latLngs: decodePolyline(shape.polyline)
255+
.map(coords => ([coords[0] / 10, coords[1] / 10]))
247256
}
248257
})
249258
}

lib/manager/actions/versions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const receiveFeedVersions = createAction(
4949
versions: Array<FeedVersion>
5050
}) => payload
5151
)
52-
const receiveGTFSEntities = createAction(
52+
export const receiveGTFSEntities = createAction(
5353
'RECEIVE_GTFS_ENTITIES',
5454
(payload: {
5555
component: string,

0 commit comments

Comments
 (0)