index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Using TopoJSON</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
</head>
<body>
<style>
body {
margin: 0;
}
svg {
background-color: #4B4B55;
}
.land {
fill: #8DBD0C;
fill-opacity: 0.25;
stroke: #8DBD0C;
stroke-width: 1;
}
.graticule {
stroke: #D6FF9F;
stroke-width: 1;
stroke-dasharray: 1,2
}
</style>
<div id="map-container"></div>
<script>
var width = 960,
height = 480;
var div = d3.select('#map-container'),
svg = div.append('svg');
svg.attr('width', width).attr('height', height);
var projection = d3.geo.equirectangular()
.translate([width / 2, height / 2]);
var pathGenerator = d3.geo.path()
.projection(projection);
var graticule = d3.geo.graticule()
.step([20, 20]);
d3.json('land.topojson', function(err, data) {
if (err) { throw err; }
var features = topojson.feature(data, data.objects.land);
var lines = svg.selectAll('path.graticule').data([graticule()]);
lines.enter().append('path').classed('graticule', true);
lines.attr('d', pathGenerator);
lines.exit().remove();
var land = svg.selectAll('path.land').data([features]);
land.enter().append('path').classed('land', true);
land.attr('d', pathGenerator);
land.exit().remove();
});
</script>
</body>
</html>
Makefile
URL = http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/physical/ne_110m_land.zip
ne_110m_land.zip:
curl -LO $(URL)
ne_110m_land.shp: ne_110m_land.zip
unzip ne_110m_land.zip
touch ne_110m_land.shp
land.geojson: ne_110m_land.shp
ogr2ogr -f GeoJSON land.geojson ne_110m_land.shp
land.topojson: land.geojson
topojson -p -o land.topojson land.geojson
clean:
rm ne_110m_land*