standard coordinates in d3.js

311 Views Asked by At

I've been keen on developing a choropleth map for Namibia.But found two interesting tools. leaflet and D3, though leaflet has clear instructions to implement which i did Its not so functionally in line with what i want to do. And that is where D3Geo came in. I've everything set but this function below to set my projection.

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

Is there just no function to just simply add the co-ordinates as how its done in the leaflet function below. for us who are not so geocentric.

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

And if there isn't, can someone please guide me on how to convert the coordinates (-22.26,16.52 )to show Namibia using the d3.geo.conicConformal().

1

There are 1 best solutions below

0
mgc On

Correct me if it didn't address your issue (maybe you can provide a minimal example showing where you are stuck, using JSFiddle for example), but if I understand well you want to move/zoom/center the displayed image on the extend of your country. Here is an example doing this (I also added some code on how the layer was added for consistency):

// 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);
};

Also, note that this example use d3 v4 (but in this case it doesn't change a lot apart from the naming of geoPath and geoConicConformal)