d3.geoEquirectangular()对地图的渲染不一致

时间:2018-03-16 02:11:20

标签: javascript d3.js geojson geo map-projections

我最近使用D3制作了一些地图,并且在路径未正确关闭时出现了一些问题,如下面的第一个示例所示。我认为这是围绕-90度纬度精度的某种数据问题,但我不确定如何验证这种怀疑。有趣的是,当我在页面上包含最新版本的d3-geo模块时,地图会按预期呈现(请参阅下面的第二个示例)。这里发生了什么?



var svg = d3.select("#map").style("border","0px solid #000000");

    var width = 600;
    var height = 300;

    var proj = d3.geoEquirectangular().scale(95).translate([300,150]);
    var path = d3.geoPath(proj);

    //countries from natural earth dataset
    var url = "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_0_countries.geojson";
    
    d3.json(url, function(err, json){

        //draw paths once
        svg.selectAll("path").data(json.features).enter().append("path")
            .attr("d", path).attr("stroke","#00aaff");
            
    });

<script src="https://d3js.org/d3.v4.min.js"></script>

<h2>Fig. 1 Multiple paths not closed properly</h2>

<svg width="600px" height="300px" id="map"></svg>

<p>Uses D3 Version 4.13.0</p>
<p>GeoJson data source: <a href="https://github.com/nvkelso/natural-earth-vector/tree/master/geojson">https://github.com/nvkelso/natural-earth-vector/tree/master/geojson</a></p>
&#13;
&#13;
&#13;

&#13;
&#13;
var svg = d3.select("#map").style("border","0px solid #000000");

    var width = 600;
    var height = 300;

    var proj = d3.geoEquirectangular().scale(95).translate([300,150]);
    var path = d3.geoPath(proj);

    //countries from natural earth dataset
    var url = "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_0_countries.geojson";
    
    d3.json(url, function(err, json){

        //draw paths once
        svg.selectAll("path").data(json.features).enter().append("path")
            .attr("d", path).attr("stroke","#00aaff");
            
    });
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-geo.v1.min.js"></script>

<h2>Fig. 2 Map renders as expected</h2>
<p>The only difference from above is the inclusion of <a href="https://d3js.org/d3-geo.v1.min.js">https://d3js.org/d3-geo.v1.min.js</a></p>

<svg width="600px" height="300px" id="map"></svg>

<p>Uses D3 Version 4.13.0</p>
<p>GeoJson data source: <a href="https://github.com/nvkelso/natural-earth-vector/tree/master/geojson">https://github.com/nvkelso/natural-earth-vector/tree/master/geojson</a></p>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

我相信你引用的是一个关注d3如何引用多边形的issue on github。该问题可导致在某些投影上与南极重叠的多边形的反转。

d3-geo.v1.min.js的当前版本位于https://d3js.org/d3-geo.v1.min.js,为1.10,已于3月发布。此版本实现了此修复程序。 d3v4(4.13)的最新版本于1月29日发布,因此不包含修复,因此发现了行为。