Skip to content

Instantly share code, notes, and snippets.

Forked from mbostock/.block
Last active Nov 10, 2020
What would you like to do?
Zoomable Geography
license: gpl-3.0

An example of d3.behavior.zoom and d3.geo.path. By modifying the transform, the browser can rapidly redraw geographic features while panning and zooming, without the overhead of reprojection. This technique can be extended by combining with d3.geo.tile.

#!/usr/bin/env node
var fs = require("fs");
var idProperty = process.argv[2],
collection = JSON.parse(fs.readFileSync("/dev/stdin")),
featureIds = {};
collection.features = collection.features.filter(function(feature) {
var id =[idProperty];
if (id == null) throw new Error("id is required for geouniq");
if (!(id in featureIds)) {
featureIds[id] = 1;
return true;
<!DOCTYPE html>
<meta charset="utf-8">
.overlay {
fill: none;
pointer-events: all;
.state {
fill: #aaa;
.state-border {
fill: none;
stroke: #fff;
stroke-linejoin: round;
stroke-linecap: round;
<script src="//"></script>
<script src="//"></script>
var width = 960,
height = 500;
var path = d3.geo.path()
var zoom = d3.behavior.zoom()
.translate([0, 0])
.scaleExtent([1, 8])
.on("zoom", zoomed);
var svg ="body").append("svg")
.attr("width", width)
.attr("height", height);
var features = svg.append("g");
.attr("class", "overlay")
.attr("width", width)
.attr("height", height)
d3.json("us.json", function(error, us) {
if (error) throw error;
.datum(topojson.feature(us, us.objects.states))
.attr("class", "state")
.attr("d", path);
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "state-border")
.attr("d", path)
.style("stroke-width", "1.5px");
.datum(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b && !( / 1000 ^ / 1000); }))
.attr("class", "county-border")
.attr("d", path)
.style("stroke-width", ".5px");
function zoomed() {
features.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");".state-border").style("stroke-width", 1.5 / d3.event.scale + "px");".county-border").style("stroke-width", .5 / d3.event.scale + "px");
}"height", height + "px");
"name": "anonymous",
"version": "0.0.1",
"private": true,
"devDependencies": {
"topojson": "1"
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This comment has been minimized.

Copy link

@stefanborchardt stefanborchardt commented Mar 7, 2016

I combined this example with raster tiles, also using as a blueprint. There is a small scaling problem when tile zoom levels change, but that could be caused by the rasterizer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment