Skip to content

Instantly share code, notes, and snippets.

@milafrerichs
Created August 8, 2017 17:42
Show Gist options
  • Save milafrerichs/81466ee479b3f520f91408ce35efe60c to your computer and use it in GitHub Desktop.
Save milafrerichs/81466ee479b3f520f91408ce35efe60c to your computer and use it in GitHub Desktop.
JS Bin OSM Basemap with Circles // source http://jsbin.com/bozonu
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="OSM Basemap with Circles">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
<script src="http://d3js.org/d3-tile.v0.0.min.js"></script>
<style id="jsbin-css">
.map {
position: relative;
overflow: hidden;
}
.layer {
position: absolute;
will-change: transform;
}
.tile {
position: absolute;
width: 256px;
height: 256px;
}
.tile path {
fill: none;
stroke: #000;
stroke-linejoin: round;
stroke-linecap: round;
}
.tile .major_road {
stroke: #776;
}
.tile .minor_road {
stroke: #ccb;
}
.tile .highway {
stroke: #f39;
stroke-width: 1.5px;
}
.tile .rail {
stroke: #7de;
}
.info {
position: absolute;
bottom: 10px;
left: 10px;
}
</style>
</head>
<body>
<svg>
</svg>
<script id="jsbin-javascript">
var pi = Math.PI,
tau = 2 * pi;
var width = Math.max(600, window.innerWidth),
height = Math.max(600, window.innerHeight);
// Initialize the projection to fit the world in a 1×1 square centered at the origin.
var projection = d3.geoMercator()
.scale(1 / tau)
.translate([0, 0]);
var path = d3.geoPath()
.projection(projection);
var tile = d3.tile()
.size([width, height]);
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
var raster = svg.append("g");
var points = svg.append("g").attr('class', 'points');
var vector = svg.append("path");
var path = d3.geoPath()
.projection(projection);
d3.json("https://gist.githubusercontent.com/anonymous/d57ad28b361f0adb030b7356173ff978/raw/b466ebacddf86c55f9cb75a443052e046a81c364/map.geojson", function(error, json) {
if (error) throw error;
init(json)
});
function init(json) {
var center = projection([ -71.11,
42.37]);
var transform = d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(1 << 20)
.translate(-center[0], -center[1])
var tiles = tile
.scale(transform.k)
.translate([transform.x, transform.y])
();
projection
.scale(transform.k / tau)
.translate([transform.x, transform.y]);
var circles = points.selectAll('path').data(json.features)
circles.enter().append('path').merge(circles).attr('d',path)
circles.exit().remove()
var image = raster
.attr("transform", stringify(tiles.scale, tiles.translate))
.selectAll("image")
.data(tiles, function(d) { return d; });
image.exit().remove();
image.enter().append("image")
.attr("xlink:href", function(d) { return "http://" + "abc"[d[1] % 3] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
.attr("x", function(d) { return d[0] * 256; })
.attr("y", function(d) { return d[1] * 256; })
.attr("width", 256)
.attr("height", 256);
}
function stringify(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "translate(" + r(translate[0] * scale) + "," + r(translate[1] * scale) + ") scale(" + k + ")";
}
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta name="description" content="OSM Basemap with Circles">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"><\/script>
<script src="//d3js.org/d3-tile.v0.0.min.js"><\/script>
</head>
<body>
<svg>
</svg>
</body>
</html></script>
<script id="jsbin-source-css" type="text/css">.map {
position: relative;
overflow: hidden;
}
.layer {
position: absolute;
will-change: transform;
}
.tile {
position: absolute;
width: 256px;
height: 256px;
}
.tile path {
fill: none;
stroke: #000;
stroke-linejoin: round;
stroke-linecap: round;
}
.tile .major_road {
stroke: #776;
}
.tile .minor_road {
stroke: #ccb;
}
.tile .highway {
stroke: #f39;
stroke-width: 1.5px;
}
.tile .rail {
stroke: #7de;
}
.info {
position: absolute;
bottom: 10px;
left: 10px;
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">var pi = Math.PI,
tau = 2 * pi;
var width = Math.max(600, window.innerWidth),
height = Math.max(600, window.innerHeight);
// Initialize the projection to fit the world in a 1×1 square centered at the origin.
var projection = d3.geoMercator()
.scale(1 / tau)
.translate([0, 0]);
var path = d3.geoPath()
.projection(projection);
var tile = d3.tile()
.size([width, height]);
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
var raster = svg.append("g");
var points = svg.append("g").attr('class', 'points');
var vector = svg.append("path");
var path = d3.geoPath()
.projection(projection);
d3.json("https://gist.githubusercontent.com/anonymous/d57ad28b361f0adb030b7356173ff978/raw/b466ebacddf86c55f9cb75a443052e046a81c364/map.geojson", function(error, json) {
if (error) throw error;
init(json)
});
function init(json) {
var center = projection([ -71.11,
42.37]);
var transform = d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(1 << 20)
.translate(-center[0], -center[1])
var tiles = tile
.scale(transform.k)
.translate([transform.x, transform.y])
();
projection
.scale(transform.k / tau)
.translate([transform.x, transform.y]);
var circles = points.selectAll('path').data(json.features)
circles.enter().append('path').merge(circles).attr('d',path)
circles.exit().remove()
var image = raster
.attr("transform", stringify(tiles.scale, tiles.translate))
.selectAll("image")
.data(tiles, function(d) { return d; });
image.exit().remove();
image.enter().append("image")
.attr("xlink:href", function(d) { return "http://" + "abc"[d[1] % 3] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
.attr("x", function(d) { return d[0] * 256; })
.attr("y", function(d) { return d[1] * 256; })
.attr("width", 256)
.attr("height", 256);
}
function stringify(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "translate(" + r(translate[0] * scale) + "," + r(translate[1] * scale) + ") scale(" + k + ")";
}
</script></body>
</html>
.map {
position: relative;
overflow: hidden;
}
.layer {
position: absolute;
will-change: transform;
}
.tile {
position: absolute;
width: 256px;
height: 256px;
}
.tile path {
fill: none;
stroke: #000;
stroke-linejoin: round;
stroke-linecap: round;
}
.tile .major_road {
stroke: #776;
}
.tile .minor_road {
stroke: #ccb;
}
.tile .highway {
stroke: #f39;
stroke-width: 1.5px;
}
.tile .rail {
stroke: #7de;
}
.info {
position: absolute;
bottom: 10px;
left: 10px;
}
var pi = Math.PI,
tau = 2 * pi;
var width = Math.max(600, window.innerWidth),
height = Math.max(600, window.innerHeight);
// Initialize the projection to fit the world in a 1×1 square centered at the origin.
var projection = d3.geoMercator()
.scale(1 / tau)
.translate([0, 0]);
var path = d3.geoPath()
.projection(projection);
var tile = d3.tile()
.size([width, height]);
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
var raster = svg.append("g");
var points = svg.append("g").attr('class', 'points');
var vector = svg.append("path");
var path = d3.geoPath()
.projection(projection);
d3.json("https://gist.githubusercontent.com/anonymous/d57ad28b361f0adb030b7356173ff978/raw/b466ebacddf86c55f9cb75a443052e046a81c364/map.geojson", function(error, json) {
if (error) throw error;
init(json)
});
function init(json) {
var center = projection([ -71.11,
42.37]);
var transform = d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(1 << 20)
.translate(-center[0], -center[1])
var tiles = tile
.scale(transform.k)
.translate([transform.x, transform.y])
();
projection
.scale(transform.k / tau)
.translate([transform.x, transform.y]);
var circles = points.selectAll('path').data(json.features)
circles.enter().append('path').merge(circles).attr('d',path)
circles.exit().remove()
var image = raster
.attr("transform", stringify(tiles.scale, tiles.translate))
.selectAll("image")
.data(tiles, function(d) { return d; });
image.exit().remove();
image.enter().append("image")
.attr("xlink:href", function(d) { return "http://" + "abc"[d[1] % 3] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
.attr("x", function(d) { return d[0] * 256; })
.attr("y", function(d) { return d[1] * 256; })
.attr("width", 256)
.attr("height", 256);
}
function stringify(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "translate(" + r(translate[0] * scale) + "," + r(translate[1] * scale) + ") scale(" + k + ")";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment