Skip to content

Commit

Permalink
fix(isochrone): Fix isochrone ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorgerhardt committed Feb 26, 2019
1 parent ab45b17 commit bbf1911
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
21 changes: 8 additions & 13 deletions src/hooks/use-isochrones.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
import reverse from 'lodash/reverse'
import React from 'react'

import useOnLoad from './use-on-load'

const EmptyCollection = {type: 'FeatureCollection', features: []}
const getId = i => `isochrone-${i}`

/**
* Reverse to add the top layer first so that we can insert subsequent
* layers beneath. Use consistently for ID purposes.
* Isochrones are in reverse order of the networks.
*/
export default function useIsochrones (map, isochrones) {
useOnLoad(initializeIsochrones, map, [isochrones])

React.useEffect(() => {
if (!map) return

reverse(isochrones).forEach((isochrone, i) => {
const source = map.getSource(getId(i))
if (source) source.setData(isochrone || EmptyCollection)
isochrones.forEach((isochrone, i) => {
const source = map.getSource(isochrone.properties.id)
if (source) source.setData(isochrone)
})
}, [map, isochrones])
}

function initializeIsochrones (map, isochrones) {
reverse(isochrones).forEach((isochrone, i) => {
const id = `isochrone-${i}`
const data = isochrone || EmptyCollection

map.addSource(id, {type: 'geojson', data})
isochrones.forEach((isochrone, i) => {
const id = isochrone.properties.id
map.addSource(id, {type: 'geojson', data: isochrone})

const beforeLayer = i === 0
? 'waterway'
: `isochrone-${i - 1}`
: isochrones[i - 1].properties.id

// Fill the base layer
map.addLayer({
Expand Down
40 changes: 23 additions & 17 deletions src/selectors/isochrones.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import get from 'lodash/get'
import reduceRight from 'lodash/reduceRight'
import {createSelector} from 'reselect'

import {colors, cutoffs, opacities} from '../constants'
Expand All @@ -9,24 +10,29 @@ export default createSelector(
state => state.networks,
state => state.timeCutoff,
(start, networks = [], timeCutoff) =>
networks.map((n, i) => {
reduceRight(networks, (isochrones, n, i) => {
const data = get(n, 'travelTimeSurface.data')
if (start && data) {
return {
type: 'FeatureCollection',
const features = start && data
? getIsochroneForNetwork(n, start, timeCutoff)
: {}

const isochrone = {
type: 'FeatureCollection',
properties: {
name: n.name,
id: `isochrone-${i}`
},
features: [{
...features,
properties: {
name: n.name
},
features: [{
...getIsochroneForNetwork(n, start, timeCutoff),
properties: {
color: n.hexColor || colors[i].hex,
opacity: 1,
timeCutoff: timeCutoff,
width: 1
}
}]
}
color: n.hexColor || colors[i].hex,
opacity: 1,
timeCutoff: timeCutoff,
width: 1
}
}]
}
})

return [...isochrones, isochrone]
}, [])
)

0 comments on commit bbf1911

Please sign in to comment.