Web Map

Use d3js to show the map of New Zealand regional council boundaries

The article introduces how to use d3js showing the map of New Zealand regional council boundaries.

You can get the geometry data of NZ regional council from the NZ official website:, remember to download the WGS84(EPSG:4326) projection format.

I downloaded the original data from the above URL and converted it to the Topo-JSON type as below.

Move the mouse to the map to get the regional councils’ basic info:

The source code is here:

<script src=""></script>
<script src=""></script>
<div id="map-nz-regional-council"></div>

        window.addEventListener('load', async () => {
            await init_map();

        async function init_map(){
            let width = 960
                , height = 960
                , centered;

            const file_name_topojson = '/wp-content/uploads/2021/03/regional_council_2018_generalised.topojson.json';
            const topo_json = await d3.json(file_name_topojson);

            const zoom = d3.zoom()
                .scaleExtent([1, 4])
                .on('zoom', zoomed);

            function zoomed(event) {
                const {transform} = event;
                g.attr("transform", transform);
                g.attr("stroke-width", 1 / transform.k);

            const projection =
                    .center([176, -38]);

            const path = d3.geoPath(projection);

            const svg ='#map-nz-regional-council').append('svg')
                .attr('width', width)
                .attr('height', height)
                .attr('fill', '#fff');

                .attr('class', 'background')
                .attr('width', width)
                .attr('height', height)
                .on('click', clicked);


            const g = svg.append('g');

            const textLayer = g.append('g');

            const mapLayer = g.append('g')
                .classed('map-layer', true);

            // const features = geo_json.features;

            // Draw each provinces as a path
                // .data(geo_json.features)
                .data(topojson.feature(topo_json, topo_json.objects['regional_council_2018_generalised']).features)
                .attr('d', path)
                .attr('vector-effect', 'non-scaling-stroke')
                .style('stroke', '#000000')
                // .style('fill', fillFn)
                .on('mouseover', mouseover)
                .on('mouseout', mouseout)
                .on('click', clicked)

            function clicked(d){
                const pointer = d3.pointer(d);
                const res = projection(pointer);


            function showInfo(data){
                if(data && data[0]){
                    Object.keys(data[0]['properties']).map((k, i) => {
                            .attr('x', 0)
                            .attr('y', i * 14)
                            .attr('dy', '1.2em')
                            .style('fill', '#000')
                            .text( k + " => " + data[0]['properties'][k] );

            function mouseover(d){
                // Highlight hovered province
      'fill', 'orange');

            function mouseout(d){
                // Reset province color
                    .style('fill', function(d){return centered && d===centered ? '#D5708B' : '#ffffff';});



Leave a Reply

Your email address will not be published. Required fields are marked *