score:0

Accepted answer

There area few approaches to accomplish this.

fitSize and fitExtent

One is to modify the projection scale and translate as opposed to scale and center. This is nearly the same operation, but translate pans the projected plane and center will pan the unprojected plane. To do so you need to use projection.fitSize([width,height],geojsonObject), or projection.fitExtent([[x0,y0],[x1,y1]],geojsonObject). The latter will allow margins of say, the first coordinate provided is the top left and the second coordinate provided is the bottom right of a bounding box in which the feature will be constrained.

d3.json(jsonfile, function(error, belgique) {
     projection.fitSize([width,height], belgique);

    // now draw as you would:
    d3.selectAll(".currentmap").remove() ;
    g.selectAll("path")
    .data(belgique.features)
    .enter()
    .append("path")
    .attr("d", path)
    ...

Note that for showing all of a country you need to have a feature that shows the whole country or a feature collection that shows all the parts of a country. You cannot use an array with fitSize or fitExtent, if you have an array of features, you can create a feature collection by using:

var featureCollection = {"type":"featureCollection","features":featureArray}

For your case, I'd suggest using fitSize or fitExtent.

centroid

If you really wanted to modify the center attribute as opposed to translate, or perhaps you want to change the rotation (a more likely outcome for conic conformals in many parts of the world, Belgium should be fine), then you need the geographic coordinates of the center. One way of a handful to do this is to get the centroid of a feature from path.geoCentroid:

var centroid = path.geoCentroid(geojsonObject); 

Then use that to set the projection parameters to rotate:

projection.rotate([ -centroid[0],-centroid[1] ])
projection.center([0,0])

or to center:

projection.rotate([0,0])
projection.center(centroid)

Or a combination of both (depending on map projection type). Now you can apply fitSize or fitExtent, the feature is in the middle already, but now we can set the scale. The reason I suggest this as a potential answer is because not all projections, concic projections in particular, will give desired results by modifying only scale along with translate and/or center.

Of course for conic projections, you may need to find a way to set the parallels as well, but I'll leave that for another answer if it ever comes up.


Related Query

More Query from same tag