Skip to content

Commit

Permalink
fix(editor-map): handle new gtfs-lib response for encoded polylines
Browse files Browse the repository at this point in the history
fix #627
  • Loading branch information
landonreed committed Nov 30, 2020
1 parent 725e897 commit 84abb63
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 29 deletions.
24 changes: 21 additions & 3 deletions lib/editor/actions/tripPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {ActionCreators} from 'redux-undo'
import {toast} from 'react-toastify'

import {resetActiveGtfsEntity, savedGtfsEntity, updateActiveGtfsEntity, updateEditSetting} from './active'
import {createVoidPayloadAction, secureFetch} from '../../common/actions'
import {createVoidPayloadAction, fetchGraphQL, secureFetch} from '../../common/actions'
import {snakeCaseKeys} from '../../common/util/map-keys'
import {generateUID} from '../../common/util/util'
import {fetchGTFSEntities} from '../../manager/actions/versions'
import {fetchGTFSEntities, receiveGTFSEntities} from '../../manager/actions/versions'
import {fetchTripCounts} from './trip'
import {getEditorNamespace} from '../util/gtfs'
import {resequenceStops, resequenceShapePoints} from '../util/map'
Expand All @@ -17,6 +17,7 @@ import {entityIsNew} from '../util/objects'
import type {ControlPoint, Pattern, PatternStop} from '../../types'
import type {dispatchFn, getStateFn} from '../../types/reducers'

