Skip to main content

New Leaflet plugin to handle multiple TileMill layers

My setup for the population density map of New Zealand made it easy to create new choropleth maps with New Zealand census data. This blog post explains how you can use Leaflet to switch between multiple interactive layers created with TileMill.

I wanted to create a map of the social geography of New Zealand, using the Index of Deprivation from the Department of Public Health, University of Otago. I downloaded a Excel sheet containing data for the 2006 census area units, which I also used for my population density map. I simply added the data to the same SQLite database, and created the map using the same techniques described in two previous blog posts (1, 2).


The Index of Deprivation is constructed from nine Census 2006 variables, and provides a summary deprivation score from 1 to 10. A score of 1 is allocated to the least deprived 10 percent of areas, and 10 is allocated to the most deprived 10 percent of areas. You'll find more information about the index in the Atlas of Socioeconomic Deprivation in New Zealand.

Wax allows you to easily add an interactive TileMill map to Leaflet or other mapping libraries. Adding more than one interactive layer is not that straightforward, so I wrote a Leaflet plugin:


This plugin allows you to switch between various layers (interactive or not) and it will automatically load and display map legends, and remove the elements when they're no longer needed. It's easy to use the plugin:

Include a wax property when you create a tile layer, containing a TileJSON object or an URL to a TileJSON file.  

var population = L.tileLayer('tiles/nz-popden/{z}/{x}/{y}.png', {
  attribution: 'Statistics New Zealand',
  wax: 'tiles/nz-popden.tilejson'
});


After you've created the map you simply add:

L.wax(map);
 
That's it! :-)

You can try to switch between the various basemaps using the layers control below:



Fullscreen map

Comments

Unknown said…
Hi,

This is very cool stuff. I am struggling to get the functionality to work. Would you mind sharing the .html and script.js that you used to integrate Wax and Leaflet for this cool web map. Thanks!
Unknown said…
Hi Bjørn Sandvik,
Thanks a lot for explaining lots of stuff about using MapBox (TileMill) tricks and techniques. I am quite new in both mapping and web-designing, despite leaned quite good staff by reading your blogs and repeating myself. I am greatly thankful to you for that.
Bjørn Sandvik, I have few "stand-alone" maps with their specific legends and tooltips prepared by TileMill and placed in MapBox. They are all somehow interrelated. I read your this blog but couldn't completely figured out how I can "join" all together and place in one interactive map and show on my website. Could you please explain a bit in detail about leaflet, modifying it, placing and editing. You can say it "interactive multilayer map" for dummies :)
I would be quite thankful for that.
I wish you the very best in doing what you love, and helping others.

Baha
Bjørn Sandvik said…
Gregg: The HTML and JavaScript is not minified, so you can have a look here.

Bakyt: Please try MapBox support.
Unknown said…
Hi Bjørn,

I've been looking at your post and find your implementation very interesting.

Am trying to replicate the functionality using offline tiles generated using tilemill. The json files produced by tilemill however are surrounded by a function 'grid();' helnce are throwing an error "grid is not defined"

Any pointers?
Anonymous said…
Greetings. Great work on the NZ map. I'm wondering if you're using this leaflet plugin with the infostreams php code from the previous post. I'm trying to do exactly this, but can't seem to figure out how to initiate the layers. I've successfully got the leaflet layer control gadget on the map, but when selecting the the layers the map does not change. The map can be viewed here
Thanks!

Popular posts from this blog

Creating a WebGL Earth with three.js

This blog post will show you how to create a WebGL Earth with three.js , a great JavaScript library which helps you to go 3D in the browser. I was surprised how easy it seemed when reading a blog post  by Jerome Etienne . So I decided to give it a try using earth textures  from one of my favourite cartographers, Tom Patterson . WebGL is a JavaScript API for rendering interactive 3D graphics in modern web browsers without the use of plug-ins. Three.js is built on top of WebGL, and allows you to create complex 3D scenes with a few lines of JavaScript. If your browser supports WebGL you should see a rotating Earth below: [ Fullscreen ] To be able to display something with three.js, you need three things: a scene, a camera and a renderer. var width  = window.innerWidth,     height = window.innerHeight; var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(45, width / height, 0.01, 1000); camera.position.z = 1.5; var rende...

Thematic Mapping Engine

It's time to introduce the Thematic Mapping Engine (TME). In my previous blog posts, I've shown various techniques of how geobrowsers can be used for thematic mapping. The goal has been to explore the possibilites and to make these techniques available to a wider audience. The Tematic Mapping Engine provides an easy-to-use web interface where you can create visually appealing maps on-the-fly. So far only prism maps are supported, but other thematic mapping techniques will be added in the upcoming weeks. The engine returns a KMZ file that you can open in Google Earth or download to your computer. My primary data source is UNdata . The above visualisation is generated by TME ( download KMZ ) and shows child mortaility in the world ( UNdata ). The Thematic Mapping Engine is also an example of what you can achieve with open source tools and datasets in the public domain: A world border dataset is loaded into a MySQL database . The same database contains tables with statistics ...

Creating 3D terrains with Cesium

Previously, I’ve used three.js to create 3D terrain maps in the browser ( 1 , 2 , 3 , 4 , 5 , 6 ). It worked great for smaller areas, but three.js doesn’t have built-in support for tiling and advanced LOD algorithms needed to render large terrains. So I decided to take Cesium for a spin. Cesium is a JavaScript library for creating 3D globes and 2D maps in the browser without a plugin. Like three.js, it uses WebGL for hardware-accelerated graphics. Cesium allows you to add your own terrain data, and this blog post will show you how. Impressed by the terrain rendering in @CesiumJS - with a 10m elevation model for Norway! Farewell Google Earth. pic.twitter.com/RQKvfu2hBb — Bjørn Sandvik (@thematicmapping) October 4, 2014 Compared to  the dying Google Earth plugin , it's quite complicated to get started with Cesium. The source code is well documented and the live coding Sandcastle is great, but there is a lack of tutorials  and my development slows down when ...