Skip to main content

Showing Zoomify images with Leaflet


Today, I'm starting a new blog series written from Foldøy, a tiny island in south west Norway which will be my home for two months. In this first post, I'll show you how to deliver zoomable high-resolution images with Leaflet.

I've brought along 17 kg of mapping and visualization books to keep me occupied:


Fullscreen

Zoom in if you want to see the details! The photo is broken into tiles and then displayed as required. You don't really need Leaflet to create images like this, as Zoomify provides both Flash and HTML5 viewers. I decided to use Leaflet to create an open source alternative, and to learn more about tiling schemes.

This is the JavaScript code needed to show this image:

var map = L.map('photo').setView(new L.LatLng(0,0), 0);

L.tileLayer.zoomify('http://thematicmapping.org/playground/zoomify/books/', {
    width: 5472,
    height: 3648,
    tolerance: 0.8,
    attribution: 'Photo: Bjørn Sandvik'
}).addTo(map);

You have to provide the url to the Zoomify image folder and the with and height dimensions of the original image. You can also specify a display tolerance for the inital zoom level. A value lower than 1.0 might center the image and cut of the edges.

Image source: Microsoft Deep Zoom

The Zoomify tile format is similar to popular map tiling schemes, and I'm extending the Leaflet TileLayer class to show the image. The tiling follows a quadtree or pyramid pattern of increasing resolution, doubling the resolution for each zoom level. The tile size is 256 x 256 px and there are 256 tiles in each folder. The filename for each tile is z-x-y.jpg where z is the zoom level and x/y is the grid position starting from the top left corner.

The code is available on GitHub as a Leaflet plugin.


The bonfire is ready for midsummer on Foldøy island.
Fullscreen

You can create Zoomify tiles from your own image with Zoomify Express or from the Export menu in Photoshop.

Comments

Ralf said…
Nice to hear from you again, Bjorn. Your last blog series was a great inspiration. Without your hints and advices I would never had tried to create this map: http://www.trails.de/mountainbike-trailsmap.php
Thank you, and looking forward to your new series!
Anonymous said…
Nice post. I spent some time recently doing a similar thing - cutting a photo into a TMS layer and displaying using OpenLayers. The difference was that I had a 360 degree cylindrical panorama and displayed it using the wrapDateLine option to get continuous rotation. Could Leaflet do something similar? Would be interested to see if it performed better than OpenLayers.
Bjørn Sandvik said…
Yes, you can do the same with Leaflet TileLayer.
Espen said…
I'm trying to do something very similar, but need to limit the bounds so the user doesn't scroll out of the image and get "lost"...

I've tried setting the max bounds using map.unproject, but I'm getting strange results - map not showing up at all if the bounds are set... Any suggestions? I think this would be a nice addition to your plugin.
Anonymous said…
Lovely plugin ... I wonder if oyu might have also come across the djatoka JPEG2000 image applications server? It would be great to have LeafltJS options with JPEG2000 images as well!
Barney Carroll said…
Espen, this was the big challenge for me too. I've developed zoom and pan functionality — the complicated stuff is bounding, which is why I was considering a robust open source map interface front end. If this can't do it, there's little point.

Do tell if you find a way!
Barney Carroll said…
Espen, I managed to find this example. The boundaries aren't 'hard': you can pan beyond them but immediately afterwards the map will reposition such that the boundaries are respected. Hope this helps.

http://jsfiddle.net/enNVz/16/
Unknown said…
Nice one. I've been searching for a simple web mapping deep zoom implementation for a while, just to stumble upon this one.

good work!
LordOtU said…
Hello, Bjorn!

Pretty cool plugin and article!

But how can I add markers and svg paths over zoomified image? In wich coordinate system I should do this?

I would be thankful if you show me an example.
JG said…
Having a hard time creating the tiles. I'm using the example for imagemagick provided here:

http://multiplans.net/en_Slicing.htm

which is:
convert "image_source.jpg" -crop 256x256 -set filename:tile "%[fx:page.x/256+1]_%[fx:page.y/256+1]" +repage +adjoin "tile_%[filename:tile].jpg"

This does not create the zoom level. Output file is Tile_X_Y.jpg.

Unknown said…
Hello! Could you help me? I try to locate my image to the 0,0 in coordinate system of leaflet, but can't. I write all parametres in my script. Please watch. http://a.disquscdn.com/uploads/mediaembed/images/1306/9641/original.jpg
BrownCode said…
Espen I solved this problem by using the code:

var bottomLeft = new L.LatLng(0, -180),

topRight = new L.LatLng(90, 90),

bounds = new L.LatLngBounds(bottomLeft, topRight);

map.setMaxBounds(bounds);

map.fitBounds(bounds);

Thanks Bjørn Sandvik for the work
Unknown said…
This is very cool! Has anyone tried Zoomify inside a pop-up? I've tried, but had little success. What I want to create is a pop-up that's zoomed to the center of a 360 panorama when opened with an option to scroll/pan.

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 ...