#!/usr/bin/python
#
# Generates a single large PNG image for a UK bounding box
# Tweak the lat/lon bounding box (ll) and image dimensions
# to get an image of arbitrary size.
#
# To use this script you must first have installed mapnik
# and imported a planet file into a Postgres DB using
# osm2pgsql.
#
# Note that mapnik renders data differently depending on
# the size of image. More detail appears as the image size
# increases but note that the text is rendered at a constant
# pixel size so will appear smaller on a large image.

from mapnik import *
from optparse import make_option,OptionParser
import sys, os

def render_image(ll, mapfile, map_uri="image.png", z=10):
    prj = Projection("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs +over")
    c0 = prj.forward(Coord(ll[0],ll[1]))
    c1 = prj.forward(Coord(ll[2],ll[3]))

    yfactor = (c1.y - c0.y) / 78097
    xfactor = (c1.x - c0.x) / (c1.y - c0.y)
    imgy = int(yfactor * (2 ** (z - 1)))
    imgx = int(imgy * xfactor)

    m = Map(imgx,imgy)
    load_map(m,mapfile)
    bbox = Envelope(c0.x,c0.y,c1.x,c1.y)
    m.zoom_to_box(bbox)
    im = Image(imgx,imgy)
    render(m, im)
    view = im.view(0,0,imgx,imgy) # x,y,width,height
    view.save(map_uri,'png')

    return (imgx, imgy) # return size of generated image

if __name__ == "__main__":
    try:
        mapfile = os.environ['MAPNIK_MAP_FILE']
    except KeyError:
        mapfile = "osm.xml"

    #---------------------------------------------------
    #  Change this to the bounding box you want
    #
    bbox = (-6.5, 49.5, 2.1, 59)
    map_uri = "image.png"
    zoom = 10

    option_list = [
        make_option("-b", "--bbox", type="float", nargs=4, dest="bbox", metavar="minLon minLat maxLon maxLat", help="Bounding box to render", default=bbox),
        make_option("-u", "--map_uri", type="string", dest="map_uri", help="URI to save the image to", default=map_uri),
        make_option("-z", "--zoom", type="int", dest="zoom", help="Zoom level to render", default=zoom),
    ]
    parser = OptionParser(option_list=option_list)
    (options, args) = parser.parse_args()

    render_image(options.bbox, mapfile, options.map_uri, options.zoom)
