Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoji Chen committed Oct 2, 2017
1 parent 98627e9 commit 6cdf890
Show file tree
Hide file tree
Showing 35 changed files with 418 additions and 584 deletions.
1 change: 1 addition & 0 deletions src/demos/custom-layers/1-combination-layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"build": "NODE_ENV=production webpack --env.prod=true"
},
"dependencies": {
"@turf/turf": "^4.7.3",
"deck.gl": "^4.1.0",
"luma.gl": "^4.0.1",
"react": "^15.4.1",
Expand Down
8 changes: 4 additions & 4 deletions src/demos/custom-layers/1-combination-layer/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export default class App extends Component {
super(props);
this.state = {
viewport: {
width: window.innerWidth,
height: window.innerHeight,
width: props.width || window.innerWidth,
height: props.height || window.innerHeight,
longitude: -74,
latitude: 40.7,
zoom: 11,
Expand Down Expand Up @@ -60,7 +60,7 @@ export default class App extends Component {
}

render() {
const {viewport, currentTime} = this.state;
const {viewport} = this.state;

return (
<div>
Expand All @@ -69,7 +69,7 @@ export default class App extends Component {
onViewportChange={this._onViewportChange}
mapboxApiAccessToken={MAPBOX_TOKEN}>

<DeckGLOverlay viewport={viewport} data={taxiData} currentTime={currentTime} />
<DeckGLOverlay viewport={viewport} data={taxiData} />

</MapGL>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import React, {Component} from 'react';
import DeckGL from 'deck.gl';
import TaxiLayer from './taxi-layer';

const PICKUP_COLOR = [0, 128, 255];
const DROPOFF_COLOR = [255, 0, 128];

export default class DeckGLOverlay extends Component {

render() {
Expand All @@ -16,9 +13,8 @@ export default class DeckGLOverlay extends Component {
new TaxiLayer({
id: 'taxi-trips',
data: this.props.data,
pickupColor: PICKUP_COLOR,
dropoffColor: DROPOFF_COLOR,
radiusScale: 40,
pickupColor: [0, 128, 255],
dropoffColor: [255, 0, 128],
getPickupLocation: d => [d.pickup_longitude, d.pickup_latitude],
getDropoffLocation: d => [d.dropoff_longitude, d.dropoff_latitude]
})
Expand Down
45 changes: 16 additions & 29 deletions src/demos/custom-layers/1-combination-layer/src/taxi-layer.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
import DeckGL, {CompositeLayer, ScatterplotLayer, ArcLayer} from 'deck.gl';

const defaultProps = {
radiusScale: 40,
pickupColor: [255, 0, 0],
dropoffColor: [0, 0, 255],
getPickupLocation: d => d.pickup,
getDropoffLocation: d => d.dropoff
};

export default class TaxiLayer extends CompositeLayer {

renderLayers() {
const {id, data, pickupColor, dropoffColor, radiusScale, getPickupLocation, getDropoffLocation} = this.props;

return [
new ScatterplotLayer({
id: `${id}-pickup`,
data,
getPosition: getPickupLocation,
getColor: d => pickupColor,
radiusScale
id: `${this.props.id}-pickup`,
data: this.props.data,
getPosition: this.props.getPickupLocation,
getColor: d => this.props.pickupColor,
radiusScale: 40
}),
new ScatterplotLayer({
id: `${id}-dropoff`,
data,
getPosition: getDropoffLocation,
getColor: d => dropoffColor,
radiusScale
id: `${this.props.id}-dropoff`,
data: this.props.data,
getPosition: this.props.getDropoffLocation,
getColor: d => this.props.dropoffColor,
radiusScale: 40
}),
new ArcLayer({
id: `${id}-arc`,
data,
id: `${this.props.id}-arc`,
data: this.props.data,
opacity: 0.3,
getSourcePosition: getPickupLocation,
getTargetPosition: getDropoffLocation,
getSourceColor: d => pickupColor,
getTargetColor: d => dropoffColor,
getSourcePosition: this.props.getPickupLocation,
getTargetPosition: this.props.getDropoffLocation,
getSourceColor: d => this.props.pickupColor,
getTargetColor: d => this.props.dropoffColor,
strokeWidth: 2
})
];
}

}

TaxiLayer.layerName = 'TaxiLayer';
TaxiLayer.defaultProps = defaultProps;
4 changes: 2 additions & 2 deletions src/demos/custom-layers/2-adaptor-layer/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class App extends Component {
}

render() {
const {viewport, currentTime} = this.state;
const {viewport} = this.state;

return (
<div>
Expand All @@ -69,7 +69,7 @@ export default class App extends Component {
onViewportChange={this._onViewportChange}
mapboxApiAccessToken={MAPBOX_TOKEN}>

<DeckGLOverlay viewport={viewport} data={taxiData} currentTime={currentTime} />
<DeckGLOverlay viewport={viewport} data={taxiData} />

</MapGL>
</div>
Expand Down
12 changes: 4 additions & 8 deletions src/demos/custom-layers/2-adaptor-layer/src/deckgl-overlay.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React, {Component} from 'react';
import DeckGL from 'deck.gl';
import TaxiLayer from './taxi-cluster-layer';

const PICKUP_COLOR = [0, 128, 255];
const DROPOFF_COLOR = [255, 0, 128];
import TaxiClusterLayer from './taxi-cluster-layer';

export default class DeckGLOverlay extends Component {

Expand All @@ -13,12 +10,11 @@ export default class DeckGLOverlay extends Component {
}

const layers = [
new TaxiLayer({
new TaxiClusterLayer({
id: 'taxi-trips',
data: this.props.data,
pickupColor: PICKUP_COLOR,
dropoffColor: DROPOFF_COLOR,
radiusScale: 40,
pickupColor: [0, 128, 255],
dropoffColor: [255, 0, 128],
getPickupLocation: d => [d.pickup_longitude, d.pickup_latitude],
getDropoffLocation: d => [d.dropoff_longitude, d.dropoff_latitude]
})
Expand Down
31 changes: 31 additions & 0 deletions src/demos/custom-layers/2-adaptor-layer/src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import turf from '@turf/helpers';
import clustersKmeans from '@turf/clusters-kmeans';

/**
* Clusters points
* @param data {array} - source data
* @param accessors {object} - key to accessor mapping.
* keys will be used to add results back to the datum.
* accessor should return a [longitude, latitude] pair.
*/
export function clusterPoints(data, accessors) {
const clusteredData = data.map(d => ({...d}));

const allPoints = [];

data.forEach((d, index) => {
for (const key in accessors) {
const p = turf.point(accessors[key](d), {index, key});
allPoints.push(p);
}
});

// Calculate clusters using K-means: https://en.wikipedia.org/wiki/K-means_clustering)
clustersKmeans(turf.featureCollection(allPoints)).features
.forEach(p => {
const {index, key, centroid} = p.properties;
clusteredData[index][key] = centroid;
});

return clusteredData;
}
33 changes: 7 additions & 26 deletions src/demos/custom-layers/2-adaptor-layer/src/taxi-cluster-layer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import DeckGL, {CompositeLayer} from 'deck.gl';
import TaxiLayer from './taxi-layer';

// turfjs.org
import turf from '@turf/helpers';
import clustersKmeans from '@turf/clusters-kmeans';
import {clusterPoints} from './helpers';

export default class TaxiClusterLayer extends CompositeLayer {

Expand All @@ -12,29 +10,12 @@ export default class TaxiClusterLayer extends CompositeLayer {

if (oldProps.data !== props.data) {
// data changed, recalculate cluster
const {data, getPickupLocation, getDropoffLocation} = props;
const clusteredData = data.map(d => ({...d}));

// Collect pickup and dropoff points into one array
const pickupPoints = data.map((d, index) =>
turf.point(getPickupLocation(d), {pickup: index}));

const dropoffPoints = data.map((d, index) =>
turf.point(getDropoffLocation(d), {dropoff: index}));

const allPoints = turf.featureCollection(pickupPoints.concat(dropoffPoints));

// Calculate clusters using K-means: https://en.wikipedia.org/wiki/K-means_clustering)
clustersKmeans(allPoints).features
.forEach(p => {
const {pickup, dropoff, centroid} = p.properties;
if (pickup !== undefined) {
clusteredData[pickup].pickup_cluster_centroid = centroid;
} else {
clusteredData[dropoff].dropoff_cluster_centroid = centroid;
}
const clusteredData = clusterPoints(props.data, {
pickup_cluster: props.getPickupLocation,
dropoff_cluster: props.getDropoffLocation
});

// save processed data to layer state
this.setState({clusteredData});
}
}
Expand All @@ -44,8 +25,8 @@ export default class TaxiClusterLayer extends CompositeLayer {
new TaxiLayer({
...this.props,
data: this.state.clusteredData,
getPickupLocation: d => d.pickup_cluster_centroid,
getDropoffLocation: d => d.dropoff_cluster_centroid
getPickupLocation: d => d.pickup_cluster,
getDropoffLocation: d => d.dropoff_cluster
})
];
}
Expand Down
45 changes: 16 additions & 29 deletions src/demos/custom-layers/2-adaptor-layer/src/taxi-layer.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
import DeckGL, {CompositeLayer, ScatterplotLayer, ArcLayer} from 'deck.gl';

const defaultProps = {
radiusScale: 40,
pickupColor: [255, 0, 0],
dropoffColor: [0, 0, 255],
getPickupLocation: d => d.pickup,
getDropoffLocation: d => d.dropoff
};

export default class TaxiLayer extends CompositeLayer {

renderLayers() {
const {id, data, pickupColor, dropoffColor, radiusScale, getPickupLocation, getDropoffLocation} = this.props;

return [
new ScatterplotLayer({
id: `${id}-pickup`,
data,
getPosition: getPickupLocation,
getColor: d => pickupColor,
radiusScale
id: `${this.props.id}-pickup`,
data: this.props.data,
getPosition: this.props.getPickupLocation,
getColor: d => this.props.pickupColor,
radiusScale: 40
}),
new ScatterplotLayer({
id: `${id}-dropoff`,
data,
getPosition: getDropoffLocation,
getColor: d => dropoffColor,
radiusScale
id: `${this.props.id}-dropoff`,
data: this.props.data,
getPosition: this.props.getDropoffLocation,
getColor: d => this.props.dropoffColor,
radiusScale: 40
}),
new ArcLayer({
id: `${id}-arc`,
data,
id: `${this.props.id}-arc`,
data: this.props.data,
opacity: 0.3,
getSourcePosition: getPickupLocation,
getTargetPosition: getDropoffLocation,
getSourceColor: d => pickupColor,
getTargetColor: d => dropoffColor,
getSourcePosition: this.props.getPickupLocation,
getTargetPosition: this.props.getDropoffLocation,
getSourceColor: d => this.props.pickupColor,
getTargetColor: d => this.props.dropoffColor,
strokeWidth: 2
})
];
}

}

TaxiLayer.layerName = 'TaxiLayer';
TaxiLayer.defaultProps = defaultProps;
4 changes: 2 additions & 2 deletions src/demos/custom-layers/3-custom-shader/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class App extends Component {
}

render() {
const {viewport, currentTime} = this.state;
const {viewport} = this.state;

return (
<div>
Expand All @@ -69,7 +69,7 @@ export default class App extends Component {
onViewportChange={this._onViewportChange}
mapboxApiAccessToken={MAPBOX_TOKEN}>

<DeckGLOverlay viewport={viewport} data={taxiData} currentTime={currentTime} />
<DeckGLOverlay viewport={viewport} data={taxiData} />

</MapGL>
</div>
Expand Down
9 changes: 3 additions & 6 deletions src/demos/custom-layers/3-custom-shader/src/deckgl-overlay.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React, {Component} from 'react';
import DeckGL from 'deck.gl';
import ScatterplotLayer from './my-scatterplot-layer';

const PICKUP_COLOR = [0, 128, 255];
const DROPOFF_COLOR = [255, 0, 128];
import MyScatterplotLayer from './my-scatterplot-layer';

export default class DeckGLOverlay extends Component {

Expand All @@ -13,11 +10,11 @@ export default class DeckGLOverlay extends Component {
}

const layers = [
new ScatterplotLayer({
new MyScatterplotLayer({
id: 'pickup',
data: this.props.data,
getPosition: d => [d.pickup_longitude, d.pickup_latitude],
getColor: d => PICKUP_COLOR,
getColor: d => [0, 128, 255],
radiusScale: 40
})
];
Expand Down
Loading

0 comments on commit 6cdf890

Please sign in to comment.