【问题标题】:D3 world-50m.json projection filling incorrectlyD3 world-50m.json 投影填充不正确
【发布时间】:2017-08-10 06:23:20
【问题描述】:

我有一个 world-50m.json 文件的投影工作,但是当我用颜色填充它时,有几个国家在边缘被切断,从而在整个地图上创建水平填充部分/线。

这实际上在 d3-geo 示例投影上可见:https://github.com/d3/d3-geo/blob/master/test/data/world-50m.json

是否还有没有这些截止国家/地区的其他 JSON 文件?或者我可以从填充中省略特定的多边形?不太确定我如何找到每个国家/地区的问题。虽然大多数都很小,如果省略也不会错过,但一个主要的似乎是俄罗斯。

这是我的参考代码:

var w = 960,
    h = 660,
    active = d3.select(null);

var projection = d3.geoMercator()
    .scale(150)
    .translate([w/2, h/2])
    .precision(.1);

var path = d3.geo.path()
    .projection(projection);

var countries = svg.append("svg:g").attr("id", "countries");

d3.json("world-50m.json", function(error, us) {
  mapFeatures = topojson.feature(us, us.objects.countries).features;
  mapFeatures.type = "countries";
  drawMap();
});

function drawMap() {
  countries.selectAll("path")
        .data(mapFeatures)
        .enter().append("svg:path")
        .attr("d", path)
        .attr("class", "feature")
        .attr("data-id", function(d) { return d.id; })
        .style("fill", "blue")
        .style("stroke", "white")
        .style("stroke-width", ".2px");
};

任何帮助将不胜感激!

【问题讨论】:

  • 是否可以在您的代码块中也包含您的pathprojection
  • 您的问题让我找到了答案!我使用了错误的路径投影。应该使用geoPath() 而不是geo.path() 请回答问题,以便我将其标记为正确:)
  • @MatOwen11 这很可能不是正确的解释。看看 Antimeridian Cutting,例如比较这个example 和这个example 没有逆子午线切割。
  • @altocumulus 非常有趣的东西!我认为你是对的,问题是逆子午线切割不起作用。在这个特定的例子中,我似乎使用了来自 d3 v4 的投影调用和来自 d3 v3 的路径调用。因此,我的问题通过更改路径和投影格式以匹配正确版本的 d3 来解决,这反过来又导致支持逆子午线切割的 D3 地理投影系统重新开始工作。

标签: javascript json d3.js map-projections


【解决方案1】:

这个解决方案在上面的 cmets 中有详细说明,是在 @Andrew Reid 和 @altocumulus 的帮助下实现的。

观察到的问题是由于 d3 v3 和 d3 v4 语法之间的路径和投影调用不匹配而导致 d3 未正确处理的 Antimeridian Cutting 实例。

通过将geo.path() 更改为geoPath() 解决了该问题,这更正了不匹配并启用了 d3 使用其逆子午线切割支持正确渲染地图。

下面是正确的代码:

var w = 960,
    h = 660,
    active = d3.select(null);

var projection = d3.geoMercator()
    .scale(150)
    .translate([w/2, h/2])
    .precision(.1);

var path = d3.geoPath()
    .projection(projection);

var countries = svg.append("svg:g").attr("id", "countries");

d3.json("world-50m.json", function(error, us) {
  mapFeatures = topojson.feature(us, us.objects.countries).features;
  mapFeatures.type = "countries";
  drawMap();
});

function drawMap() {
  countries.selectAll("path")
        .data(mapFeatures)
        .enter().append("svg:path")
        .attr("d", path)
        .attr("class", "feature")
        .attr("data-id", function(d) { return d.id; })
        .style("fill", "blue")
        .style("stroke", "white")
        .style("stroke-width", ".2px");
};

【讨论】:

  • 我还是有点迷茫。 d3.geo.path() 调用一定给了你一个类型错误,对吧? 无法读取未定义的属性“路径”。另一方面,你说你得到了它的工作。你能告诉我这个吗?
  • 抱歉回复晚了,我没有收到类型错误,我认为这是因为我同时初始化了 d3 v3 和 d3 v4。这意味着路径和投影调用路由到它们各自的 d3 版本,但不能一起正常工作。为了简化问题,我从问题中省略了自定义 d3 变量,但我现在意识到在我的问题中包含更多上下文会有所帮助。
猜你喜欢
  • 1970-01-01
  • 2017-09-04
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多