d3.js中的标准坐标

时间:2016-09-11 19:51:05

标签: d3.js d3.geo

我一直热衷于为纳米比亚开发一个等值线图。但他找到了两个有趣的工具。传单和D3,虽然传单有明确的指示实施,我做了它的功能不符合我想要做的。这就是D3Geo的用武之地。我已经完成了所有设置,但下面的功能可以设置我的预测。

var projection = d3.geo.conicConformal()
.rotate([, 0])
.center([0, 0])
.parallels([ , ])
.scale(1000) 

是否只是简单地添加坐标作为其在下面的传单函数中的完成方式的功能。对于那些不那么地心的人来说。

var map = L.map('mapid').setView([-22.26,16.52], 5);

如果没有,有人可以指导我如何转换坐标(-22.26,16.52)以使用d3.geo.conicConformal()显示纳米比亚。

1 个答案:

答案 0 :(得分:1)

如果它没有解决你的问题,请纠正我(也许你可以提供一个最小的例子来显示你被卡住的地方,例如使用JSFiddle),但是如果我理解得很好你想要移动/缩放/在您所在国家/地区的中心显示图像。这是一个这样做的例子(我还添加了一些关于如何添加图层以保持一致性的代码):

// Define the projection you want to use,
// setting scale and translate to some starting values :
var projection = d3.geoConicConformal()
                        .translate([0, 0])
                        .scale(1) 

var layer_name = "your_layer_name";
var geo_features = topojson.feature(topoObj, topoObj.objects[layer_name]).features;

// Define the path generator :
var path = d3.geoPath().projection(projection);

var width = 800,
    height = 600;

// This is the main svg object on which you are drawing :
var map = d3.select("body").append("div")
                .style("width", width + "px")
                .style("height", height + "px")
            .append("svg")
                .attr("id", "svg_map")
                .attr("width", width)
                .attr("height", height);

// Add you layer to the map
map.append("g").attr("id", layer_name)
      .attr("class", "layers")
      .selectAll("path")
      .data(geo_features)
      .enter().append("path")
      .attr("d", path)
      .attr("id", (d,i)=> "feature_" + i)
      .styles({"stroke": "rgb(0, 0, 0)", "fill": "beige")

// Where the job is done :
scale_to_layer(layer_name)

function scale_to_layer(name){
    var bbox_layer = undefined;
    // use all the paths of the layer (if there is many features)
    // to compute the layer bbox :
    map.select("#"+name).selectAll('path').each(function(d, i){
        var bbox_path = path.bounds(d);
        if(bbox_layer === undefined){
            bbox_layer = bbox_path;
        }
        else {
            bbox_layer[0][0] = bbox_path[0][0] < bbox_layer[0][0] 
                               ? bbox_path[0][0] : bbox_layer[0][0];
            bbox_layer[0][1] = bbox_path[0][1] < bbox_layer[0][1]
                               ? bbox_path[0][1] : bbox_layer[0][1];
            bbox_layer[1][0] = bbox_path[1][0] > bbox_layer[1][0]
                               ? bbox_path[1][0] : bbox_layer[1][0];
            bbox_layer[1][1] = bbox_path[1][1] > bbox_layer[1][1]
                               ? bbox_path[1][1] : bbox_layer[1][1];
        }
    });
    // Compute the new scale param, with a little space (5%) around the outer border :
    var s = 0.95 / Math.max((bbox_layer[1][0] - bbox_layer[0][0]) / width,
                            (bbox_layer[1][1] - bbox_layer[0][1]) / height);
    // Compute the according translation :
    var t = [(width - s * (bbox_layer[1][0] + bbox_layer[0][0])) / 2,
             (height - s * (bbox_layer[1][1] + bbox_layer[0][1])) / 2];
    // Apply the new projections parameters :
    projection.scale(s)
            .translate(t);
    // And redraw your paths :
    map.selectAll("g.layer").selectAll("path").attr("d", path);
};

另请注意,此示例使用的是d3 v4(但在这种情况下,除了geoPathgeoConicConformal的命名之外,它并没有发生太大变化)

相关问题