Skip to content

Instantly share code, notes, and snippets.

@pnavarrc
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pnavarrc/9843638 to your computer and use it in GitHub Desktop.
Save pnavarrc/9843638 to your computer and use it in GitHub Desktop.
D3 Orthographic Projection with TopoJSON
<html>
<head>
<title>D3 Orthographic Projection with TopoJSON</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.4.4/d3.min.js" charset="utf-8"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/topojson/1.1.0/topojson.min.js" charset="utf-8"></script>
</head>
<body>
<div id="map-container"></div>
<script>
var width = 500,
height = 500;
d3.json('land.topojson', function(error, data) {
if (error) { return error; }
// Transform the TopoJSON data to GeoJSON
var geojson = topojson.feature(data, data.objects.land);
var div = d3.select('#map-container'),
svg = div.selectAll('#map-container').data([geojson]);
// Create the SVG container
svg.enter().append('svg')
.attr('width', width)
.attr('height', height);
// Create and configure the projection
var projection = d3.geo.orthographic()
.scale(width / 2)
.translate([width / 2, height / 2])
.clipAngle(90);
// Create and configure the geo path generator
var path = d3.geo.path()
.projection(projection);
// Globe
var globe = svg.append('path').datum({type: 'Sphere'})
.attr('d', path)
.attr('class', 'globe')
.attr('fill', '#6D9BB9');
var land = svg.selectAll('path.feature').data([geojson])
.enter()
.append('path')
.attr('d', path)
.attr('class', 'feature')
.attr('fill', '#DEE1CB');
});
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
URL = http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/physical/ne_50m_land.zip
ne_50m_land.zip:
curl -LO $(URL)
ne_50m_land.shp: ne_50m_land.zip
unzip ne_50m_land.zip
touch ne_50m_land.shp
land.json: ne_50m_land.shp
ogr2ogr -f GeoJSON land.json ne_50m_land.shp
land.topojson: land.json
topojson -o land.topojson land.json
clean:
rm ne_50m_land*
rm land.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment