Skip to content

Instantly share code, notes, and snippets.

@armollica
Last active March 5, 2016 05:28
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 armollica/c6c97710ac404e67b6bf to your computer and use it in GitHub Desktop.
Save armollica/c6c97710ac404e67b6bf to your computer and use it in GitHub Desktop.
In-browser GIS buffer
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.county {
fill: rgb(97, 183, 58);
stroke: #fff;
stroke-width: 2px;
}
.county.buffered {
fill: rgba(255, 255, 255, 0.3);
stroke: none;
}
</style>
</head>
<body>
<script src="//api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js"></script>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.20/topojson.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.conicConformal()
.rotate([90, 0])
.center([0.1995, 44.75])
.parallels([29.5, 45.5])
.scale(6000)
.translate([width/2, height/2])
.precision(0.1);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("wi.json", function(error, wi) {
if (error) throw error;
wi = topojson.feature(wi, wi.objects.wi);
var wiBuffered = buffer(wi, -5, "miles");
svg.selectAll(".county").data(wi.features)
.enter().append("path")
.attr("class", "county")
.attr("d", path);
svg.selectAll(".county.buffered").data(wiBuffered.features)
.enter().append("path")
.attr("class", "county buffered")
.attr("d", path);
});
function buffer(featureCollection, distance, unit) {
var features = featureCollection.features
.map(function(feature) {
var buffered = null;
try { buffered = turf.buffer(feature, distance, unit); }
catch(e) { /* console.error(e); */ }
return buffered;
})
.filter(function(feature) { return feature !== null; });
return {
type: "FeatureCollection",
features: features,
properties: null
};
}
</script>
</body>
</html>
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment