-
Notifications
You must be signed in to change notification settings - Fork 13.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[refactor] Remove dependency on personal fork of supercluster from mapbox visualizations #5902
Merged
mistercrunch
merged 2 commits into
apache:master
from
lyft:xtinec/use-mapbox-supercluster
Sep 18, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1361,7 +1361,6 @@ export const controls = { | |
'mean', | ||
'min', | ||
'max', | ||
'median', | ||
'stdev', | ||
'var', | ||
]), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,10 +48,6 @@ class MapBox extends React.Component { | |
height, | ||
}).fitBounds(bounds); | ||
const { latitude, longitude, zoom } = mercator; | ||
// Compute the clusters based on the bounds. Again, this is only done once because | ||
// we don't update the clusters as we pan/zoom in the current design. | ||
const bbox = [bounds[0][0], bounds[0][1], bounds[1][0], bounds[1][1]]; | ||
this.clusters = this.props.clusterer.getClusters(bbox, Math.round(zoom)); | ||
|
||
this.state = { | ||
viewport: { | ||
|
@@ -80,10 +76,19 @@ class MapBox extends React.Component { | |
pointRadiusUnit, | ||
renderWhileDragging, | ||
rgb, | ||
hasCustomMetric, | ||
bounds, | ||
} = this.props; | ||
const { viewport } = this.state; | ||
const isDragging = viewport.isDragging === undefined ? false : | ||
viewport.isDragging; | ||
|
||
// Compute the clusters based on the original bounds and current zoom level. Note when zoom/pan | ||
// to an area outside of the original bounds, no additional queries are made to the backend to | ||
// retrieve additional data. | ||
const bbox = [bounds[0][0], bounds[0][1], bounds[1][0], bounds[1][1]]; | ||
const clusters = this.props.clusterer.getClusters(bbox, Math.round(viewport.zoom)); | ||
|
||
return ( | ||
<MapGL | ||
{...viewport} | ||
|
@@ -98,14 +103,14 @@ class MapBox extends React.Component { | |
isDragging={isDragging} | ||
width={width} | ||
height={height} | ||
locations={Immutable.fromJS(this.clusters)} | ||
locations={Immutable.fromJS(clusters)} | ||
dotRadius={pointRadius} | ||
pointRadiusUnit={pointRadiusUnit} | ||
rgb={rgb} | ||
globalOpacity={globalOpacity} | ||
compositeOperation={'screen'} | ||
renderWhileDragging={renderWhileDragging} | ||
aggregatorName={aggregatorName} | ||
aggregation={hasCustomMetric ? aggregatorName : null} | ||
lngLatAccessor={(location) => { | ||
const coordinates = location.get('geometry').get('coordinates'); | ||
return [coordinates.get(0), coordinates.get(1)]; | ||
|
@@ -119,34 +124,10 @@ class MapBox extends React.Component { | |
MapBox.propTypes = propTypes; | ||
MapBox.defaultProps = defaultProps; | ||
|
||
function createReducer(aggregatorName, customMetric) { | ||
if (aggregatorName === 'sum' || !customMetric) { | ||
return (a, b) => a + b; | ||
} else if (aggregatorName === 'min') { | ||
return Math.min; | ||
} else if (aggregatorName === 'max') { | ||
return Math.max; | ||
} | ||
return function (a, b) { | ||
if (a instanceof Array) { | ||
if (b instanceof Array) { | ||
return a.concat(b); | ||
} | ||
a.push(b); | ||
return a; | ||
} | ||
if (b instanceof Array) { | ||
b.push(a); | ||
return b; | ||
} | ||
return [a, b]; | ||
}; | ||
} | ||
|
||
function mapbox(slice, payload, setControlValue) { | ||
const { formData, selector } = slice; | ||
const { | ||
customMetric, | ||
hasCustomMetric, | ||
geoJSON, | ||
bounds, | ||
mapboxApiKey, | ||
|
@@ -170,18 +151,41 @@ function mapbox(slice, payload, setControlValue) { | |
return; | ||
} | ||
|
||
const clusterer = supercluster({ | ||
const opts = { | ||
radius: clusteringRadius, | ||
maxZoom: DEFAULT_MAX_ZOOM, | ||
metricKey: 'metric', | ||
metricReducer: createReducer(aggregatorName, customMetric), | ||
}); | ||
}; | ||
if (hasCustomMetric) { | ||
opts.initial = () => ({ | ||
sum: 0, | ||
squaredSum: 0, | ||
min: Infinity, | ||
max: -Infinity, | ||
}); | ||
opts.map = prop => ({ | ||
sum: prop.metric, | ||
squaredSum: Math.pow(prop.metric, 2), | ||
min: prop.metric, | ||
max: prop.metric, | ||
}); | ||
opts.reduce = (accu, prop) => { | ||
// Temporarily disable param-reassignment linting to work with supercluster's api | ||
/* eslint-disable no-param-reassign */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [non actionable] interesting related conversation: |
||
accu.sum += prop.sum; | ||
accu.squaredSum += prop.squaredSum; | ||
accu.min = Math.min(accu.min, prop.min); | ||
accu.max = Math.max(accu.max, prop.max); | ||
/* eslint-enable no-param-reassign */ | ||
}; | ||
} | ||
const clusterer = supercluster(opts); | ||
clusterer.load(geoJSON.features); | ||
|
||
ReactDOM.render( | ||
<MapBox | ||
width={slice.width()} | ||
height={slice.height()} | ||
hasCustomMetric={hasCustomMetric} | ||
aggregatorName={aggregatorName} | ||
clusterer={clusterer} | ||
globalOpacity={globalOpacity} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍾 🎆 🍾