jonnyhuck / mapnik-scalebar

A quick and dirty PIL-based scale bar generator for Mapnik maps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mapnik-scalebar

A quick and dirty PIL-based scale bar generator for Mapnik maps

example scalebar

Dependencies

Usage

This script is designed to provide a simple way to add a 'quick and dirty' scale bar to a Mapnik map using PIL. It works out how long it wants to be based upon the size of the map, and draws itself to that size. Simple!

Usage is as simple as:

import scalebar

# make a mapnik map
m = mapnik.Map(600, 600)

...

# render mapnik map to file
mapnik.render_to_file(m, 'map.png', 'png')

# open rendered map for editing
im = Image.open('images/wgs84.png')

# add scalebar
sb = scalebar.getScaleBar(m)
im.paste(sb, (5, 5))

# save output
im.save('images/wgs84.png', "png")

This function does nothing fancy, you can't tell it how long you want the scalebar to be, what colour or what units. It is purely intended as a cheap and nasty approach, though if you want more control it should be relatively simple to modify the code.

More detailed usage is given in test.py

Projection

scalebar can be used with a projected CRS, or with a geographic one (though this is not generally good practice). If you do wish to do the latter, scalebar handles this by calculating the length of a degree at the centre of the map using a simple equirectangular projection (the same as mapnik will use to draw the map), and using this to calculate the length of the scalebar.

To facilitate this you can, if you wish, add the radius of the sphere from which you are working using th R argument (if you do not provide one, it assumes 6371008.771415m - the value for WGS84):

sb = scalebar.getScaleBar(m, R=6371008.771415)

Here is an example of a projected map of the United Kindom with a Scalebar:

uk-projected

And the same map using a 'default' equirectangular projection:

uk-projected

DPI

scalebar bases some of its cosmetic measurements on a conversion from mm to px, and uses a default value of 90.7dpi, which is based upon the OGC definition of a pixel. If you would like to print to a particular resolution, you can edit this using the dpi argument:

sb = scalebar.getScaleBar(m, dpi=90.7)

Acknowledgements

The Open-Sans Font

For convenience, the Open-Sans font has been bundled with this software. This is available under the Apache 2.0 license. This copy was obtained from Google Fonts.

Natural Earth Data

The dataset included for use with the test.py file is derived from the freely available dataset at Natural Earth. These dataset have been released into the public domain.

About

A quick and dirty PIL-based scale bar generator for Mapnik maps

License:GNU General Public License v3.0


Languages

Language:Python 100.0%