Moore Spatial

GIS and Related Pursuits

By

Adding To the Leaf Pile

I recently presented at the 2013 MN GIS/LIS conference on leaflet plugin development. You can now see my demo and presentation online.

I demonstrate three ways to write leaflet plugins.

  1. Adding a new method to an existing Leaflet object
  2. Overriding an existing method in an existing Leaflet object
  3. Extending an existing Leaflet object to create your own object

 

The presentation can be found here:

PDF, Google Docs PDF, SlideShare

 

The Demo can be found here:

Leaflet-Spiders Demo on MooreSpatial.com

Leaflet-Spiders Demo on GitHub

 

The code can be found here:

GitHub

 

Happy Hacking!

 

By

SSTiles — Stupid Simple Tile Maker

SSTiles is a small project I’ve been working on. It’s a simple PHP script that generates XYZ slippy map tiles on demand.

Ideally you’ll use a correctly projected base image (a square image, in Web Mercator projection). Since the image is square, SSTiles will just slice it up, no scaling is needed.

Usage

Usage is simple. Place the sstiles.php library and the tile.php or pad.php helper script in a directory with your base map, and add the layer to your map like so:

http://example.com/mysite/sstile/tile.php/basemap.jpg/$Z/$X/$Y.png

For Leaflet, you would do:

L.tileLayer('http://example.com/mysite/sstile/tile.php/basemap.jpg/{z}/{x}/{y}.png').addTo(map);

For OpenLayers you would do:
var basemap = new OpenLayers.Layer.XYZ("Basemap", "http://example.com/mysite/sstile/tile.php/basemap.jpg/${z}/${x}/${y}.png");
map.addLayer(basemap);

Basemap types

DEMO

A Nice Square Map

A Nice Square Map

Other times it might not matter if the map is correctly projected, in which case you can use a non-square image which will be sliced and scaled so that resulting tiles are square.

DEMO

A Non-Square Map Is Stretched To Be Square

 

And finally, for the real use case that I wrote this script, if you want to abuse Leaflet (or OpenLayers, etc.) to make a tiled image viewer for high-resolution images you can ask SSTiles to pad the image so that resulting tiles won’t be distorted.

DEMO

padded

Features

  • Lets you use any image as a slippy map!
  • Generates slippy-map tiles from a single top-level image!
  • Stretches the image if it’s not square!
    • OR Pads the image if it’s not square!
  • Caches generated tiles!
  • Auto-updates cache when source file is updated!
  • Sends HTTP caching headers!
  • Supports GD and ImageMagick!

By

What Slippy Map Zoom Level Is Ideal For My Source Map?

If you’re creating XYZ tiles for your slippy map (eg. Leaflet or OpenLayers), and your source is a raster image, you might wonder which zoom level will display the map with the least artifacts.

Or you might not. In any case, if you do want to know what zoom level is ideal, here’s how you find it. The code is in PHP, but should be easy to translate to other languages.

$min_dimension = min($image_width_in_pixels,$image_height_in_pixels);
$zoom_level = round(log($min_dimension/256,2)

Take the smaller of the height or width[1], divide it by 256 and find the base-2 logarithm of that. You will likely need to round that result to get an integer value. That integer will be the zoom level which has to resize your image the least to produce the appropriate tiles.

[1] Really, your source map should likely be square since you’re probably using Web Mercator