d3 v4地理绘制边界反转 [英] d3 v4 geo draws boundary inverted

查看:97
本文介绍了d3 v4地理绘制边界反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在SVG元素中绘制百慕大三角时,比例不是我期望的(三角形应延伸到框的边缘)并且填充是向后的(而不是绘制三角形,而是绘制了一个带有三角形切出的正方形) ).

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>

我在做什么错了?

推荐答案

让我们对此进行更改:

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

对此:

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

这似乎是一个很小的变化,但它是一个重要的变化:D3希望多边形顶点按顺时针顺序排列.

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

根据 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)

此外,这是Bostock(D3创建者)制作的有趣的bl.ocks,从说教上解释了您的问题: https://bl.ocks.org/mbostock/a7bdfeb041e850799a8d3dce4d8c50c8

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

这是您所做的更改(并删除了fitSize)的代码:

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>

这篇关于d3 v4地理绘制边界反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