Here you will find an empty HTML template for the workshop. We'll be adding javascript code to the section between the opening and closing <script></script>
tags in the index.html file.
Let's get started!
Built with blockbuilder.org
forked from clhenrick's block: ischool-geo-viz-template
xxxxxxxxxx
<html>
<head>
<meta charset='utf-8' />
<title>iSchool GeoVisualization Workshop</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src="//d3js.org/d3.v3.min.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.35.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.35.0/mapbox-gl.css' rel='stylesheet' />
<script src="https://cdnjs.cloudflare.com/ajax/libs/turf.js/2.0.2/turf.min.js"></script>
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:700px; }
</style>
</head>
<body>
<div id='map'></div>
<script>
// javascript code goes here
var dataURL = "https://raw.githubusercontent.com/clhenrick/geovisualization_workshop_ischool/master/data/nyc_crashes.geojson";
var accessToken = 'pk.eyJ1IjoiZW5qYWxvdCIsImEiOiIzOTJmMjBiZmI2NGQ2ZjAzODhiMzhiOGI2MTI1YTk4YSJ9.sIOXXU3TPp5dLg_L3cUxhQ';
mapboxgl.accessToken = accessToken;
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/dark-v9',
center: [-74.0059, 40.7127],
zoom: 10
});
d3.json(dataURL, function(error, data) {
if (error) throw error;
// console.log(data);
// bounding box of NYC
var bbox = [-74.2777, 40.488, -73.66, 40.939];
// cellsize of our hex bins
var cellSize = 5;
// units of measure for cell size
var units = 'kilometers';
var hexGrid = turf.hexGrid(bbox, cellSize, units);
// count number of crashes
var hexCrashes = turf.count(hexGrid, data, 'totalCrashes');
var numberOfBreaks = 5;
var jenksBreaks = turf.jenks(hexCrashes, 'totalCrashes', numberOfBreaks);
var colors = ['#feebe2', '#fbb4b9', '#f768a1', '#aaa', '#aaa']
var colorStops = jenksBreaks.map(function(b, i) {
if (i > 0) {
return [b, colors[i-1]];
} else {
return [b, 'rgb(0,0,0,0)']
}
});
map.addSource('crashesHexGrd', {
type: 'geojson',
data: hexCrashes
});
map.addLayer({
id: "crashesHexGrid",
type: "fill",
source: "crashesHexGrid",
layout: {},
paint: {
"fill-color": {
property: "totalCrahes",
stops: colorStops
},
"fill-opacity"" 0.6
}
})
// add our data source to our map object
// map.addSource('crashes', {
// type: 'geojson',
// data: data
// })
// // add our layer style definition
// map.addLayer({
// id: 'crashes',
// type: 'circle',
// source: 'crashes',
// layout: {},
// paint: {
// // 'circle-color': 'orange',
// 'circle-color': {
// property: 'crash_type',
// type: 'categorical',
// stops: [
// ['no_injury_fatality', '#FECC5C'],
// ['injury', '#FD8D3C'],
// ['fatality', '#F03B20'],
// ['injury_and_fatality', '#BD0026']
// ]
// },
// 'circle-opacity': 0.8,
// // 'circle-radius': 5
// // set circle radius to be 2 at near beginning,
// // then increase to 180 at zoom 22
// 'circle-radius': {
// 'base': 1.75,
// 'stops': [
// [12, 2],
// [22, 180]
// ]
// }
// }
// });
});
// Final solution at https://github.com/clhendrick/visualization_workshop_ischool
</script>
</body>
</html>
Updated missing url https://cdnjs.cloudflare.com/ajax/libs/Turf.js/2.0.2/turf.min.js to https://cdnjs.cloudflare.com/ajax/libs/turf.js/2.0.2/turf.min.js
https://d3js.org/d3.v3.min.js
https://api.tiles.mapbox.com/mapbox-gl-js/v0.35.0/mapbox-gl.js
https://cdnjs.cloudflare.com/ajax/libs/Turf.js/2.0.2/turf.min.js