An example of the New Jersey state plane, a transverse Mercator projection with an origin of 74° 30′ W and 38° 50′ N. The projections for each state are defined in the USGS publication, State Plane Coordinate System of 1983. This map shows census tracts; the data is available as TopoJSON in the U.S. Atlas project. This example borrows from my earlier Project to Bounding Box example to compute the projection’s scale and translate automatically from the displayed geometry.
-
-
Save pgiraud/9001340 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.tract { | |
fill: #eee; | |
} | |
.tract:hover { | |
fill: orange; | |
} | |
.tract-border { | |
fill: none; | |
stroke: #777; | |
pointer-events: none; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/topojson.v1.min.js"></script> | |
<script> | |
var width = 960, | |
height = 1200; | |
var projection = d3.geo.transverseMercator(); | |
// .rotate([74 + 30 / 60, -38 - 50 / 60]); | |
var path = d3.geo.path() | |
.projection(projection); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
d3.json("nj-tracts.json", function(error, nj) { | |
var tracts = topojson.feature(nj, nj.objects.tracts); | |
projection | |
.scale(1) | |
.translate([0, 0]); | |
var b = path.bounds(tracts), | |
s = .95 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height), | |
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2]; | |
projection | |
.scale(s) | |
.translate(t); | |
svg.selectAll("path") | |
.data(tracts.features.filter(function(d) { return (d.id / 10000 | 0) % 100 !== 99; })) | |
.enter().append("path") | |
.attr("class", "tract") | |
.attr("d", path) | |
.append("title") | |
.text(function(d) { return d.id; }); | |
svg.append("path") | |
.datum(topojson.mesh(nj, nj.objects.tracts, function(a, b) { return a !== b; })) | |
.attr("class", "tract-border") | |
.attr("d", path); | |
}); | |
d3.select(self.frameElement).style("height", height + "px"); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment