Search code examples
javascriptd3.jssvggeo

d3 v4 geo draws boundary inverted


When I draw the Bermuda Triangle in an SVG element the scale is not what I expect (triangle should extend to edges of box) and the fill is backward (instead of drawing a triangle, it draws a square with a triangle cut out).

var geojson = {
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "Bermuda Triangle",
        "area": 1150180
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [-64.73, 32.31],
            [-80.19, 25.76],
            [-66.09, 18.43],
            [-64.73, 32.31]
          ]
        ]
      }
    }
  ],
  "type":"FeatureCollection"
};

var width = 480;
var height = 480;

var svg = d3.select("body").append("svg")
  .attr("width", width)
  .attr("height", height)
  .attr("style", "border: 2px solid black");

var projection = d3.geoMercator().fitSize([width, height], geojson);
var path = d3.geoPath().projection(projection);

svg.selectAll('path')
  .data(geojson.features)
  .enter()
  .append('path')
  .attr('d', path)
  .style("fill", "red")
  .style("stroke-width", "1")
  .style("stroke", "black");
<script src="//d3js.org/d3.v4.js"></script>

What am I doing wrong?


Solution

  • Let's change this:

    [
        [-64.73, 32.31],
        [-80.19, 25.76],
        [-66.09, 18.43],
        [-64.73, 32.31]
    ]
    

    To this:

    [
        [-64.73, 32.31],
        [-66.09, 18.43],
        [-80.19, 25.76],
        [-64.73, 32.31]
    ]
    

    It seems like a small change, but it is an important one: D3 expects the polygon vertices in a clockwise order.

    According to the API:

    Spherical polygons also require a winding order convention to determine which side of the polygon is the inside: the exterior ring for polygons smaller than a hemisphere must be clockwise, while the exterior ring for polygons larger than a hemisphere must be anticlockwise. (emphasis mine)

    Also, this is an interesting bl.ocks made by Bostock (D3 creator), didactically explaining your issue: https://bl.ocks.org/mbostock/a7bdfeb041e850799a8d3dce4d8c50c8

    Here is your code with that change (and removing the fitSize):

    var geojson = {
      "features": [{
        "type": "Feature",
        "properties": {
          "name": "Bermuda Triangle",
          "area": 1150180
        },
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [-64.73, 32.31],
              [-66.09, 18.43],
              [-80.19, 25.76],
              [-64.73, 32.31]
            ]
          ]
        }
      }],
      "type": "FeatureCollection"
    };
    
    var width = 480;
    var height = 480;
    
    var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height)
      .attr("style", "border: 2px solid black");
    
    var projection = d3.geoMercator();
    var path = d3.geoPath().projection(projection);
    
    svg.selectAll('path')
      .data(geojson.features)
      .enter()
      .append('path')
      .attr('d', path)
      .style("fill", "red")
      .style("stroke-width", "1")
      .style("stroke", "black");
    <script src="//d3js.org/d3.v4.js"></script>