const fetchingTripPatterns = createVoidPayloadAction('FETCHING_TRIP_PATTERNS')
const savedTripPattern = createVoidPayloadAction('SAVED_TRIP_PATTERN')
export const setActivePatternSegment = createAction(
'SET_ACTIVE_PATTERN_SEGMENT',
Expand Down Expand Up @@ -118,8 +119,25 @@ export function togglePatternEditing () {
*/
export function fetchTripPatterns (feedId: string) {
return function (dispatch: dispatchFn, getState: getStateFn) {
dispatch(fetchingTripPatterns())
const namespace = getEditorNamespace(feedId, getState())
return dispatch(fetchGTFSEntities({namespace, type: 'pattern', editor: true}))
const query = `
query shapesQuery ($namespace: String) {
feed(namespace: $namespace) {
feed_version
feed_info {
feed_id
}
shapes (limit: -1) {
shape_id
polyline
}
}
}
`
const variables = {namespace}
return dispatch(fetchGraphQL({query, variables}))
.then(data => dispatch(receiveGTFSEntities({namespace, component: 'pattern', data, editor: true, replaceNew: true})))
}
}

Expand Down
19 changes: 8 additions & 11 deletions lib/editor/components/map/EditorMapLayersControl.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// @flow

import React, {Component} from 'react'
import {Browser} from 'leaflet'
import React, {PureComponent} from 'react'
import L from 'leaflet'
import {
TileLayer,
FeatureGroup,
LayersControl,
Polyline,
Tooltip
Polyline
} from 'react-leaflet'

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

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

export default class EditorMapLayersControl extends Component<Props> {
export default class EditorMapLayersControl extends PureComponent<Props> {
render () {
const canvas = L.canvas()
const { tripPatterns, stops, user } = this.props
const { BaseLayer, Overlay } = LayersControl
const OVERLAYS: Array<OverlayItem> = [
Expand All @@ -43,11 +43,8 @@ export default class EditorMapLayersControl extends Component<Props> {
key={tp.id}
positions={tp.latLngs}
weight={2}
color='#888'>
<Tooltip sticky>
<span>{tp.name} ({tp.route_id})</span>
</Tooltip>
</Polyline>
renderer={canvas}
color='#888' />
)
})
: null
Expand Down Expand Up @@ -78,7 +75,7 @@ export default class EditorMapLayersControl extends Component<Props> {
name='Route layer'
key='route-layer'>
<TileLayer
url={`https://s3.amazonaws.com/datatools-nysdot/tiles/{z}_{x}_{y}${Browser.retina ? '@2x' : ''}.png`} />
url={`https://s3.amazonaws.com/datatools-nysdot/tiles/{z}_{x}_{y}${L.Browser.retina ? '@2x' : ''}.png`} />
</Overlay>
}
{OVERLAYS.map((overlay: OverlayItem, i: number) => (
Expand Down
3 changes: 2 additions & 1 deletion lib/editor/containers/ActiveGtfsEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const mapStateToProps = (state: AppState, ownProps: Props) => {
const {activeEntityId, subEntityId} = getIdsFromParams(ownProps.routeParams)
const {data, editSettings: editSettingsState, mapState} = state.editor
const {present: editSettings} = editSettingsState
const {active, tables, status} = data
const {active, tables, tripPatterns, status} = data
// FIXME: entityId is now a non-string line number and somewhere the number is
// being cast to a string.
const activeEntity = active.entity && active.entity.id === activeEntityId
Expand Down Expand Up @@ -130,6 +130,7 @@ const mapStateToProps = (state: AppState, ownProps: Props) => {
tableData: tables,
tableView,
timetableStatus,
tripPatterns,
user,
validationErrors
}
Expand Down
35 changes: 22 additions & 13 deletions lib/editor/reducers/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import update from 'react-addons-update'
import clone from 'lodash/cloneDeep'
import {decode as decodePolyline} from 'polyline'
import SortDirection from 'react-virtualized/dist/commonjs/Table/SortDirection'

import {ENTITY} from '../constants'
Expand All @@ -22,6 +23,11 @@ const getUpdateEntityKeys = component => component === 'trippattern'
export const defaultState = {
active: {},
lock: {},
sort: {
key: 'name',
direction: SortDirection.ASC
},
status: {},
tables: {
agency: [],
calendar: [],
Expand All @@ -36,11 +42,7 @@ export const defaultState = {
service_id: []
}
},
sort: {
key: 'name',
direction: SortDirection.ASC
},
status: {}
tripPatterns: null
}

/* eslint-disable complexity */
Expand All @@ -51,6 +53,10 @@ const data = (state: DataState = defaultState, action: Action): DataState => {
return update(state, {active: {$merge: {feedSourceId}}})
case 'RECEIVE_BASE_GTFS': {
const feed = clone(action.payload.feed)
if (!feed) {
console.warn('Could not fetch base GTFS!')
return state
}
if (feed.feed_info.length === 0) {
console.warn(`No feed info found. Adding feed info with null values.`)
feed.feed_info.push(generateNullProps('feedinfo'))
Expand Down Expand Up @@ -223,6 +229,11 @@ const data = (state: DataState = defaultState, action: Action): DataState => {
if (!tableName) return state
return update(state, {tables: {[tableName]: {$push: [entity]}}})
}
case 'FETCHING_TRIP_PATTERNS': {
// Set value to an empty array to prevent the user from accidentally
// triggering multiple fetches.
return update(state, {tripPatterns: {$set: []}})
}
case 'RECEIVE_GTFS_ENTITIES': {
let activePattern
const {data, editor, component} = action.payload
Expand All @@ -235,15 +246,13 @@ const data = (state: DataState = defaultState, action: Action): DataState => {
// Handle trip patterns to be drawn as overlay layer in editor
return update(state, {
tripPatterns: {$set:
data.feed.patterns.map(pattern => {
data.feed.shapes.map(shape => {
return {
id: pattern.id,
name: pattern.name,
route_id: pattern.route_id,
latLngs: pattern.shape_points
? (pattern.shape_points.map(sp =>
({lon: sp.shape_pt_lon, lat: sp.shape_pt_lat})))
: null
id: shape.shape_id,
// Decode polyline and coords divide by ten (gtfs-lib
// simplification level requires this).
latLngs: decodePolyline(shape.polyline)
.map(coords => ([coords[0] / 10, coords[1] / 10]))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/actions/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const receiveFeedVersions = createAction(
versions: Array<FeedVersion>
}) => payload
)
const receiveGTFSEntities = createAction(
export const receiveGTFSEntities = createAction(
'RECEIVE_GTFS_ENTITIES',
(payload: {
component: string,
Expand Down

0 comments on commit 84abb63

Please sign in to comment.