Skip to content

Instantly share code, notes, and snippets.

@rukku
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rukku/63c34e7918f61cf166ca to your computer and use it in GitHub Desktop.
Save rukku/63c34e7918f61cf166ca to your computer and use it in GitHub Desktop.
For the Turf Exercise
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Buildings along the Ikot Route</title>
<link href='https://www.mapbox.com/base/latest/base.css' rel='stylesheet' />
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v1.6.2/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v1.6.2/mapbox.css' rel='stylesheet' />
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-label/v0.2.1/leaflet.label.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-label/v0.2.1/leaflet.label.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
.small-input { width: 80px; vertical-align:middle; }
</style>
</head>
<body>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v1.3.0/turf.min.js'></script>
<div id='map'></div>
<div class='pin-top fill-gray pad1'>
UP Buildings near the Ikot route
<input type='number' id='radius' max='4000' min='0' class='clean short small-input' value='500' step='10' /> feet
of UP Ikot route.
<p class='small'>A GeoJSON route is buffered with <a target='_blank' href='https://github.com/Turfjs/turf-buffer'>turf-buffer</a>
and points are found with <a href='https://github.com/Turfjs/turf-within'>turf-within</a>.</p>
</div>
<script src='site.js'></script>
</body>
</html>
var map = L.mapbox.map('map', 'examples.map-y7l23tes', { zoomControl: false });
map.scrollWheelZoom.disable();
var bufferLayer = L.mapbox.featureLayer().addTo(map);
var raceRoute = L.mapbox.featureLayer().addTo(map);
var schoolFountains = L.mapbox.featureLayer().addTo(map);
var schoolFountainsInside = L.mapbox.featureLayer().addTo(map);
schoolFountains.loadURL('./buildingCentroids.geojson')
.on('ready', done);
raceRoute.loadURL('./ikot.geojson')
.on('ready', done);
var loaded = 0;
function done() {
if (++loaded !== 2) return;
raceRoute.setStyle({ color: 'hotpink', weight: 3 });
map.fitBounds(raceRoute.getBounds());
function run() {
var radius = parseInt(document.getElementById('radius').value);
if (isNaN(radius)) radius = 100;
var buffer = turf.buffer(raceRoute.getGeoJSON(), radius/5280, 'miles');
var fountains = schoolFountains.getGeoJSON();
fountains.features.forEach(function(feature) {
feature.properties['marker-color'] = '#111';
feature.properties['marker-symbol'] = 'building';
feature.properties['marker-size'] = 'small';
});
schoolFountains.setGeoJSON(fountains);
bufferLayer
.setGeoJSON(buffer)
.setStyle({
stroke: false,
fillColor: 'red',
fillOpacity: 0.2
})
.eachLayer(function(layer) {
layer.bindLabel('Ikot Route', { noHide: true });
});
var fountainsInside = turf.within(schoolFountains.getGeoJSON(), buffer);
fountainsInside.features.forEach(function(feature) {
feature.properties['marker-color'] = '#00f';
feature.properties['marker-symbol'] = 'building';
feature.properties['marker-size'] = 'large';
});
schoolFountainsInside
.setGeoJSON(fountainsInside)
.eachLayer(function(layer) {
layer.bindLabel('Accessible school fountain');
});
}
run();
document.getElementById('radius').onchange = run;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment