Skip to content

Instantly share code, notes, and snippets.

@rickj33
Last active October 5, 2016 21:00
Show Gist options
  • Save rickj33/6d2ae74b0f4ac9d15b899032e840d3e8 to your computer and use it in GitHub Desktop.
Save rickj33/6d2ae74b0f4ac9d15b899032e840d3e8 to your computer and use it in GitHub Desktop.
TopoJsonMergeIssue
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.state {
fill: orange;
stroke: #000;
}
.state-boundary {
fill: none;
stroke: #fff;
}
.us {
fill: #ccc;
}
</style>
<body>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="//d3js.org/d3-tile.v0.0.min.js"></script>
<script src="/rickj33/raw/6d2ae74b0f4ac9d15b899032e840d3e8/selectedZipcodes.js"></script>
<script>
var width = Math.max(960, window.innerWidth), height = Math.max(600, window.innerHeight);
var pi = Math.PI, tau = 2 * pi;
var projection = d3.geoMercator()
.scale(1 / tau)
.translate([0, 0]);
var path = d3.geoPath().projection(projection);
var tile = d3.tile()
.size([width, height]);
var zoom = d3.zoom()
//.scaleExtent([1 << 11, 1 << 14])
.on("zoom", zoomed);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var raster = svg.append("g");
var vector = svg.append("path");
d3.json("/rickj33/raw/6d2ae74b0f4ac9d15b899032e840d3e8/midAtlanticZipcodeTopo.json", function(error, midAtlantic)
{
if (error)
{
throw error;
}
var center = projection([-76.640625, 38.676933444637925]);
svg.call(zoom)
.call(zoom.transform, d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(1 << 12)
.translate(-center[0], -center[1]));
vector.attr("d", path(topojson.merge(midAtlantic, midAtlantic.objects.midAtlantic.geometries.filter(function(d)
{
return selectedZipcodes.has(d.id);
}))))
.attr("class", "state");
});
function zoomed()
{
var transform = d3.event.transform;
var tiles = tile
.scale(transform.k)
.translate([transform.x, transform.y])();
vector
.attr("transform", transform)
.style("stroke-width", 1 / transform.k);
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>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

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