ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – d3 v4 geo绘制边界倒置

2019-08-23 15:38:36  阅读:218  来源: 互联网

标签:javascript svg d3-js geo


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

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期望按顺时针顺序的多边形顶点.

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

这是你的代码改变(并删除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>

标签:javascript,svg,d3-js,geo
来源: https://codeday.me/bug/20190823/1698534.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有