|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>Shaded relief</title> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
body { |
|
padding: 0; |
|
margin: 0; |
|
} |
|
|
|
.parishes { |
|
fill: none; |
|
stroke: white; |
|
stroke-width: 1px; |
|
stroke-opacity: 1; |
|
} |
|
|
|
.raster { |
|
fill: none; |
|
opacity: 1; |
|
} |
|
|
|
.country-border { |
|
fill: none; |
|
stroke: red; |
|
stroke-width: 5px; |
|
stroke-opacity: 0.7; |
|
} |
|
|
|
</style> |
|
</head> |
|
<body> |
|
<svg id="map"></svg> |
|
<script src="//d3js.org/d3.v3.min.js"></script> |
|
<script src="//d3js.org/queue.v1.min.js"></script> |
|
<script src="//d3js.org/topojson.v1.min.js"></script> |
|
<script> |
|
|
|
var map_width = 600, |
|
map_height = 600; |
|
|
|
var svg = d3.select("#map") |
|
.attr("width", map_width) |
|
.attr("height", map_height); |
|
|
|
var la_vector_projection = d3.geo.mercator() |
|
.scale(1) |
|
.translate([0, 0]); |
|
|
|
var la_vector_path = d3.geo.path() |
|
.projection(la_vector_projection); |
|
|
|
queue() |
|
.defer(d3.json, "louisiana.json") |
|
.await(ready); |
|
|
|
function ready(error, louisiana) { |
|
if (error) throw error; |
|
|
|
var parishes = topojson.feature(louisiana, louisiana.objects.louisiana); |
|
|
|
var b = la_vector_path.bounds(parishes), |
|
s = 1 / Math.max((b[1][0] - b[0][0]) / map_width, (b[1][1] - b[0][1]) / map_height), |
|
t = [(map_width - s * (b[1][0] + b[0][0])) / 2, (map_height - s * (b[1][1] + b[0][1])) / 2]; |
|
|
|
// Update the projection to use computed scale & translate. |
|
la_vector_projection |
|
.scale(s) |
|
.translate(t); |
|
|
|
// Raster |
|
var raster_width = (b[1][0] - b[0][0]) * s; |
|
var raster_height = (b[1][1] - b[0][1]) * s; |
|
|
|
var rtranslate_x = (map_width - raster_width) / 2; |
|
var rtranslate_y = (map_height - raster_height) / 2; |
|
|
|
// Shaded relief, Louisiana |
|
// svg.append("clipPath") |
|
// .attr("id", "la_clip") |
|
// .append("use") |
|
// .attr("xlink:href", "#louisiana"); |
|
svg.append("image") |
|
.attr("clip-path", "url(#la_clip)") |
|
.attr("xlink:href", "louisiana-crop.png") |
|
.attr("class", "raster") |
|
.attr("width", raster_width) |
|
.attr("height", raster_height) |
|
.attr("transform", "translate(" + rtranslate_x + ", " + rtranslate_y + ")"); |
|
// svg.append("use") |
|
// .attr("xlink:href", "#louisiana"); |
|
|
|
// Draw parishes |
|
svg.append("path") |
|
.datum(parishes) |
|
.datum(parishes, function(a, b) { return a !== b; }) |
|
.attr("class", "parishes") |
|
.attr("id", "louisiana") // For shaded relief |
|
.attr("d", la_vector_path); |
|
} |
|
|
|
// Allows iframe on bl.ocks.org. |
|
d3.select(self.frameElement).style("height", map_height + "px"); |
|
|
|
</script> |
|
</body> |
|
</html> |