Center a map in d3 given a geoJSON object

后端 未结 11 2353
慢半拍i
慢半拍i 2020-11-21 22:55

Currently in d3 if you have a geoJSON object that you are going to draw you have to scale it and translate it in order to get it to the size that one wants and translate it

11条回答
  •  一个人的身影
    2020-11-21 23:22

    I'm new to d3 - will try to explain how I understand it but I'm not sure I got everything right.

    The secret is knowing that some methods will operate on the cartographic space (latitude,longitude) and others on the cartesian space (x,y on the screen). The cartographic space (our planet) is (almost) spherical, the cartesian space (screen) is flat - in order to map one over the other you need an algorithm, which is called projection. This space is too short to deep into the fascinating subject of projections and how they distort geographic features in order to turn spherical into plane; some are designed to conserve angles, others conserve distances and so on - there is always a compromise (Mike Bostock has a huge collection of examples).

    enter image description here

    In d3, the projection object has a center property/setter, given in map units:

    projection.center([location])

    If center is specified, sets the projection’s center to the specified location, a two-element array of longitude and latitude in degrees and returns the projection. If center is not specified, returns the current center which defaults to ⟨0°,0°⟩.

    There is also the translation, given in pixels - where the projection center stands relative to the canvas:

    projection.translate([point])

    If point is specified, sets the projection’s translation offset to the specified two-element array [x, y] and returns the projection. If point is not specified, returns the current translation offset which defaults to [480, 250]. The translation offset determines the pixel coordinates of the projection’s center. The default translation offset places ⟨0°,0°⟩ at the center of a 960×500 area.

    When I want to center a feature in the canvas, I like to set the projection center to the center of the feature bounding box - this works for me when using mercator (WGS 84, used in google maps) for my country (Brazil), never tested using other projections and hemispheres. You may have to make adjustments for other situations, but if you nail these basic principles you will be fine.

    For example, given a projection and path:

    var projection = d3.geo.mercator()
        .scale(1);
    
    var path = d3.geo.path()
        .projection(projection);
    

    The bounds method from path returns the bounding box in pixels. Use it to find the correct scale, comparing the size in pixels with the size in map units (0.95 gives you a 5% margin over the best fit for width or height). Basic geometry here, calculating the rectangle width/height given diagonally opposed corners:

    var b = path.bounds(feature),
        s = 0.9 / Math.max(
                       (b[1][0] - b[0][0]) / width, 
                       (b[1][1] - b[0][1]) / height
                   );
    projection.scale(s); 
    

    enter image description here

    Use the d3.geo.bounds method to find the bounding box in map units:

    b = d3.geo.bounds(feature);
    

    Set the center of the projection to the center of the bounding box:

    projection.center([(b[1][0]+b[0][0])/2, (b[1][1]+b[0][1])/2]);
    

    Use the translate method to move the center of the map to the center of the canvas:

    projection.translate([width/2, height/2]);
    

    By now you should have the feature in the center of the map zoomed with a 5% margin.

提交回复
热议问题