【问题标题】:Given latitude and longitude, plotting points on D3 geo projection给定纬度和经度,在 D3 地理投影上绘制点
【发布时间】:2015-08-29 04:39:19
【问题描述】:

我正在学习一些关于 D3 的知识,我一直在研究的一个示例是地理投影(正交)。我意识到有很多关于与此非常相似的主题的问题,但没有一个问题能帮助我理解如何使用投影正确绘制点(其中一些演示了翻译,但对我来说似乎不是一回事) .

代码如下:

var width = 960,
    height = 500;

var projection = d3.geo.orthographic()
    .scale(248)
    .clipAngle(90);

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

var graticule = d3.geo.graticule()
    .extent([[-180, -90], [180 - .1, 90 - .1]]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var line = svg.append("path")
    .datum(graticule)
    .attr("class", "graticule")
    .attr("d", path);

d3.json("readme-world-110m.json", function(error, world) {
    var countries = topojson.feature(world, world.objects.countries).features;
        i = -1,
        n = countries.length;

    var country = svg.selectAll(".country")
      .data(countries)
      .enter().insert("path", ".graticule")
      .attr("class", "country")
      .attr("d", path);

    var allPoints = [];
    for (var lat = -90; lat < 90; lat=lat+10) {
      for (var lon = -180; lon < 180; lon=lon+10) {
          allPoints.push(projection([lat, lon]));
      }
    }

    var intersections = svg.selectAll('.gridpoints')
            .data(allPoints)
          .enter().append('circle', '.gridpoints')
            .attr('cy', d => d[0])
            .attr('cx', d => d[1])
            .attr('r', 5);
});

这个想法只是在所有经纬网交叉点上画一个圆圈。虽然投影和线/国家路径的渲染就像我预期的那样(因为它们来自一个示例),但我对我正在尝试绘制的圆圈有点迷失。结果如下:

如您所见,这些圆圈似乎相互之间定位正确,但它们似乎逆时针旋转了大约 -90 度,我不明白为什么。谁能告诉我我做错了什么?

【问题讨论】:

  • D3 投影按经度、纬度的顺序接受参数,而不是相反。
  • 我想你想要.attr('cx', d =&gt; projection(d)[0]).attr('cy', d =&gt; projection(d)[1])
  • @LarsKotthoff 啊,谢谢!我注意到.centroid 函数,给定一个路径,会产生一个[long, lat],我认为这很奇怪,但在应用投影时我没有考虑到这一点。它就在文档中,我只是没有仔细阅读它,因为我只是假设[lat, long]。如果你想把它写成答案,我很乐意以你的方式抛出 25 个代表。

标签: javascript d3.js


【解决方案1】:

简单的错误——投影采用reverse order to what you've specified中的参数:

根据输入数组 [longitude, latitude] 返回一个数组 [x, y]。

【讨论】:

    猜你喜欢
    • 2015-06-18
    • 1970-01-01
    • 2017-10-09
    • 2012-04-19
    • 2012-07-04
    • 1970-01-01
    • 2021-04-09
    • 2023-03-05
    • 2013-05-23
    相关资源
    最近更新 更多