让我们制作一个地图教程,坚持投影步骤

时间:2014-03-03 14:55:14

标签: d3.js

我正试图从Mike Bostock's tutorial重新创建一张荷兰地图。我完成了第一步,实际上看到了较小的地图。但当我进入第二步并试图改变投影时,一切都是空白的。这可能是由包括荷属安的列斯群岛在内的数据引起的吗?到目前为止它们是分开的,我可能正在看海洋?

My files

1 个答案:

答案 0 :(得分:0)

在我看来,在投影上设置中心属性是搞乱的事情,可能是因为你的数据正在跨越日期线?我注意到边界框向南和向西延伸。

我的方法是根据位于中心省份之一设置比例和翻译。在此stack overflow问题中,根据Mike的代码自动计算出比例和翻译。我们的想法是使用单位投影然后计算给定特征的边界。然后使用这些边界来计算适当的比例和平移(避免使用center属性)。然后使用重新计算的比例和变换更新投影。

相关代码是:

//select a province to center on
var l = topojson.feature(nld, nld.objects.subunits).features[8],
    //calculate the bounds
    b = path.bounds(l),
    //calculate the scale based on the bounds (note that I've set the proportion to  
    //less than one so you can see all of the Netherlands)
    s = .125 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height),
    //calculate the translation based on the bounds
    t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2];

//update the projection
projection
    .scale(s)
    .translate(t);

你可以找到一个例子here