Wednesday, 19 June 2013

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.

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